]> git.openstreetmap.org Git - rails.git/commitdiff
Add Canadian geocoding, and attribute the geocoders we use
authorNick Burch <openstreetmap@gagravarr.org>
Tue, 24 Apr 2007 10:32:34 +0000 (10:32 +0000)
committerNick Burch <openstreetmap@gagravarr.org>
Tue, 24 Apr 2007 10:32:34 +0000 (10:32 +0000)
app/controllers/geocoder_controller.rb
app/views/site/index.rhtml
public/stylesheets/site.css

index 22b1a8b3ff11759aed811fbddea85e6c8deb68e5..b04a71cdc361285512dc5e966a51149e5e19e663 100644 (file)
@@ -10,7 +10,7 @@ class GeocoderController < ApplicationController
     @res_ary = []
 
     if params[:query][:postcode] 
-      postcode = params[:query][:postcode]
+      postcode = params[:query][:postcode].upcase
       escaped_postcode = postcode.sub(/\s/,'%20')
 
       if postcode.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/)
@@ -18,12 +18,16 @@ class GeocoderController < ApplicationController
         # (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"
+              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"
         end
-      elsif postcode.match(/^(\w{1,2}\d+\w?\s*\d\w\w)/)
+      elsif postcode.match(/^([A-Z]{1,2}\d+[A-Z]?\s*\d[A-Z]{2})/)
         # It matched our naive UK postcode regexp
         # Ask npemap to do a combined npemap + freethepostcode search
         Net::HTTP.start('www.npemap.org.uk') do |http|
@@ -34,6 +38,20 @@ class GeocoderController < ApplicationController
           lon = data[4]
           redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
         end
+      elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/)
+        # It's a canadian postcode
+        # Ask geocoder.ca (note - they have a per-day limit)
+        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]
+          lon = data_lon.split(/[<>]/)[1]
+          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"
index a066677ea666ee26203565d1c29e5ba6f244ea9c..31b0a480e07d96015d1603511adf3ab24d49456b 100644 (file)
@@ -6,8 +6,14 @@
   <%= text_field 'query', 'place_name'%>
   <%= submit_tag 'Search' %>
   <%= end_form_tag %>
+
+<div id="geocoder-attribution">
+  Geolocation provided by <a href="http://npemap.org.uk/">npemap.org.uk</a>,
+  <a href="http://geocoder.us/">geocoder.us</a>, <a href="http://geocoder.ca/">geocoder.ca</a>
+  and <a href="http://www.geonames.org/">geonames.org</a>
+</div>
 <div id="postcode-helper">
-eg: SW15 6JH
+eg: SW15 6JH, 95472 or H2L4C1
 </div>
 <div id="placename-helper">
 eg:Essen
index 1a4b8df4c996429228e874c46c9abe946f9e1dda..d1834fbced74e7e2aeafe1512350d1fb8e7ab633 100644 (file)
@@ -289,17 +289,26 @@ hides rule from IE5-Mac \*/
   font-size: 12px;\r
   padding-top: 5px;\r
   padding-left: 14px;\r
+  padding-bottom: 15px;\r
+  width: 700px;\r
 }\r
 \r
 #placename-helper{\r
   font-size: 9px;\r
-  position:relative; bottom:36px; left: 470px;\r
+  position:absolute; top:20px; left: 250px;\r
   color: gray;\r
 }\r
 \r
 #postcode-helper{\r
   font-size: 9px;\r
-  position:relative; bottom:17px; left: 180px;\r
+  position:absolute; top:20px; left: 20px;\r
+  color: gray;\r
+}\r
+\r
+#geocoder-attribution{\r
+  font-size: 9px;\r
+  position:absolute; top:26px; left: 450px;\r
+  line-height: 8px;\r
   color: gray;\r
 }\r
 \r