X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/f6758877e80430060d445fd92d9a30970a5f2816..4c8e1fa545b9fd7196a8a6cd4b3cb1708e1c4dcc:/app/controllers/geocoder_controller.rb?ds=sidebyside diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 4c56ce9d7..22b1a8b3f 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -1,38 +1,65 @@ class GeocoderController < ApplicationController + layout 'site' require 'net/http' require 'rexml/document' def search - @res_hash = {} + res_hash = {} @postcode_arr = [] + @res_ary = [] + if params[:query][:postcode] + postcode = params[:query][:postcode] + escaped_postcode = postcode.sub(/\s/,'%20') - if params[:query][:place_name] != nil or "" - place_name = params[:query][:place_name] - 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| - ele.elements.each("name"){ |n| @res_hash['name'] = n.text } - ele.elements.each("countryCode"){ |n| @res_hash['country'] = n.text } - ele.elements.each("lat"){ |n| @res_hash['lat'] = n.text } - ele.elements.each("lng"){ |n| @res_hash['lon']= n.text } + if postcode.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/) + # Its a zip code - ask geocoder.us + # (They have a non commerical use api) + Net::HTTP.start('rpc.geocoder.us') do |http| + resp = http.get("/service/csv?zip=#{postcode}") + 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)/) + # 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| + resp = http.get("/cgi/geocoder.fcgi?format=text&postcode=#{escaped_postcode}") + dataline = resp.body.split(/\n/)[1] + data = dataline.split(/,/) # easting,northing,postcode,lat,long + lat = data[3] + lon = data[4] + redirect_to "/index.html?lat=#{lat}&lon=#{lon}&zoom=14" end - end - end - - if params[:query][:postcode] - postcode = params[:query][:postcode] - Net::HTTP.start('www.freethepostcode.org') do |http| - unless resp.code ~ /^2*/ - resp = http.get("/geocode?postcode=#{postcode}") - lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/) - lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/) - @postcode_array = [lat, lon] else + # Some other postcode / zip file + redirect_to "/index.html?error=unknown_postcode_or_zip" end - redirect_to "/index.html?lat=#{@postcode_array[0]}&lon=#{@postcode_array[1]}&zoom=14" + else + if params[:query][:place_name] != nil or "" + place_name = params[:query][:place_name] + 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['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 + + def result + @res = :params[@res_ary] + end end