X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/61ae5e7af0dd759ddb1acfcea10adcbede9da58c..77d2fcf9dd80a57e2748ff4a57fb1d603562b9e9:/app/controllers/geocoder_controller.rb?ds=sidebyside diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 6955579ed..76b157faa 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -5,25 +5,35 @@ class GeocoderController < ApplicationController require 'rexml/document' def search - @postcode_arr = [] + if params[:postcode] and not params[:postcode].empty? + check_postcode(params[:postcode]) + elsif params[:query][:postcode] and not params[:query][:postcode].empty? + check_postcode(params[:query][:postcode]) + elsif params[:query][:place_name] + redirect_to :controller => 'geocoder', :action => 'results', :params => {:place_name => params[:query][:place_name]} + end + end - unless params[:query][:postcode].empty? - postcode = params[:query][:postcode].upcase - escaped_postcode = postcode.sub(/\s/,'%20') + def check_postcode(p) + @postcode_arr = [] + postcode = p.upcase + escaped_postcode = postcode.sub(/\s/,'%20') + + begin if postcode.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/) # Its a zip code - ask geocoder.us # (They have a non commerical use api) Net::HTTP.start('rpc.geocoder.us') do |http| resp = http.get("/service/csv?zip=#{postcode}") if resp.body.match(/couldn't find this zip/) - redirect_to "/index.html?error=invalid_zip_code" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "invalid_zip_code" return end data = resp.body.split(/, /) # lat,long,town,state,zip lat = data[0] lon = data[1] - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14 return end elsif postcode.match(/^([A-Z]{1,2}\d+[A-Z]?\s*\d[A-Z]{2})/) @@ -35,7 +45,7 @@ class GeocoderController < ApplicationController data = dataline.split(/,/) # easting,northing,postcode,lat,long lat = data[3] lon = data[4] - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14 return end elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/) @@ -48,7 +58,7 @@ class GeocoderController < ApplicationController data_lon = resp.body.slice(/longt>.*?]/)[1] lon = data_lon.split(/[<>]/)[1] - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14 return end elsif postcode.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]) [0-9][ABD-HJLNP-UW-Z]{2}) @@ -59,15 +69,13 @@ class GeocoderController < ApplicationController resp = http.get("/geocode?postcode=#{postcode}") lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/) lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/) - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14 return end rescue - redirect_to "/index.html" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "invalid_postcode" #redirect to somewhere else end - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" - #redirect_to "/index.html?error=unknown_postcode_or_zip" elsif # Some other postcode / zip code # Throw it at geonames, and see if they have any luck with it @@ -76,26 +84,24 @@ class GeocoderController < ApplicationController 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" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "invalid_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" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14 end else # Some other postcode / zip file - redirect_to "/index.html?error=unknown_postcode_or_zip" + redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "invalid_postcode_or_zip" return end + rescue + #Its likely that an api is down + redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "api_down" end - - if params[:query][:place_name] - @place_name = params[:query][:place_name] - redirect_to :controller => 'geocoder', :action => 'results', :params => {:place_name => @place_name} - end end def results @@ -116,9 +122,12 @@ class GeocoderController < ApplicationController @res_ary << res_hash end end - rescue - #Flash a notice to say that geonames is broken - redirect_to "/index.html" + + flash.delete(:notice) + rescue Timeout::Error + flash[:notice] = "Timed out waiting for results from ws.geonames.org" + rescue Exception => ex + flash[:notice] = "Error contacting ws.geonames.org: #{ex.to_s}" end end end