]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/5585'
authorTom Hughes <tom@compton.nu>
Sun, 2 Feb 2025 10:16:43 +0000 (10:16 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 2 Feb 2025 10:16:43 +0000 (10:16 +0000)
app/assets/javascripts/index/contextmenu.js
app/assets/javascripts/index/directions-endpoint.js
app/assets/javascripts/index/directions.js
app/assets/javascripts/index/export.js
app/assets/javascripts/index/query.js
app/assets/javascripts/index/search.js
app/assets/javascripts/leaflet.map.js
app/assets/javascripts/osm.js.erb
app/assets/javascripts/user.js
app/controllers/api/relations_controller.rb
app/controllers/api/ways_controller.rb

index ea284f29b97d87f0ee2508697b6afb1228c8a5b2..51ec87723b0a18ecc871a0bdd5d5a8993c6a7562 100644 (file)
@@ -4,13 +4,10 @@ OSM.initializeContextMenu = function (map) {
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.directions_from"),
     callback: function directionsFromHere(e) {
-      var precision = OSM.zoomPrecision(map.getZoom()),
-          latlng = e.latlng.wrap(),
-          lat = latlng.lat.toFixed(precision),
-          lng = latlng.lng.toFixed(precision);
+      const latlng = OSM.cropLocation(e.latlng, map.getZoom());
 
       OSM.router.route("/directions?" + Qs.stringify({
-        from: lat + "," + lng,
+        from: latlng.join(","),
         to: getDirectionsEndpointCoordinatesFromInput($("#route_to"))
       }));
     }
@@ -19,14 +16,11 @@ OSM.initializeContextMenu = function (map) {
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.directions_to"),
     callback: function directionsToHere(e) {
-      var precision = OSM.zoomPrecision(map.getZoom()),
-          latlng = e.latlng.wrap(),
-          lat = latlng.lat.toFixed(precision),
-          lng = latlng.lng.toFixed(precision);
+      const latlng = OSM.cropLocation(e.latlng, map.getZoom());
 
       OSM.router.route("/directions?" + Qs.stringify({
         from: getDirectionsEndpointCoordinatesFromInput($("#route_from")),
-        to: lat + "," + lng
+        to: latlng.join(",")
       }));
     }
   });
@@ -34,36 +28,27 @@ OSM.initializeContextMenu = function (map) {
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.add_note"),
     callback: function addNoteHere(e) {
-      var precision = OSM.zoomPrecision(map.getZoom()),
-          latlng = e.latlng.wrap(),
-          lat = latlng.lat.toFixed(precision),
-          lng = latlng.lng.toFixed(precision);
+      const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
-      OSM.router.route("/note/new?lat=" + lat + "&lon=" + lng);
+      OSM.router.route("/note/new?" + Qs.stringify({ lat, lon }));
     }
   });
 
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.show_address"),
     callback: function describeLocation(e) {
-      var precision = OSM.zoomPrecision(map.getZoom()),
-          latlng = e.latlng.wrap(),
-          lat = latlng.lat.toFixed(precision),
-          lng = latlng.lng.toFixed(precision);
+      const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom()).map(encodeURIComponent);
 
-      OSM.router.route("/search?lat=" + encodeURIComponent(lat) + "&lon=" + encodeURIComponent(lng));
+      OSM.router.route("/search?" + Qs.stringify({ lat, lon }));
     }
   });
 
   map.contextmenu.addItem({
     text: I18n.t("javascripts.context.query_features"),
     callback: function queryFeatures(e) {
-      var precision = OSM.zoomPrecision(map.getZoom()),
-          latlng = e.latlng.wrap(),
-          lat = latlng.lat.toFixed(precision),
-          lng = latlng.lng.toFixed(precision);
+      const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
-      OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
+      OSM.router.route("/query?" + Qs.stringify({ lat, lon }));
     }
   });
 
