From: Tom Hughes Date: Sun, 9 Mar 2014 11:46:38 +0000 (+0000) Subject: Filter out nodes which are too far away X-Git-Tag: live~4907^2~31 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/479b22ca772c4a128ab7d4ff33cf6a7aba320be2 Filter out nodes which are too far away --- diff --git a/app/assets/javascripts/index/query.js b/app/assets/javascripts/index/query.js index 8d04efd5f..80df594c8 100644 --- a/app/assets/javascripts/index/query.js +++ b/app/assets/javascripts/index/query.js @@ -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'], + searchRadius = 10, marker; queryButton.on("click", function (e) { @@ -28,8 +29,13 @@ OSM.Query = function(map) { $(this).removeClass("selected"); }); - function interestingFeature(feature) { + function interestingFeature(feature, origin) { 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; @@ -118,7 +124,7 @@ OSM.Query = function(map) { return geometry; } - function runQuery(query, $section) { + function runQuery(latlng, query, $section) { 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]; - if (interestingFeature(element)) { + if (interestingFeature(element, latlng)) { var $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), - 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;"; @@ -180,7 +186,7 @@ OSM.Query = function(map) { 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) { @@ -194,8 +200,8 @@ OSM.Query = function(map) { } }, 10); - runQuery(nearby, $("#query-nearby")); - runQuery(isin, $("#query-isin")); + runQuery(latlng, nearby, $("#query-nearby")); + runQuery(latlng, isin, $("#query-isin")); } function clickHandler(e) { diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index 103110f7e..719c6b832 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -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('|'); + }, + + 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) + )); } }; diff --git a/test/javascripts/osm_test.js b/test/javascripts/osm_test.js index f26f08c9f..51f74fe7a 100644 --- a/test/javascripts/osm_test.js +++ b/test/javascripts/osm_test.js @@ -239,4 +239,14 @@ describe("OSM", function () { 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); + }); + }); });