Use decoded strings when parsing XML responses from geocoders
authorTom Hughes <tom@compton.nu>
Wed, 29 Nov 2017 23:07:19 +0000 (23:07 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 29 Nov 2017 23:07:19 +0000 (23:07 +0000)
Fixes #1690

app/controllers/geocoder_controller.rb
test/controllers/geocoder_controller_test.rb
test/http/nominatim.yml

index 6110bae..5d7ba91 100644 (file)
@@ -91,8 +91,8 @@ class GeocoderController < ApplicationController
 
     # parse the response
     if response.get_elements("geodata/error").empty?
-      @results.push(:lat => response.get_text("geodata/latt").to_s,
-                    :lon => response.get_text("geodata/longt").to_s,
+      @results.push(:lat => response.text("geodata/latt"),
+                    :lon => response.text("geodata/longt"),
                     :zoom => POSTCODE_ZOOM,
                     :name => query.upcase)
     end
@@ -138,12 +138,12 @@ class GeocoderController < ApplicationController
 
     # parse the response
     results.elements.each("place") do |place|
-      lat = place.attributes["lat"].to_s
-      lon = place.attributes["lon"].to_s
-      klass = place.attributes["class"].to_s
-      type = place.attributes["type"].to_s
-      name = place.attributes["display_name"].to_s
-      min_lat, max_lat, min_lon, max_lon = place.attributes["boundingbox"].to_s.split(",")
+      lat = place.attributes["lat"]
+      lon = place.attributes["lon"]
+      klass = place.attributes["class"]
+      type = place.attributes["type"]
+      name = place.attributes["display_name"]
+      min_lat, max_lat, min_lon, max_lon = place.attributes["boundingbox"].split(",")
       prefix_name = if type.empty?
                       ""
                     else
@@ -190,10 +190,11 @@ class GeocoderController < ApplicationController
 
     # parse the response
     response.elements.each("geonames/geoname") do |geoname|
-      lat = geoname.get_text("lat").to_s
-      lon = geoname.get_text("lng").to_s
-      name = geoname.get_text("name").to_s
-      country = geoname.get_text("countryName").to_s
+      lat = geoname.text("lat")
+      lon = geoname.text("lng")
+      name = geoname.text("name")
+      country = geoname.text("countryName")
+
       @results.push(:lat => lat, :lon => lon,
                     :zoom => GEONAMES_ZOOM,
                     :name => name,
@@ -220,11 +221,11 @@ class GeocoderController < ApplicationController
 
     # parse the response
     response.elements.each("reversegeocode/result") do |result|
-      lat = result.attributes["lat"].to_s
-      lon = result.attributes["lon"].to_s
+      lat = result.attributes["lat"]
+      lon = result.attributes["lon"]
       object_type = result.attributes["osm_type"]
       object_id = result.attributes["osm_id"]
-      description = result.get_text.to_s
+      description = result.text
 
       @results.push(:lat => lat, :lon => lon,
                     :zoom => zoom,
@@ -254,8 +255,9 @@ class GeocoderController < ApplicationController
 
     # parse the response
     response.elements.each("geonames/countrySubdivision") do |geoname|
-      name = geoname.get_text("adminName1").to_s
-      country = geoname.get_text("countryName").to_s
+      name = geoname.text("adminName1")
+      country = geoname.text("countryName")
+
       @results.push(:lat => lat, :lon => lon,
                     :zoom => GEONAMES_ZOOM,
                     :name => name,
index ee8991d..0d062cf 100644 (file)
@@ -1,4 +1,3 @@
-
 require "test_helper"
 require "geocoder_controller"
 
@@ -425,6 +424,12 @@ class GeocoderControllerTest < ActionController::TestCase
       results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
                     :lat => 51.7634883, :lon => -0.0088373,
                     :type => "way", :id => 3489841, :zoom => 17
+
+      get :search_osm_nominatim_reverse, :xhr => true,
+                                         :params => { :lat => 13.7709, :lon => 100.50507, :zoom => 19 }
+      results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
+                    :lat => 13.7708691, :lon => 100.505073233221,
+                    :type => "way", :id => 542901374, :zoom => 19
     end
   end
 
index accaebf..35070fa 100644 (file)
         <country_code>gb</country_code>
       </addressparts>
     </reversegeocode>
+
+/reverse?accept-language=&lat=13.7709&lon=100.50507&zoom=19:
+  code: 200
+  body: |
+    <?xml version="1.0" encoding="UTF-8" ?>
+    <reversegeocode timestamp='Wed, 29 Nov 17 22:48:30 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='accept-language=&amp;lat=13.7709&amp;lon=100.50507&amp;zoom=19'>
+      <result place_id="220797400" osm_type="way" osm_id="542901374" ref="MM Steak&amp;Grill" lat="13.7708691" lon="100.505073233221" boundingbox="13.7708162,13.7709465,100.5049978,100.5051344">MM Steak&amp;Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย</result>
+      <addressparts>
+        <restaurant>MM Steak&amp;Grill</restaurant>
+        <road>ถนนศรีอยุธยา</road>
+        <neighbourhood>บางขุนพรหม</neighbourhood>
+        <city>กรุงเทพมหานคร</city>
+        <suburb>เขตดุสิต</suburb>
+        <state>กรุงเทพมหานคร</state>
+        <postcode>10300</postcode>
+        <country>ประเทศไทย</country>
+        <country_code>th</country_code>
+      </addressparts>
+    </reversegeocode>