Created
January 11, 2011 11:16
-
-
Save thokra/774309 to your computer and use it in GitHub Desktop.
Set to true on line 44 to find bicycle routes
This file contains 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
require 'net/http' | |
require 'rexml/document' | |
MAPS_KEY = '' | |
UNIT = 'imperial' # or 'metric' (For us that uses the metric system) | |
states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Florida', 'Georgia', | |
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', | |
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', | |
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', | |
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'] | |
western = {:address => '', :long => 180.0, :lat => 0.0} | |
eastern = {:address => '', :long => -180.0, :lat => 0.0} | |
i = 0 | |
states.each do |state| | |
# Just to give a bit of progression in the terminal | |
puts "\e[H\e[2J#{i*2}%" | |
i += 1 | |
url = URI.parse("http://maps.google.com/maps/geo?q=#{URI.escape state}%20Ruby%20Street&output=xml&key=#{MAPS_KEY}") | |
xml = Net::HTTP.get_response(url) | |
document = REXML::Document.new(xml.body) | |
document.elements.each('kml/Response/Placemark') do |placemark| | |
next unless placemark.elements['AddressDetails/Country/CountryNameCode'].text == 'US' | |
coordinates = placemark.elements['Point/coordinates'].text.split(',') | |
current_position = {:address => placemark.elements['address'].text, :long => coordinates[0].to_f, :lat => coordinates[1].to_f} | |
western = current_position if western[:long] > current_position[:long] | |
eastern = current_position if eastern[:long] < current_position[:long] | |
end | |
end | |
puts "\e[H\e[2JCalculating the distance" | |
distance_url = URI.parse("http://maps.google.com/maps/api/directions/xml?origin=#{eastern[:lat]},#{eastern[:long]}&destination=#{western[:lat]},#{western[:long]}&sensor=false&key=#{MAPS_KEY}&units=#{UNIT}") | |
distance_xml = Net::HTTP.get_response(distance_url) | |
distance_document = REXML::Document.new(distance_xml.body) | |
distance = distance_document.elements["DirectionsResponse/route/leg/distance/text"].text | |
#Set this to true to find the shortest bicycle route | |
if false | |
puts "\e[H\e[2JCalculating the bicycle distance" | |
bicycle_url = URI.parse("http://maps.google.com/maps/api/directions/xml?origin=#{eastern[:lat]},#{eastern[:long]}&destination=#{western[:lat]},#{western[:long]}&sensor=false&key=#{MAPS_KEY}&units=#{UNIT}&mode=bicycling&alternatives=true") | |
bicycle_xml = Net::HTTP.get_response(bicycle_url) | |
bicycle_document = REXML::Document.new(bicycle_xml.body) | |
bicycle_distance = 0 | |
bicycle_value = 0 | |
bicycle_document.elements.each("DirectionsResponse/route/leg/distance") do |dist| | |
if dist.elements['value'].text.to_i > bicycle_value | |
bicycle_distance = dist.elements['text'].text | |
bicycle_value = dist.elements['value'].text.to_i | |
end | |
end | |
end | |
puts "\e[H\e[2JWestern most address #{western[:address]}" | |
puts "Eastern most address #{eastern[:address]}" | |
puts "Distance: #{distance}" | |
puts "Shortest bicycle distance: #{bicycle_distance}" if bicycle_distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment