Created
July 23, 2014 15:34
-
-
Save hkrishna/7749ea1dd17fcf0df0cb to your computer and use it in GitHub Desktop.
A quick and dirty script that fetches bike routes from mapzen's osrm service and converts points geom (from the input geojson) into linestrings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
require 'net/http' | |
require 'nokogiri' | |
json = File.read('citibike_in.geojson') | |
data = JSON.parse(json) | |
out = data | |
out['features'].each{ |f| | |
pt = f['geometry']['coordinates'][0] | |
src = pt[0] | |
des = pt[1] | |
url = "http://osrm.mapzen.com/bicycle/viaroute?loc="+src[1].to_s+"%2C"+src[0].to_s+"&loc="+des[1].to_s+"%2C"+des[0].to_s+"&output=gpx" | |
resp = Net::HTTP.get_response(URI.parse(url)) | |
case resp | |
when Net::HTTPSuccess then | |
# puts "2xx" | |
data = resp.body | |
data = Nokogiri::XML(data) | |
tt =[] | |
tt.push(f['geometry']['coordinates'][0][0]) | |
data.css("rtept").map{|n| tt.push([n.attr("lon").to_f,n.attr("lat").to_f])} | |
tt.push(f['geometry']['coordinates'][0][1]) | |
f['geometry']['coordinates'] = [tt] | |
when Net::HTTPClientError | |
puts "4xx" | |
else | |
puts "no dice" | |
end | |
} | |
File.open('citibike_out.geojson', 'w') do |file| | |
file.write(JSON.pretty_generate(out)) | |
end | |
puts JSON.pretty_generate(out) | |
# puts out.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment