if @params[:query] =~ /^\d{5}(-\d{4})?$/
@sources.push "osm_nominatim"
elsif @params[:query] =~ /^(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])\s*[0-9][ABD-HJLNP-UW-Z]{2})$/i
- @sources.push "uk_postcode"
@sources.push "osm_nominatim"
elsif @params[:query] =~ /^[A-Z]\d[A-Z]\s*\d[A-Z]\d$/i
@sources.push "ca_postcode"
end
end
- def search_uk_postcode
- # get query parameters
- query = params[:query]
-
- # create result array
- @results = []
-
- # ask npemap.org.uk to do a combined npemap + freethepostcode search
- response = fetch_text("http://www.npemap.org.uk/cgi/geocoder.fcgi?format=text&postcode=#{escape_query(query)}")
-
- # parse the response
- unless response =~ /Error/
- dataline = response.split(/\n/)[1]
- data = dataline.split(/,/) # easting,northing,postcode,lat,long
- postcode = data[2].delete("'")
- zoom = POSTCODE_ZOOM - postcode.count("#")
- @results.push(:lat => data[3], :lon => data[4], :zoom => zoom,
- :name => postcode)
- end
-
- render :action => "results"
- rescue StandardError => ex
- @error = "Error contacting www.npemap.org.uk: #{ex}"
- render :action => "error"
- end
-
def search_ca_postcode
# get query parameters
query = params[:query]
search:
title:
latlon: 'Results from <a href="https://openstreetmap.org/">Internal</a>'
- uk_postcode: 'Results from <a href="http://www.npemap.org.uk/">NPEMap / FreeThe Postcode</a>'
ca_postcode: 'Results from <a href="https://geocoder.ca/">Geocoder.CA</a>'
osm_nominatim: 'Results from <a href="https://nominatim.openstreetmap.org/">OpenStreetMap Nominatim</a>'
geonames: 'Results from <a href="http://www.geonames.org/">GeoNames</a>'
# geocoder
match "/search" => "geocoder#search", :via => :get, :as => :search
match "/geocoder/search_latlon" => "geocoder#search_latlon", :via => :get
- match "/geocoder/search_uk_postcode" => "geocoder#search_uk_postcode", :via => :get
match "/geocoder/search_ca_postcode" => "geocoder#search_ca_postcode", :via => :get
match "/geocoder/search_osm_nominatim" => "geocoder#search_osm_nominatim", :via => :get
match "/geocoder/search_geonames" => "geocoder#search_geonames", :via => :get
{ :path => "/geocoder/search_latlon", :method => :get },
{ :controller => "geocoder", :action => "search_latlon" }
)
- assert_routing(
- { :path => "/geocoder/search_uk_postcode", :method => :get },
- { :controller => "geocoder", :action => "search_uk_postcode" }
- )
assert_routing(
{ :path => "/geocoder/search_ca_postcode", :method => :get },
{ :controller => "geocoder", :action => "search_ca_postcode" }
"CR2 6XH",
"DN55 1PT"
].each do |code|
- search_check code, %w[uk_postcode osm_nominatim]
+ search_check code, %w[osm_nominatim]
end
end
results_check_error "Longitude 180.23 out of range"
end
- ##
- # Test the UK postcode search
- def test_search_uk_postcode
- with_http_stubs "npemap" do
- get :search_uk_postcode, :xhr => true,
- :params => { :query => "CV4 7AL", :zoom => 10,
- :minlon => -0.559, :minlat => 51.217,
- :maxlon => 0.836, :maxlat => 51.766 }
- results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
-
- get :search_uk_postcode, :xhr => true,
- :params => { :query => "XX9 9XX", :zoom => 10,
- :minlon => -0.559, :minlat => 51.217,
- :maxlon => 0.836, :maxlat => 51.766 }
- results_check
- end
- end
-
##
# Test the Canadian postcode search
def test_search_ca_postcode
+++ /dev/null
-/cgi/geocoder.fcgi?format=text&postcode=CV4%207AL:
- code: 200
- body: |
- # Easting,Northing,Matched Postcode,Latitude,Longitude
- 429926,276058,'CV4 7AL',52.381748701968,-1.56176420939232
-
-/cgi/geocoder.fcgi?format=text&postcode=XX9%209XX:
- code: 200
- body: |
- Error: Postcode area 'XX' not found, postcode probably invalid