more geocoder stuff
This commit is contained in:
parent
738cf50d08
commit
3b4ee4cb0f
1 changed files with 55 additions and 26 deletions
|
@ -5,9 +5,7 @@ class GeocoderController < ApplicationController
|
||||||
require 'rexml/document'
|
require 'rexml/document'
|
||||||
|
|
||||||
def search
|
def search
|
||||||
res_hash = {}
|
|
||||||
@postcode_arr = []
|
@postcode_arr = []
|
||||||
@res_ary = []
|
|
||||||
|
|
||||||
if params[:query][:postcode]
|
if params[:query][:postcode]
|
||||||
postcode = params[:query][:postcode].upcase
|
postcode = params[:query][:postcode].upcase
|
||||||
|
@ -19,13 +17,14 @@ class GeocoderController < ApplicationController
|
||||||
Net::HTTP.start('rpc.geocoder.us') do |http|
|
Net::HTTP.start('rpc.geocoder.us') do |http|
|
||||||
resp = http.get("/service/csv?zip=#{postcode}")
|
resp = http.get("/service/csv?zip=#{postcode}")
|
||||||
if resp.body.match(/couldn't find this zip/)
|
if resp.body.match(/couldn't find this zip/)
|
||||||
redirect_to "/index.html?error=invalid_zip_code"
|
redirect_to "/index.html?error=invalid_zip_code"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
data = resp.body.split(/, /) # lat,long,town,state,zip
|
data = resp.body.split(/, /) # lat,long,town,state,zip
|
||||||
lat = data[0]
|
lat = data[0]
|
||||||
lon = data[1]
|
lon = data[1]
|
||||||
redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
|
redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
|
||||||
|
return
|
||||||
end
|
end
|
||||||
elsif postcode.match(/^([A-Z]{1,2}\d+[A-Z]?\s*\d[A-Z]{2})/)
|
elsif postcode.match(/^([A-Z]{1,2}\d+[A-Z]?\s*\d[A-Z]{2})/)
|
||||||
# It matched our naive UK postcode regexp
|
# It matched our naive UK postcode regexp
|
||||||
|
@ -37,6 +36,7 @@ class GeocoderController < ApplicationController
|
||||||
lat = data[3]
|
lat = data[3]
|
||||||
lon = data[4]
|
lon = data[4]
|
||||||
redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
|
redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
|
||||||
|
return
|
||||||
end
|
end
|
||||||
elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/)
|
elsif postcode.match(/^[A-Z]\d[A-Z]\s*\d[A-Z]\d/)
|
||||||
# It's a canadian postcode
|
# It's a canadian postcode
|
||||||
|
@ -49,7 +49,25 @@ class GeocoderController < ApplicationController
|
||||||
lat = data_lat.split(/[<>]/)[1]
|
lat = data_lat.split(/[<>]/)[1]
|
||||||
lon = data_lon.split(/[<>]/)[1]
|
lon = data_lon.split(/[<>]/)[1]
|
||||||
redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
|
redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14"
|
||||||
|
return
|
||||||
end
|
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})
|
||||||
|
/)
|
||||||
|
#its a UK postcode
|
||||||
|
begin
|
||||||
|
Net::HTTP.start('www.freethepostcode.org') do |http|
|
||||||
|
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"
|
||||||
|
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?error=unknown_postcode_or_zip"
|
||||||
else
|
else
|
||||||
# Some other postcode / zip code
|
# Some other postcode / zip code
|
||||||
# Throw it at geonames, and see if they have any luck with it
|
# Throw it at geonames, and see if they have any luck with it
|
||||||
|
@ -57,9 +75,9 @@ class GeocoderController < ApplicationController
|
||||||
resp = http.get("/postalCodeSearch?postalcode=#{escaped_postcode}&maxRows=1")
|
resp = http.get("/postalCodeSearch?postalcode=#{escaped_postcode}&maxRows=1")
|
||||||
hits = resp.body.slice(/totalResultsCount>.*?</).split(/[<>]/)[1]
|
hits = resp.body.slice(/totalResultsCount>.*?</).split(/[<>]/)[1]
|
||||||
if hits == "0"
|
if hits == "0"
|
||||||
# Geonames doesn't know, it's probably wrong
|
# Geonames doesn't know, it's probably wrong
|
||||||
redirect_to "/index.html?error=unknown_postcode_or_zip"
|
redirect_to "/index.html?error=unknown_postcode_or_zip"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
data_lat = resp.body.slice(/lat>.*?</)
|
data_lat = resp.body.slice(/lat>.*?</)
|
||||||
data_lon = resp.body.slice(/lng>.*?</)
|
data_lon = resp.body.slice(/lng>.*?</)
|
||||||
|
@ -69,27 +87,38 @@ class GeocoderController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if params[:query][:place_name] != nil or ""
|
# Some other postcode / zip file
|
||||||
place_name = params[:query][:place_name]
|
redirect_to "/index.html?error=unknown_postcode_or_zip"
|
||||||
Net::HTTP.start('ws.geonames.org') do |http|
|
return
|
||||||
res = http.get("/search?q=#{place_name}&maxRows=10")
|
|
||||||
xml = REXML::Document.new(res.body)
|
|
||||||
xml.elements.each("geonames/geoname") do |ele|
|
|
||||||
res_hash = {}
|
|
||||||
ele.elements.each("name"){ |n| res_hash['name'] = n.text }
|
|
||||||
ele.elements.each("countryCode"){ |n| res_hash['country_code'] = n.text }
|
|
||||||
ele.elements.each("countryNode"){ |n| res_hash['country_name'] = n.text }
|
|
||||||
ele.elements.each("lat"){ |n| res_hash['lat'] = n.text }
|
|
||||||
ele.elements.each("lng"){ |n| res_hash['lon']= n.text }
|
|
||||||
@res_ary << res_hash
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
redirect_to :controller => 'geocoder', :action => 'results', :params => @res_ary
|
|
||||||
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
|
end
|
||||||
|
|
||||||
def result
|
def results
|
||||||
@res = :params[@res_ary]
|
@place_name = params[:place_name]
|
||||||
|
res_hash = {}
|
||||||
|
@res_ary = []
|
||||||
|
begin
|
||||||
|
Net::HTTP.start('ws.geonames.org') do |http|
|
||||||
|
res = http.get("/search?q=#{@place_name}&maxRows=10")
|
||||||
|
xml = REXML::Document.new(res.body)
|
||||||
|
xml.elements.each("geonames/geoname") do |ele|
|
||||||
|
res_hash = {}
|
||||||
|
ele.elements.each("name"){ |n| res_hash['name'] = n.text }
|
||||||
|
ele.elements.each("countryCode"){ |n| res_hash['countrycode'] = n.text }
|
||||||
|
ele.elements.each("countryName"){ |n| res_hash['countryname'] = n.text }
|
||||||
|
ele.elements.each("lat"){ |n| res_hash['lat'] = n.text }
|
||||||
|
ele.elements.each("lng"){ |n| res_hash['lon']= n.text }
|
||||||
|
@res_ary << res_hash
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
#Flash a notice to say that geonames is broken
|
||||||
|
redirect_to "/index.html"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue