]> git.openstreetmap.org Git - rails.git/commitdiff
Filter out nodes which are too far away
authorTom Hughes <tom@compton.nu>
Sun, 9 Mar 2014 11:46:38 +0000 (11:46 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 16 Mar 2014 16:18:13 +0000 (16:18 +0000)
app/assets/javascripts/index/query.js
app/assets/javascripts/osm.js.erb
test/javascripts/osm_test.js

index 8d04efd5f7a4bb19af540d5de470dc848f3f9233..80df594c82b0b34250a8f4a84fff2e907aa11a64 100644 (file)
@@ -1,6 +1,7 @@
 OSM.Query = function(map) {
   var queryButton = $(".control-query .control-button"),
     uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
 OSM.Query = function(map) {
   var queryButton = $(".control-query .control-button"),
     uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
+    searchRadius = 10,
     marker;
 
   queryButton.on("click", function (e) {
     marker;
 
   queryButton.on("click", function (e) {
@@ -28,8 +29,13 @@ OSM.Query = function(map) {
       $(this).removeClass("selected");
     });
 
       $(this).removeClass("selected");
     });
 
-  function interestingFeature(feature) {
+  function interestingFeature(feature, origin) {
     if (feature.tags) {
     if (feature.tags) {
+      if (feature.type === "node" &&
+          OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > searchRadius) {
+        return false;
+      }
+
       for (var key in feature.tags) {
         if (uninterestingTags.indexOf(key) < 0) {
           return true;
       for (var key in feature.tags) {
         if (uninterestingTags.indexOf(key) < 0) {
           return true;
@@ -118,7 +124,7 @@ OSM.Query = function(map) {
     return geometry;
   }
 
     return geometry;
   }
 
-  function runQuery(query, $section) {
+  function runQuery(latlng, query, $section) {
     var $ul = $section.find("ul");
 
     $ul.empty();
     var $ul = $section.find("ul");
 
     $ul.empty();
@@ -148,7 +154,7 @@ OSM.Query = function(map) {
         for (var i = 0; i < results.elements.length; i++) {
           var element = results.elements[i];
 
         for (var i = 0; i < results.elements.length; i++) {
           var element = results.elements[i];
 
-          if (interestingFeature(element)) {
+          if (interestingFeature(element, latlng)) {
             var $li = $("<li>")
               .data("geometry", featureGeometry(element, nodes))
               .appendTo($ul);
             var $li = $("<li>")
               .data("geometry", featureGeometry(element, nodes))
               .appendTo($ul);
@@ -169,7 +175,7 @@ OSM.Query = function(map) {
 
   function queryOverpass(lat, lng) {
     var latlng = L.latLng(lat, lng),
 
   function queryOverpass(lat, lng) {
     var latlng = L.latLng(lat, lng),
-      around = "around:10.0," + lat + "," + lng,
+      around = "around:" + searchRadius + "," + lat + "," + lng,
       features = "(node(" + around + ");way(" + around + ");relation(" + around + "))",
       nearby = "((" + features + ";way(bn));node(w));out;",
       isin = "(is_in(" + lat + "," + lng + ");>);out;";
       features = "(node(" + around + ");way(" + around + ");relation(" + around + "))",
       nearby = "((" + features + ";way(bn));node(w));out;",
       isin = "(is_in(" + lat + "," + lng + ");>);out;";
@@ -180,7 +186,7 @@ OSM.Query = function(map) {
     if (marker) {
       marker.setLatLng(latlng).addTo(map);
     } else {
     if (marker) {
       marker.setLatLng(latlng).addTo(map);
     } else {
-      marker = L.circle(latlng, 10, { clickable: false }).addTo(map);
+      marker = L.circle(latlng, searchRadius, { clickable: false }).addTo(map);
     }
 
     $(document).everyTime(75, "fadeQueryMarker", function (i) {
     }
 
     $(document).everyTime(75, "fadeQueryMarker", function (i) {
@@ -194,8 +200,8 @@ OSM.Query = function(map) {
       }
     }, 10);
 
       }
     }, 10);
 
-    runQuery(nearby, $("#query-nearby"));
-    runQuery(isin, $("#query-isin"));
+    runQuery(latlng, nearby, $("#query-nearby"));
+    runQuery(latlng, isin, $("#query-isin"));
   }
 
   function clickHandler(e) {
   }
 
   function clickHandler(e) {
index 103110f7edd36756570d36d9d853c3e5b97dc80d..719c6b83266433a6e461f5eb1afb8d6d5d0a5c76 100644 (file)
@@ -170,5 +170,20 @@ OSM = {
       zoom = map.getZoom(),
       precision = OSM.zoomPrecision(zoom);
     return [center.lng.toFixed(precision), center.lat.toFixed(precision), zoom, map.getLayersCode()].join('|');
       zoom = map.getZoom(),
       precision = OSM.zoomPrecision(zoom);
     return [center.lng.toFixed(precision), center.lat.toFixed(precision), zoom, map.getLayersCode()].join('|');
+  },
+
+  distance: function(latlng1, latlng2) {
+    var lat1 = latlng1.lat * Math.PI / 180,
+      lng1 = latlng1.lng * Math.PI / 180,
+      lat2 = latlng2.lat * Math.PI / 180,
+      lng2 = latlng2.lng * Math.PI / 180,
+      latdiff = lat2 - lat1,
+      lngdiff = lng2 - lng1;
+
+    return 6372795 * 2 * Math.asin(
+      Math.sqrt(
+        Math.pow(Math.sin(latdiff / 2), 2) + 
+        Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2)
+      ));
   }
 };
   }
 };
index f26f08c9f4594de9043d6cb0eb5207c083a200db..51f74fe7a56b98087fba50772169642e0797d673 100644 (file)
@@ -239,4 +239,14 @@ describe("OSM", function () {
       expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
     });
   });
       expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
     });
   });
+
+  describe(".distance", function () {
+    it("computes distance between points", function () {
+      var latlng1 = L.latLng(51.76712,-0.00484),
+        latlng2 = L.latLng(51.7675159, -0.0078329);
+
+      expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
+      expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);
+    });
+  });
 });
 });