From: Nick Burch Date: Tue, 24 Apr 2007 10:56:28 +0000 (+0000) Subject: If we can't match a postcode using a known geocoder, throw it at geonames and see... X-Git-Tag: live~9109 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/738cf50d088a3adb556e72e61c1a9ed5f87daa12 If we can't match a postcode using a known geocoder, throw it at geonames and see if they happen to know what to do with it --- diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index b04a71cdc..8ef43ec6d 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -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>.*?.*?.*?]/)[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>.*?]/)[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>.*?.*?]/)[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 ""