index 7fe4b4ea7f4803053f816d2f9d7986817e798aab..2cc3ea66256488c3bbd426d31f175e3836b3c3dd 100644 (file)
@@ -34,7 +34,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   };
 
   function markerDragListener(e) {
-    var latlng = convertLatLngToZoomPrecision(e.target.getLatLng());
+    const latlng = L.latLng(OSM.cropLocation(e.target.getLatLng(), map.getZoom()));
 
     setLatLng(latlng);
     setInputValueFromLatLng(latlng);
@@ -109,11 +109,5 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     input.val(latlng.lat + ", " + latlng.lng);
   }
 
-  function convertLatLngToZoomPrecision(latlng) {
-    var precision = OSM.zoomPrecision(map.getZoom());
-
-    return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
-  }
-
   return endpoint;
 };
index 391c1f9315c7fe945d822d49380f9578729e79c0..4eb0e09f46c13bbfb83edb59ae773f65d18d601c 100644 (file)
@@ -116,18 +116,14 @@ OSM.Directions = function (map) {
     // Cancel any route that is already in progress
     if (routeRequest) routeRequest.abort();
 
-    var o = endpoints[0].latlng,
-        d = endpoints[1].latlng;
+    const points = endpoints.map(p => p.latlng);
 
-    if (!o || !d) return;
+    if (!points[0] || !points[1]) return;
     $("header").addClass("closed");
 
-    var precision = OSM.zoomPrecision(map.getZoom());
-
     OSM.router.replace("/directions?" + Qs.stringify({
       engine: chosenEngine.id,
-      route: o.lat.toFixed(precision) + "," + o.lng.toFixed(precision) + ";" +
-             d.lat.toFixed(precision) + "," + d.lng.toFixed(precision)
+      route: points.map(p => OSM.cropLocation(p, map.getZoom()).join()).join(";")
     }));
 
     // copy loading item to sidebar and display it. we copy it, rather than
@@ -136,7 +132,7 @@ OSM.Directions = function (map) {
     $("#sidebar_content").html($(".directions_form .loader_copy").html());
     map.setSidebarOverlaid(false);
 
-    routeRequest = chosenEngine.getRoute([o, d], function (err, route) {
+    routeRequest = chosenEngine.getRoute(points, function (err, route) {
       routeRequest = null;
 
       if (err) {
@@ -285,10 +281,8 @@ OSM.Directions = function (map) {
       var pt = L.DomEvent.getMousePosition(oe, map.getContainer()); // co-ordinates of the mouse pointer at present
       pt.y += 20;
       var ll = map.containerPointToLatLng(pt);
-      var precision = OSM.zoomPrecision(map.getZoom());
-      var value = ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision);
-      var llWithPrecision = L.latLng(ll.lat.toFixed(precision), ll.lng.toFixed(precision));
-      endpoints[type === "from" ? 0 : 1].setValue(value, llWithPrecision);
+      const llWithPrecision = OSM.cropLocation(ll, map.getZoom());
+      endpoints[type === "from" ? 0 : 1].setValue(llWithPrecision.join(", "), llWithPrecision);
     });
 
     endpoints[0].enable();
index a953920f895d8fe62cefafdd3927f90f040ea62d..3b23b9cbf3abba23e53375f89cabf95eeea2d9b3 100644 (file)
@@ -36,16 +36,16 @@ OSM.Export = function (map) {
   }
 
   function setBounds(bounds) {
-    var precision = OSM.zoomPrecision(map.getZoom());
-    $("#minlon").val(bounds.getWest().toFixed(precision));
-    $("#minlat").val(bounds.getSouth().toFixed(precision));
-    $("#maxlon").val(bounds.getEast().toFixed(precision));
-    $("#maxlat").val(bounds.getNorth().toFixed(precision));
+    const truncated = [bounds.getSouthWest(), bounds.getNorthEast()]
+      .map(c => OSM.cropLocation(c, map.getZoom()));
+    $("#minlon").val(truncated[0][1]);
+    $("#minlat").val(truncated[0][0]);
+    $("#maxlon").val(truncated[1][1]);
+    $("#maxlat").val(truncated[1][0]);
 
     $("#export_overpass").attr("href",
                                "https://overpass-api.de/api/map?bbox=" +
-                               $("#minlon").val() + "," + $("#minlat").val() + "," +
-                               $("#maxlon").val() + "," + $("#maxlat").val());
+                               truncated.map(p => p.reverse()).join());
   }
 
   function validateControls() {
index 09e4de31e0c6e78faf57444534b55fa3c5653292..672f650fa2145ee8832beead533b5a9161a61765 100644 (file)
@@ -272,18 +272,18 @@ OSM.Query = function (map) {
   function queryOverpass(lat, lng) {
     var latlng = L.latLng(lat, lng).wrap(),
         bounds = map.getBounds().wrap(),
-        precision = OSM.zoomPrecision(map.getZoom()),
-        bbox = bounds.getSouth().toFixed(precision) + "," +
-               bounds.getWest().toFixed(precision) + "," +
-               bounds.getNorth().toFixed(precision) + "," +
-               bounds.getEast().toFixed(precision),
-        radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
-        around = "around:" + radius + "," + lat + "," + lng,
-        nodes = "node(" + around + ")",
-        ways = "way(" + around + ")",
-        relations = "relation(" + around + ")",
-        nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
-        isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
+        zoom = map.getZoom(),
+        bbox = [bounds.getSouthWest(), bounds.getNorthEast()]
+          .map(c => OSM.cropLocation(c, zoom))
+          .join(),
+        geombbox = "geom(" + bbox + ");",
+        radius = 10 * Math.pow(1.5, 19 - zoom),
+        around = "(around:" + radius + "," + lat + "," + lng + ")",
+        nodes = "node" + around,
+        ways = "way" + around,
+        relations = "relation" + around,
+        nearby = "(" + nodes + ";" + ways + ";);out tags " + geombbox + relations + ";out " + geombbox,
+        isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids " + geombbox + "relation(pivot.a);out tags bb;";
 
     $("#sidebar_content .query-intro")
       .hide();
@@ -299,12 +299,9 @@ OSM.Query = function (map) {
   }
 
   function clickHandler(e) {
-    var precision = OSM.zoomPrecision(map.getZoom()),
-        latlng = e.latlng.wrap(),
-        lat = latlng.lat.toFixed(precision),
-        lng = latlng.lng.toFixed(precision);
+    const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
-    OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
+    OSM.router.route("/query?" + Qs.stringify({ lat, lon }));
   }
 
   function enableQueryMode() {
index 2bfbb2e1c6d4fa9bc18c7264cdbb65651785899a..7f93cca69a9257bbfa714fe2489a785cfdd6d4ef 100644 (file)
@@ -33,12 +33,9 @@ OSM.Search = function (map) {
   $(".describe_location").on("click", function (e) {
     e.preventDefault();
     $("header").addClass("closed");
-    var center = map.getCenter().wrap(),
-        precision = OSM.zoomPrecision(map.getZoom()),
-        lat = center.lat.toFixed(precision),
-        lng = center.lng.toFixed(precision);
+    const [lat, lon] = OSM.cropLocation(map.getCenter(), map.getZoom()).map(encodeURIComponent);
 
-    OSM.router.route("/search?lat=" + encodeURIComponent(lat) + "&lon=" + encodeURIComponent(lng));
+    OSM.router.route("/search?" + Qs.stringify({ lat, lon }));
   });
 
   $("#sidebar_content")
index 10ce9471580b038c776c232c3a953c6d499c5559..91d9efbde9c708999428a8f4d1355fb874d2b348 100644 (file)
@@ -152,13 +152,10 @@ L.OSM.Map = L.Map.extend({
   },
 
   getUrl: function (marker) {
-    var precision = OSM.zoomPrecision(this.getZoom()),
-        params = {};
+    const params = {};
 
     if (marker && this.hasLayer(marker)) {
-      var latLng = marker.getLatLng().wrap();
-      params.mlat = latLng.lat.toFixed(precision);
-      params.mlon = latLng.lng.toFixed(precision);
+      [params.mlat, params.mlon] = OSM.cropLocation(marker.getLatLng(), this.getZoom());
     }
 
     var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
@@ -236,21 +233,14 @@ L.OSM.Map = L.Map.extend({
   },
 
   getGeoUri: function (marker) {
-    var precision = OSM.zoomPrecision(this.getZoom()),
-        latLng,
-        params = {};
+    let latLng = this.getCenter();
+    const zoom = this.getZoom();
 
     if (marker && this.hasLayer(marker)) {
-      latLng = marker.getLatLng().wrap();
-    } else {
-      latLng = this.getCenter();
+      latLng = marker.getLatLng();
     }
 
-    params.lat = latLng.lat.toFixed(precision);
-    params.lon = latLng.lng.toFixed(precision);
-    params.zoom = this.getZoom();
-
-    return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
+    return `geo:${OSM.cropLocation(latLng, zoom).join(",")}?z=${zoom}`;
   },
 
   addObject: function (object, callback) {
index 602d3af10afb65049f194af1dca778832686ec65..4eebbe871f85e85bbd76f7e41c10fcccc1f72281 100644 (file)
@@ -190,13 +190,9 @@ OSM = {
       layers = args.layers || "";
     }
 
-    center = center.wrap();
     layers = layers.replace("M", "");
 
-    var precision = OSM.zoomPrecision(zoom),
-        hash = "#map=" + zoom +
-          "/" + center.lat.toFixed(precision) +
-          "/" + center.lng.toFixed(precision);
+    let hash = "#map=" + [zoom, ...OSM.cropLocation(center, zoom)].join("/");
 
     if (layers) {
       hash += "&layers=" + layers;
@@ -211,11 +207,16 @@ OSM = {
     return Math.ceil(Math.log10(pixels / degrees));
   },
 
+  cropLocation: function (latLng, zoom) {
+    const precision = OSM.zoomPrecision(zoom),
+          wrapped = latLng.wrap();
+    return [wrapped.lat, wrapped.lng].map(c => c.toFixed(precision));
+  },
+
   locationCookie: function (map) {
-    var center = map.getCenter().wrap(),
-        zoom = map.getZoom(),
-        precision = OSM.zoomPrecision(zoom);
-    return [center.lng.toFixed(precision), center.lat.toFixed(precision), zoom, map.getLayersCode()].join("|");
+    const zoom = map.getZoom(),
+          center = OSM.cropLocation(map.getCenter(), zoom).reverse();
+    return [...center, zoom, map.getLayersCode()].join("|");
   },
 
   distance: function (latlng1, latlng2) {
index b94db8b557e8fe7430be8e14fe490887fa107918..1d167d977fb70e85481179b0671e963c9bc1c08a 100644 (file)
@@ -64,12 +64,10 @@ $(document).ready(function () {
       map.on("click", function (e) {
         if (!$("#updatehome").is(":checked")) return;
 
-        var zoom = map.getZoom(),
-            precision = OSM.zoomPrecision(zoom),
-            location = e.latlng.wrap();
+        const [lat, lon] = OSM.cropLocation(e.latlng);
 
-        $("#home_lat").val(location.lat.toFixed(precision));
-        $("#home_lon").val(location.lng.toFixed(precision));
+        $("#home_lat").val(lat);
+        $("#home_lon").val(lon);
 
         deleted_lat = null;
         deleted_lon = null;
index a9b85de7dc3f68d40fa84cab7b782b0766fda9e9..ae101f373762b0c282ed1b6218b1bbe875cfc5b7 100644 (file)
@@ -102,14 +102,11 @@ module Api
         node_ids += way_node_ids.flatten
         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
 
-        visible_nodes = {}
-
         @nodes = []
         nodes.each do |node|
           next unless node.visible? # should be unnecessary if data is consistent.
 
           @nodes << node
-          visible_nodes[node.id] = node
         end
 
         @ways = []
index 27c4a1fcc7a319ce21e5e629aeeebbe9f81dc765..632fdb9a629637d1112831012e803e3f145605ba 100644 (file)
@@ -76,15 +76,10 @@ module Api
       @way = Way.includes(:nodes => :node_tags).find(params[:id])
 
       if @way.visible
-        visible_nodes = {}
-
         @nodes = []
 
         @way.nodes.uniq.each do |node|
-          if node.visible
-            @nodes << node
-            visible_nodes[node.id] = node
-          end
+          @nodes << node if node.visible
         end
 
         # Render the result