X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/3b4ee4cb0f3b04a48abd69ff10c3b1779b2bd736..8758685a86c3032a412183a53399e3e77dae75a1:/app/controllers/geocoder_controller.rb diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index c12c0525b..b352c315b 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -5,12 +5,34 @@ class GeocoderController < ApplicationController require 'rexml/document' def search - @postcode_arr = [] - if params[:query][:postcode] - postcode = params[:query][:postcode].upcase - escaped_postcode = postcode.sub(/\s/,'%20') + if params[:postcode] + unless params[:postcode].empty? + postcode = params[:postcode] + check_postcode(postcode) + return + end + end + if params[:query][:postcode] + unless params[:query][:postcode].empty? + postcode =params[:query][:postcode] + check_postcode(postcode) + return + end + 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 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) @@ -23,7 +45,7 @@ class GeocoderController < ApplicationController 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 "/index.html?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 +57,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 "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14" return end elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/) @@ -48,7 +70,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 "/index.html?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,16 +81,16 @@ 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 "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14" return end rescue redirect_to "/index.html" #redirect to somewhere else end - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" + redirect_to "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14" #redirect_to "/index.html?error=unknown_postcode_or_zip" - else + elsif # 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| @@ -83,19 +105,17 @@ class GeocoderController < ApplicationController data_lon = resp.body.slice(/lng>.*?]/)[1] lon = data_lon.split(/[<>]/)[1] - redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" + redirect_to "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14" end + else + # Some other postcode / zip file + redirect_to "/index.html?error=unknown_postcode_or_zip" + return end - else - # Some other postcode / zip file - redirect_to "/index.html?error=unknown_postcode_or_zip" - return + rescue + #Its likely that an api is down + redirect_to "/index.html?error=api_dpwn" 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