1 class GeocoderController < ApplicationController
4 require 'rexml/document'
10 if query.match(/^\d{5}(-\d{4})?$/)
11 results.push search_us_postcode(query)
12 elsif query.match(/(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s*[0-9][ABD-HJLNP-UW-Z]{2})/i)
13 results.push search_uk_postcode(query)
14 elsif query.match(/[A-Z]\d[A-Z]\s*\d[A-Z]\d/i)
15 results.push search_ca_postcode(query)
17 results.push search_osm_namefinder(query)
18 results.push search_geonames(query)
21 results_count = count_results(results)
23 render :update do |page|
24 page.replace_html :sidebar_content, :partial => 'results', :object => results
27 position = results.collect { |s| s[:results] }.compact.flatten[0]
28 page.call "setPosition", position[:lat], position[:lon], position[:zoom]
30 page.call "openSidebar"
41 results.push description_osm_namefinder("cities", lat, lon, 2)
42 results.push description_osm_namefinder("towns", lat, lon, 4)
43 results.push description_osm_namefinder("places", lat, lon, 10)
44 results.push description_geonames(lat, lon)
46 render :update do |page|
47 page.replace_html :sidebar_content, :partial => 'results', :object => results
48 page.call "openSidebar"
54 def search_us_postcode(query)
57 # ask geocoder.us (they have a non-commercial use api)
58 response = fetch_text("http://rpc.geocoder.us/service/csv?zip=#{URI.escape(query)}")
61 unless response.match(/couldn't find this zip/)
62 data = response.split(/\s*,\s+/) # lat,long,town,state,zip
63 results.push({:lat => data[0], :lon => data[1], :zoom => 12,
64 :prefix => "#{data[2]}, #{data[3]}, ",
68 return { :source => "Geocoder.us", :url => "http://geocoder.us/", :results => results }
69 rescue Exception => ex
70 return { :source => "Geocoder.us", :url => "http://geocoder.us/", :error => "Error contacting rpc.geocoder.us: #{ex.to_s}" }
73 def search_uk_postcode(query)
76 # ask npemap.org.uk to do a combined npemap + freethepostcode search
77 response = fetch_text("http://www.npemap.org.uk/cgi/geocoder.fcgi?format=text&postcode=#{URI.escape(query)}")
80 unless response.match(/Error/)
81 dataline = response.split(/\n/)[1]
82 data = dataline.split(/,/) # easting,northing,postcode,lat,long
83 results.push({:lat => data[3], :lon => data[4], :zoom => 12,
84 :name => data[2].gsub(/'/, "")})
87 return { :source => "NPEMap / FreeThePostcode", :url => "http://www.npemap.org.uk/", :results => results }
88 rescue Exception => ex
89 return { :source => "NPEMap / FreeThePostcode", :url => "http://www.npemap.org.uk/", :error => "Error contacting www.npemap.org.uk: #{ex.to_s}" }
92 def search_ca_postcode(query)
95 # ask geocoder.ca (note - they have a per-day limit)
96 response = fetch_xml("http://geocoder.ca/?geoit=XML&postal=#{URI.escape(query)}")
99 unless response.get_elements("geodata/error")
100 results.push({:lat => response.get_text("geodata/latt").to_s,
101 :lon => response.get_text("geodata/longt").to_s,
103 :name => query.upcase})
106 return { :source => "Geocoder.CA", :url => "http://geocoder.ca/", :results => results }
107 rescue Exception => ex
108 return { :source => "Geocoder.CA", :url => "http://geocoder.ca/", :error => "Error contacting geocoder.ca: #{ex.to_s}" }
111 def search_osm_namefinder(query)
115 response = fetch_xml("http://www.frankieandshadow.com/osm/search.xml?find=#{URI.escape(query)}")
118 response.elements.each("searchresults/named") do |named|
119 lat = named.attributes["lat"].to_s
120 lon = named.attributes["lon"].to_s
121 zoom = named.attributes["zoom"].to_s
122 place = named.elements["place/named"] || named.elements["nearestplaces/named"]
123 type = named.attributes["info"].to_s.capitalize
124 name = named.attributes["name"].to_s
125 description = named.elements["description"].to_s
133 distance = format_distance(place.attributes["approxdistance"].to_i)
134 direction = format_direction(place.attributes["direction"].to_i)
135 placename = place.attributes["name"].to_s
136 suffix = ", #{distance} #{direction} of #{placename}"
140 results.push({:lat => lat, :lon => lon, :zoom => zoom,
141 :prefix => prefix, :name => name, :suffix => suffix,
142 :description => description})
145 return { :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :results => results }
146 rescue Exception => ex
147 return { :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :error => "Error contacting www.frankieandshadow.com: #{ex.to_s}" }
150 def search_geonames(query)
154 response = fetch_xml("http://ws.geonames.org/search?q=#{URI.escape(query)}&maxRows=20")
157 response.elements.each("geonames/geoname") do |geoname|
158 lat = geoname.get_text("lat").to_s
159 lon = geoname.get_text("lng").to_s
160 name = geoname.get_text("name").to_s
161 country = geoname.get_text("countryName").to_s
162 results.push({:lat => lat, :lon => lon, :zoom => 12,
164 :suffix => ", #{country}"})
167 return { :source => "GeoNames", :url => "http://www.geonames.org/", :results => results }
168 rescue Exception => ex
169 return { :source => "GeoNames", :url => "http://www.geonames.org/", :error => "Error contacting ws.geonames.org: #{ex.to_s}" }
172 def description_osm_namefinder(types, lat, lon, max)
176 response = fetch_xml("http://www.frankieandshadow.com/osm/search.xml?find=#{types}+near+#{lat},#{lon}&max=#{max}")
179 response.elements.each("searchresults/named") do |named|
180 lat = named.attributes["lat"].to_s
181 lon = named.attributes["lon"].to_s
182 zoom = named.attributes["zoom"].to_s
183 place = named.elements["place/named"] || named.elements["nearestplaces/named"]
184 type = named.attributes["info"].to_s
185 name = named.attributes["name"].to_s
186 description = named.elements["description"].to_s
187 distance = format_distance(place.attributes["approxdistance"].to_i)
188 direction = format_direction((place.attributes["direction"].to_i - 180) % 360)
189 prefix = "#{distance} #{direction} of #{type} "
190 results.push({:lat => lat, :lon => lon, :zoom => zoom,
191 :prefix => prefix.capitalize, :name => name,
192 :description => description})
195 return { :type => types.capitalize, :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :results => results }
196 rescue Exception => ex
197 return { :type => types.capitalize, :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :error => "Error contacting www.frankieandshadow.com: #{ex.to_s}" }
200 def description_geonames(lat, lon)
204 response = fetch_xml("http://ws.geonames.org/countrySubdivision?lat=#{lat}&lng=#{lon}")
207 response.elements.each("geonames/countrySubdivision") do |geoname|
208 name = geoname.get_text("adminName1").to_s
209 country = geoname.get_text("countryName").to_s
210 results.push({:prefix => "#{name}, #{country}"})
213 return { :type => "Location", :source => "GeoNames", :url => "http://www.geonames.org/", :results => results }
214 rescue Exception => ex
215 return { :type => types.capitalize, :source => "OpenStreetMap Namefinder", :url => "http://www.frankieandshadow.com/osm/", :error => "Error contacting www.frankieandshadow.com: #{ex.to_s}" }
219 return Net::HTTP.get(URI.parse(url))
223 return REXML::Document.new(fetch_text(url))
226 def format_distance(distance)
227 return "less than 1km" if distance == 0
228 return "about #{distance}km"
231 def format_direction(bearing)
232 return "south-west" if bearing >= 22.5 and bearing < 67.5
233 return "south" if bearing >= 67.5 and bearing < 112.5
234 return "south-east" if bearing >= 112.5 and bearing < 157.5
235 return "east" if bearing >= 157.5 and bearing < 202.5
236 return "north-east" if bearing >= 202.5 and bearing < 247.5
237 return "north" if bearing >= 247.5 and bearing < 292.5
238 return "north-west" if bearing >= 292.5 and bearing < 337.5
242 def count_results(results)
245 results.each do |source|
246 count += source[:results].length if source[:results]