]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/geocoder_controller.rb
Use SQL schemas, as ruby can't cope with the things we do...
[rails.git] / app / controllers / geocoder_controller.rb
index b352c315bad20f4c7441957e009467ffa7da023c..4ce31baf4da099dd1d1e61cbbb502efa5ec37e51 100644 (file)
@@ -1,28 +1,17 @@
 class GeocoderController < ApplicationController
   layout 'site'
 
 class GeocoderController < ApplicationController
   layout 'site'
 
+  require 'uri'
   require 'net/http'
   require 'rexml/document'
 
   def search
   require 'net/http'
   require 'rexml/document'
 
   def search
-
-    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}
+    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
 
     end 
   end
 
@@ -39,13 +28,13 @@ class GeocoderController < ApplicationController
         Net::HTTP.start('rpc.geocoder.us') do |http|
           resp = http.get("/service/csv?zip=#{postcode}")
           if resp.body.match(/couldn't find this zip/)
         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]
             return
           end
           data = resp.body.split(/, /) # lat,long,town,state,zip
           lat = data[0] 
           lon = data[1]
-          redirect_to "/index.html?mlat=#{lat}&mlon=#{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})/)
           return
         end
       elsif postcode.match(/^([A-Z]{1,2}\d+[A-Z]?\s*\d[A-Z]{2})/)
@@ -57,7 +46,7 @@ class GeocoderController < ApplicationController
           data = dataline.split(/,/) # easting,northing,postcode,lat,long
           lat = data[3] 
           lon = data[4]
           data = dataline.split(/,/) # easting,northing,postcode,lat,long
           lat = data[3] 
           lon = data[4]
-          redirect_to "/index.html?mlat=#{lat}&mlon=#{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/)
           return
         end
       elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/)
@@ -70,7 +59,7 @@ class GeocoderController < ApplicationController
           data_lon = resp.body.slice(/longt>.*?</)
           lat = data_lat.split(/[<>]/)[1]
           lon = data_lon.split(/[<>]/)[1]
           data_lon = resp.body.slice(/longt>.*?</)
           lat = data_lat.split(/[<>]/)[1]
           lon = data_lon.split(/[<>]/)[1]
-          redirect_to "/index.html?mlat=#{lat}&mlon=#{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})
           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})
@@ -81,15 +70,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]+/)
             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?mlat=#{lat}&mlon=#{lon}&zoom=14"
+            redirect_to :controller => params[:next_controller], :action => params[:next_action], :mlat => lat, :mlon => lon, :zoom => 14
             return
           end
         rescue
             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 somewhere else
         end
-        redirect_to "/index.html?mlat=#{lat}&mlon=#{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
       elsif
         # Some other postcode / zip code
         # Throw it at geonames, and see if they have any luck with it
@@ -98,23 +85,23 @@ class GeocoderController < ApplicationController
           hits = resp.body.slice(/totalResultsCount>.*?</).split(/[<>]/)[1]
           if hits == "0"
             # Geonames doesn't know, it's probably wrong
           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"
+            redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "invalid_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]
             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?mlat=#{lat}&mlon=#{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
         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
         return
       end
     rescue
       #Its likely that an api is down
-      redirect_to "/index.html?error=api_dpwn"
+      redirect_to :controller => params[:next_controller], :action => params[:next_action], :error => "api_down"
     end
   end
 
     end
   end
 
@@ -124,7 +111,7 @@ class GeocoderController < ApplicationController
     @res_ary = []
     begin
       Net::HTTP.start('ws.geonames.org') do |http|
     @res_ary = []
     begin
       Net::HTTP.start('ws.geonames.org') do |http|
-        res = http.get("/search?q=#{@place_name}&maxRows=10")
+        res = http.get("/search?q=#{URI.escape(@place_name)}&maxRows=10")
         xml = REXML::Document.new(res.body)
         xml.elements.each("geonames/geoname") do |ele|
           res_hash = {}
         xml = REXML::Document.new(res.body)
         xml.elements.each("geonames/geoname") do |ele|
           res_hash = {}
@@ -136,9 +123,12 @@ class GeocoderController < ApplicationController
           @res_ary << res_hash
         end 
       end
           @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
     end
   end
 end