]> git.openstreetmap.org Git - rails.git/commitdiff
Unify lat/lon formatting for json output
authormmd-osm <mmd.osm@gmail.com>
Thu, 2 Jan 2020 13:37:27 +0000 (14:37 +0100)
committermmd-osm <mmd.osm@gmail.com>
Thu, 2 Jan 2020 13:52:53 +0000 (14:52 +0100)
app/models/concerns/geo_record.rb
app/views/api/map/_bounds.json.jbuilder
app/views/api/nodes/_node.json.jbuilder
app/views/api/old_nodes/_old_node.json.jbuilder
test/controllers/api/map_controller_test.rb

index 91533ece438f89fd9cf8440bfd5400d84ad9addf..447ee19df61050e83f229619db8968351498a62a 100644 (file)
@@ -12,6 +12,10 @@ module GeoRecord
     def to_s
       format("%.7f", self)
     end
+
+    def as_json(_)
+      format("%.7f", self).to_f
+    end
   end
 
   # This scaling factor is used to convert between the float lat/lon that is
index 98755900efcbf978f3b05eb2ff8dd76768ac136a..16fdbeb0f3d8051053b8d56e46d77769c8b1f49a 100644 (file)
@@ -1,6 +1,6 @@
 json.bounds do
-  json.minlat format("%.7f", @bounds.min_lat)
-  json.minlon format("%.7f", @bounds.min_lon)
-  json.maxlat format("%.7f", @bounds.max_lat)
-  json.maxlon format("%.7f", @bounds.max_lon)
+  json.minlat GeoRecord::Coord.new(@bounds.min_lat)
+  json.minlon GeoRecord::Coord.new(@bounds.min_lon)
+  json.maxlat GeoRecord::Coord.new(@bounds.max_lat)
+  json.maxlon GeoRecord::Coord.new(@bounds.max_lon)
 end
index 12cf2fb95a9c9fb124441866cd4125725f83997c..e48d5f17c2e38bfb2151d6aa38ca13b61e8bc873 100644 (file)
@@ -1,8 +1,8 @@
 json.type "node"
 json.id node.id
 if node.visible
-  json.lat format("%.7f", node.lat.to_f)
-  json.lon format("%.7f", node.lon.to_f)
+  json.lat GeoRecord::Coord.new(node.lat)
+  json.lon GeoRecord::Coord.new(node.lon)
 end
 json.timestamp node.timestamp.xmlschema
 json.version node.version
index 5042c84a73dfce39ef769856557d0622e89ef1e9..211d50332324242bffc5f65dc69318dc02b43784 100644 (file)
@@ -1,8 +1,8 @@
 json.type "node"
 json.id old_node.node_id
 if old_node.visible
-  json.lat format("%.7f", old_node.lat.to_f)
-  json.lon format("%.7f", old_node.lon.to_f)
+  json.lat GeoRecord::Coord.new(old_node.lat)
+  json.lon GeoRecord::Coord.new(old_node.lon)
 end
 json.timestamp old_node.timestamp.xmlschema
 json.version old_node.version
index 5df2dc6a3cabb30c1e58d13e6463a933566566ca..54461868b7934eff2d22813dc93f20e7673c48fb 100644 (file)
@@ -92,15 +92,15 @@ module Api
 
       assert_equal Settings.api_version, js["version"]
       assert_equal Settings.generator, js["generator"]
-      assert_equal "#{format('%.7f', minlon)}", js["bounds"]["minlon"]
-      assert_equal "#{format('%.7f', minlat)}", js["bounds"]["minlat"]
-      assert_equal "#{format('%.7f', maxlon)}", js["bounds"]["maxlon"]
-      assert_equal "#{format('%.7f', maxlat)}", js["bounds"]["maxlat"]
+      assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
+      assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
+      assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
+      assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
 
       result_nodes = js["elements"].select { |a| a["type"] == "node" }
                                    .select { |a| a["id"] == node.id }
-                                   .select { |a| a["lat"] == "#{format('%.7f', node.lat)}" }
-                                   .select { |a| a["lon"] == "#{format('%.7f', node.lon)}" }
+                                   .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
+                                   .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
                                    .select { |a| a["version"] == node.version }
                                    .select { |a| a["changeset"] == node.changeset_id }
                                    .select { |a| a["timestamp"] == node.timestamp.xmlschema }