Add Canadian geocoding, and attribute the geocoders we use
[rails.git] / app / controllers / geocoder_controller.rb
1 class GeocoderController < ApplicationController
2   layout 'site'
3
4   require 'net/http'
5   require 'rexml/document'
6
7   def search
8     res_hash = {}
9     @postcode_arr = []
10     @res_ary = []
11
12     if params[:query][:postcode] 
13       postcode = params[:query][:postcode].upcase
14       escaped_postcode = postcode.sub(/\s/,'%20')
15
16       if postcode.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/)
17         # Its a zip code - ask geocoder.us
18         # (They have a non commerical use api)
19         Net::HTTP.start('rpc.geocoder.us') do |http|
20           resp = http.get("/service/csv?zip=#{postcode}")
21           if resp.body.match(/couldn't find this zip/)
22               redirect_to "/index.html?error=invalid_zip_code"
23               return
24           end
25           data = resp.body.split(/, /) # lat,long,town,state,zip
26           lat = data[0] 
27           lon = data[1]
28           redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
29         end
30       elsif postcode.match(/^([A-Z]{1,2}\d+[A-Z]?\s*\d[A-Z]{2})/)
31         # It matched our naive UK postcode regexp
32         # Ask npemap to do a combined npemap + freethepostcode search
33         Net::HTTP.start('www.npemap.org.uk') do |http|
34           resp = http.get("/cgi/geocoder.fcgi?format=text&postcode=#{escaped_postcode}")
35           dataline = resp.body.split(/\n/)[1]
36           data = dataline.split(/,/) # easting,northing,postcode,lat,long
37           lat = data[3] 
38           lon = data[4]
39           redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
40         end
41       elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/)
42         # It's a canadian postcode
43         # Ask geocoder.ca (note - they have a per-day limit)
44         postcode = postcode.sub(/\s/,'')
45         Net::HTTP.start('geocoder.ca') do |http|
46           resp = http.get("?geoit=XML&postal=#{postcode}")
47           $stderr.print resp.body
48           $stderr.print resp.body.slice(/latt>.*?</)
49           data_lat = resp.body.slice(/latt>.*?</)
50           data_lon = resp.body.slice(/longt>.*?</)
51           lat = data_lat.split(/[<>]/)[1]
52           lon = data_lon.split(/[<>]/)[1]
53           redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
54         end
55       else
56         # Some other postcode / zip file
57         redirect_to "/index.html?error=unknown_postcode_or_zip"
58       end
59     else
60       if params[:query][:place_name] != nil or "" 
61         place_name = params[:query][:place_name]
62         Net::HTTP.start('ws.geonames.org') do |http|
63           res = http.get("/search?q=#{place_name}&maxRows=10")
64           xml = REXML::Document.new(res.body)
65           xml.elements.each("geonames/geoname") do |ele|
66             res_hash = {}
67             ele.elements.each("name"){ |n| res_hash['name'] = n.text }
68             ele.elements.each("countryCode"){ |n| res_hash['country_code'] = n.text }
69             ele.elements.each("countryNode"){ |n| res_hash['country_name'] = n.text }
70             ele.elements.each("lat"){ |n| res_hash['lat'] = n.text }
71             ele.elements.each("lng"){ |n| res_hash['lon']= n.text }
72             @res_ary << res_hash
73           end 
74         end
75       end
76       redirect_to :controller => 'geocoder', :action => 'results', :params => @res_ary
77     end
78   end
79
80   def result
81     @res = :params[@res_ary]
82   end
83 end