OSM Ruby Scripts

From 1ste Gimnechiske Wiki

Jump to: navigation, search


This is a collection of Scripts I have made or hacked, based on Ruby, to use with OpenStreetMap and services based on OSM.

Contents

routes.rb

This is a script made by Matt Amos. I have the copy from another OSM user, but think this is the original. The only changes I have done is that I didn't actually copy the script, but write it off from another screen. The script takes a list of positions from an .yml file, and prepares a ; devided cvs with distances between the various cities, using a routing service by CloudMade.

sample.yml

---
Los Angeles, California:
- 34.05
- -118.25
Tempe, Arizona: 
- 33.41463
- -111.90938
Kansas City, Kansas: 
- 39.09947
- -94.58133
Costa Mesa, California: 
- 33.66978
- -117.90432
Peoria, Illinois: 
- 40.69365
- -89.58899

routes.rb

require 'rubygems'
require 'cloudmade'
require 'yaml'
include CloudMade

API_KEY='' # PUT YOUR API KEY HERE
CITIES = YAML::load_file(ARGV[0])
CM = Client.from_parameters(API_KEY)

def route_or_nil(from, to)
  backoff = 60
  loop do
    begin
      return CM.routing.route(Point.new(CITIES[from]), Point.new(CITIES[to]))
    rescue Timeout::Error
      STDERR.puts "[#{Time.now}] Timeout, retrying..."
      sleep(backoff)
      backoff = [60 * 30, backoff * 2].min
    rescue HTTPError => e
      STDERR.puts "[#{Time.now}] HTTP error: #{e}, retrying..."
      sleep(backoff)
      backoff = [60 * 30, backoff * 2].min      
    rescue
      STDERR.puts "[#{Time.now}] Other error: #{e}, retrying..."
      sleep(backoff)
      backoff = [60 * 30, backoff * 2].min      
    end
  end
rescue RouteNotFound
  nil
end

num_cities = CITIES.length
CITIES.keys.sort.each do |i|
  CITIES.keys.sort.each do |j|
    r = route_or_nil(i, j)
    if r.nil?
      puts "#{i};#{j};NO ROUTE"
    else
      puts "#{i};#{j};#{r.summary.total_distance}"
    end
  end
end

to_html.rb

This is a script originally made by Matt Amos, but have been hacked several times by various users. I have my copy from the script page of the Brasil 250 Cidades (Brazil 250 Cities) project, that uses this to generate lists of distances between the most important cities of Brazil, in order to fix routing internally in Brazil. I have adopted it to make a distance table for the state Espírito Santo, and also between South American capitals. The two projects will in turn get two different hacks of this script, where the one for Espírito Santo will be the most hacked. This script also contains formulas to calculate great circle distances in direct lines between the cities, as an aid to find out "how healthy" the various routes are.