If we can't match a postcode using a known geocoder, throw it at geonames and see...
authorNick Burch <openstreetmap@gagravarr.org>
Tue, 24 Apr 2007 10:56:28 +0000 (10:56 +0000)
committerNick Burch <openstreetmap@gagravarr.org>
Tue, 24 Apr 2007 10:56:28 +0000 (10:56 +0000)
app/controllers/geocoder_controller.rb

index b04a71cdc361285512dc5e966a51149e5e19e663..8ef43ec6de66a4405758e7866fdcee849e8e9b59 100644 (file)
@@ -44,8 +44,6 @@ class GeocoderController < ApplicationController
         postcode = postcode.sub(/\s/,'')
         Net::HTTP.start('geocoder.ca') do |http|
           resp = http.get("?geoit=XML&postal=#{postcode}")
-          $stderr.print resp.body
-          $stderr.print resp.body.slice(/latt>.*?</)
           data_lat = resp.body.slice(/latt>.*?</)
           data_lon = resp.body.slice(/longt>.*?</)
           lat = data_lat.split(/[<>]/)[1]
@@ -53,8 +51,22 @@ class GeocoderController < ApplicationController
           redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
         end
       else
-        # Some other postcode / zip file
-        redirect_to "/index.html?error=unknown_postcode_or_zip"
+        # Some other postcode / zip code
+        # Throw it at geonames, and see if they have any luck with it
+        Net::HTTP.start('ws.geonames.org') do |http|
+          resp = http.get("/postalCodeSearch?postalcode=#{escaped_postcode}&maxRows=1")
+          hits = resp.body.slice(/totalResultsCount>.*?</).split(/[<>]/)[1]
+          if hits == "0"
+             # Geonames doesn't know, it's probably wrong
+             redirect_to "/index.html?error=unknown_postcode_or_zip"
+             return
+          end
+          data_lat = resp.body.slice(/lat>.*?</)
+          data_lon = resp.body.slice(/lng>.*?</)
+          lat = data_lat.split(/[<>]/)[1]
+          lon = data_lon.split(/[<>]/)[1]
+          redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
+        end
       end
     else
       if params[:query][:place_name] != nil or ""