From: maxious Date: Tue, 27 Apr 2010 10:37:48 +0000 Subject: Add geocoder for guessing X-Git-Url: http://maxious.lambdacomplex.org/git/?p=bus.git&a=commitdiff&h=f0151a997650115ff26645f1800fcc628a26fe64 --- Add geocoder for guessing --- --- a/maxious-canberra-transit-feed/03-locatetimepoints.rb +++ b/maxious-canberra-transit-feed/03-locatetimepoints.rb @@ -3,6 +3,26 @@ require 'highline.rb' include HighLine + +require 'rubygems' +require 'json' +require 'net/http' +def cbr_geocode(query) + base_url = "http://geocoding.cloudmade.com/daa03470bb8740298d4b10e3f03d63e6/geocoding/v2/find.js?query=" + url = "#{base_url}#{URI.encode(query)}&bbox=-35.47,148.83,-35.16,149.25&return_location=true" + resp = Net::HTTP.get_response(URI.parse(url)) + data = resp.body + + # we convert the returned JSON data to native Ruby + # data structure - a hash + result = JSON.parse(data) + + # if the hash has 'Error' as a key, we raise an error + if result.has_key? 'Error' + raise "web service error" + end + return result +end require 'yaml' require 'pp' @@ -111,6 +131,32 @@ null_points.each do |null_point_name| pp null_point_name + name = null_point_name.to_s.gsub(/\\/, '\&\&').gsub(/'/, "''") + results = cbr_geocode(null_point_name[0]) + if !results.empty? + results['features'].each_with_index { |feature,index| + print "#{index}: #{feature['properties']['name']} (#{feature['location']}) => #{feature['centroid']['coordinates']}\n" + } + nodeID = ask("Enter selected node ID:", :integer) + if results['features'].at(nodeID) != nil + node = results['features'][nodeID] + guess = ask_if("Is this a guess?") + puts "Location #{node['centroid']['coordinates'][0]},#{node['centroid']['coordinates'][1]} for #{null_point_name}" + begin + res = connbus.exec("UPDATE timing_point SET lat = #{node['centroid']['coordinates'][0]*10000000}, lng = + #{node['centroid']['coordinates'][1]*10000000},guess = #{guess} WHERE name = '#{name}'") + puts "Put '#{null_point_name}' into DB" + rescue PGError => e + puts "Error inserting '#{null_point_name}' to DB #{e}" + ask_if("Continue?") + #conn.close() if conn + end + else + puts "Uhh, there was no suggestion ID like that. Try again next time!" + end + else + puts "Uhh, there were no geocoding results. Try again next time!" + end end end