Created
January 11, 2011 10:17
-
-
Save gkleiman/774269 to your computer and use it in GitHub Desktop.
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
| require 'net/http' | |
| require 'rexml/document' | |
| require 'CGI' | |
| Location = Struct.new(:longitude, :latitude, :state, :country, :city, :zip_code) | |
| Location.class_eval do | |
| def self.from_placemark_and_state(placemark, state) | |
| longitude, latitude = placemark.elements['Point/coordinates'].text.split(',').map(&:to_f) | |
| location = Location.new(longitude, latitude, state) | |
| paths = %w(AddressDetails/Country/CountryNameCode AddressDetails/Country/AdministrativeArea/SubAdministrativeArea/Locality/LocalityName AddressDetails/Country/AdministrativeArea/SubAdministrativeArea/Locality/PostalCode/PostalCodeNumber) | |
| location.country, location.city, location.zip_code = paths.map do |path| | |
| result = placemark.elements[path] | |
| result != nil ? result.text : nil | |
| end | |
| location | |
| end | |
| def to_s | |
| members.map { |member| "#{member.to_s}=\"#{self[member]}\"" }.join(' ') | |
| end | |
| end | |
| def get_extreme_locations(states, maps_key) | |
| western_location = Location.new(180.0) | |
| eastern_location = Location.new(-180.0) | |
| states.each do |state| | |
| url = "http://maps.google.com/maps/geo?q=#{CGI.escape(state)}%20Ruby%20Street&output=xml&key=#{maps_key}" | |
| doc = REXML::Document.new(Net::HTTP.get_response(URI.parse(url)).body) | |
| doc.elements.each('kml/Response/Placemark') do |placemark| | |
| location = Location.from_placemark_and_state(placemark, state) | |
| next unless location.country == "US" | |
| western_location = location if western_location.longitude > location.longitude | |
| eastern_location = location if eastern_location.longitude < location.longitude | |
| end | |
| end | |
| [ eastern_location, western_location ] | |
| end | |
| def driving_distance_between_locations(location1, location2, maps_key) | |
| # The following url returns the driving directions, it would be better to get the cycling distance or to calculate the straigth-line distance | |
| url = "http://maps.google.com/maps/api/directions/xml?origin=#{location1.latitude},#{location1.longitude}&destination=#{location2.latitude},#{location2.longitude}&sensor=false&key=#{maps_key}" | |
| doc = REXML::Document.new(Net::HTTP.get_response(URI.parse(url)).body) | |
| doc.elements["DirectionsResponse/route/leg/distance/text"].text | |
| end | |
| 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'] | |
| maps_key = 'insert your key here' | |
| eastern_location, western_location = get_extreme_locations(states, maps_key) | |
| puts "Eastern location's data: #{eastern_location.to_s}" | |
| puts "Western location's data: #{western_location.to_s}" | |
| puts "Distance: #{driving_distance_between_locations(eastern_location, western_location, maps_key)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment