From: Tom Hughes Date: Tue, 4 Nov 2014 09:55:26 +0000 (+0000) Subject: Sort enclosing features by size X-Git-Tag: live~4307^2~4 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/9c7e0e13d79248830a4041877ccdcdb3981a0b7b?ds=sidebyside Sort enclosing features by size --- diff --git a/app/assets/javascripts/index/query.js b/app/assets/javascripts/index/query.js index 12261abb8..cabb93f50 100644 --- a/app/assets/javascripts/index/query.js +++ b/app/assets/javascripts/index/query.js @@ -149,7 +149,7 @@ OSM.Query = function(map) { return geometry; } - function runQuery(latlng, radius, query, $section) { + function runQuery(latlng, radius, query, $section, compare) { var $ul = $section.find("ul"); $ul.empty(); @@ -170,10 +170,18 @@ OSM.Query = function(map) { data: "[timeout:5][out:json];" + query, }, success: function(results) { + var elements; + $section.find(".loader").stopTime("loading").hide(); - for (var i = 0; i < results.elements.length; i++) { - var element = results.elements[i]; + if (compare) { + elements = results.elements.sort(compare); + } else { + elements = results.elements; + } + + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; if (interestingFeature(element, latlng, radius)) { var $li = $("
  • ") @@ -207,6 +215,17 @@ OSM.Query = function(map) { })); } + function compareSize(feature1, feature2) { + var width1 = feature1.bounds.maxlon - feature1.bounds.minlon, + height1 = feature1.bounds.maxlat - feature1.bounds.minlat, + area1 = width1 * height1, + width2 = feature2.bounds.maxlat - feature2.bounds.minlat, + height2 = feature2.bounds.maxlat - feature2.bounds.minlat, + area2 = width2 * height2; + + return area1 - area2; + } + /* * To find nearby objects we ask overpass for the union of the * following sets: @@ -237,7 +256,7 @@ OSM.Query = function(map) { ways = "way(" + around + ")", relations = "relation(" + around + ")", nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");", - isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a));out geom(" + bbox + ");"; + isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags geom(" + bbox + ");relation(pivot.a);out tags bb;"; $("#sidebar_content .query-intro") .hide(); @@ -257,7 +276,7 @@ OSM.Query = function(map) { }, 10); runQuery(latlng, radius, nearby, $("#query-nearby")); - runQuery(latlng, radius, isin, $("#query-isin")); + runQuery(latlng, radius, isin, $("#query-isin"), compareSize); } function clickHandler(e) {