1 //= require jquery.simulate
3 OSM.Query = function(map) {
4 var protocol = document.location.protocol === "https:" ? "https:" : "http:",
5 url = protocol + OSM.OVERPASS_URL,
6 queryButton = $(".control-query .control-button"),
7 uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
18 queryButton.on("click", function (e) {
22 if (queryButton.hasClass("disabled")) return;
24 if (queryButton.hasClass("active")) {
25 if ($("#content").hasClass("overlay-sidebar")) {
31 }).on("disabled", function (e) {
32 if (queryButton.hasClass("active")) {
33 map.off("click", clickHandler);
34 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
35 $(this).tooltip("show");
37 }).on("enabled", function (e) {
38 if (queryButton.hasClass("active")) {
39 map.on("click", clickHandler);
40 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
41 $(this).tooltip("hide");
46 .on("mouseover", ".query-results li.query-result", function () {
47 var geometry = $(this).data("geometry")
48 if (geometry) map.addLayer(geometry);
49 $(this).addClass("selected");
51 .on("mouseout", ".query-results li.query-result", function () {
52 var geometry = $(this).data("geometry")
53 if (geometry) map.removeLayer(geometry);
54 $(this).removeClass("selected");
56 .on("mousedown", ".query-results li.query-result", function (e) {
58 $(this).one("click", function (e) {
60 var geometry = $(this).data("geometry")
61 if (geometry) map.removeLayer(geometry);
63 if (!$(e.target).is('a')) {
64 $(this).find("a").simulate("click", e);
67 }).one("mousemove", function () {
72 function interestingFeature(feature, origin, radius) {
74 for (var key in feature.tags) {
75 if (uninterestingTags.indexOf(key) < 0) {
84 function featurePrefix(feature) {
85 var tags = feature.tags;
88 if (tags.boundary === "administrative") {
89 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
91 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
93 for (var key in tags) {
94 var value = tags[key];
97 if (prefixes[key][value]) {
98 return prefixes[key][value];
100 var first = value.substr(0, 1).toUpperCase(),
101 rest = value.substr(1).replace(/_/g, " ");
110 prefix = I18n.t("javascripts.query." + feature.type);
116 function featureName(feature) {
117 var tags = feature.tags;
121 } else if (tags["ref"]) {
123 } else if (tags["addr:housename"]) {
124 return tags["addr:housename"];
125 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
126 return tags["addr:housenumber"] + " " + tags["addr:street"];
128 return "#" + feature.id;
132 function featureGeometry(feature) {
135 if (feature.type === "node" && feature.lat && feature.lon) {
136 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
137 } else if (feature.type === "way" && feature.geometry) {
138 geometry = L.polyline(feature.geometry.filter(function (point) {
139 return point !== null;
140 }).map(function (point) {
141 return [point.lat, point.lon];
143 } else if (feature.type === "relation" && feature.members) {
144 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
145 return geometry !== undefined;
152 function runQuery(latlng, radius, query, $section, compare) {
153 var $ul = $section.find("ul");
158 $section.find(".loader").oneTime(1000, "loading", function () {
162 if ($section.data("ajax")) {
163 $section.data("ajax").abort();
166 $section.data("ajax", $.ajax({
170 data: "[timeout:5][out:json];" + query,
172 success: function(results) {
175 $section.find(".loader").stopTime("loading").hide();
178 elements = results.elements.sort(compare);
180 elements = results.elements;
183 for (var i = 0; i < elements.length; i++) {
184 var element = elements[i];
186 if (interestingFeature(element, latlng, radius)) {
188 .addClass("query-result")
189 .data("geometry", featureGeometry(element))
192 .text(featurePrefix(element) + " ")
196 .attr("href", "/" + element.type + "/" + element.id)
197 .text(featureName(element))
202 if ($ul.find("li").length == 0) {
204 .text(I18n.t("javascripts.query.nothing_found"))
208 error: function(xhr, status, error) {
209 $section.find(".loader").stopTime("loading").hide();
212 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
218 function compareSize(feature1, feature2) {
219 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
220 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
221 area1 = width1 * height1,
222 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
223 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
224 area2 = width2 * height2;
226 return area1 - area2;
230 * To find nearby objects we ask overpass for the union of the
233 * node(around:<radius>,<lat>,lng>)
234 * way(around:<radius>,<lat>,lng>)
235 * relation(around:<radius>,<lat>,lng>)
237 * to find enclosing objects we first find all the enclosing areas:
239 * is_in(<lat>,<lng>)->.a
241 * and then return the union of the following sets:
246 * In both cases we then ask to retrieve tags and the geometry
249 function queryOverpass(lat, lng) {
250 var latlng = L.latLng(lat, lng),
251 bounds = map.getBounds(),
252 bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
253 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
254 around = "around:" + radius + "," + lat + "," + lng,
255 nodes = "node(" + around + ")",
256 ways = "way(" + around + ")",
257 relations = "relation(" + around + ")",
258 nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
259 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags geom(" + bbox + ");relation(pivot.a);out tags bb;";
261 $("#sidebar_content .query-intro")
264 if (marker) map.removeLayer(marker);
265 marker = L.circle(latlng, radius, featureStyle).addTo(map);
267 $(document).everyTime(75, "fadeQueryMarker", function (i) {
269 map.removeLayer(marker);
272 opacity: 1 - i * 0.1,
273 fillOpacity: 0.5 - i * 0.05
278 runQuery(latlng, radius, nearby, $("#query-nearby"));
279 runQuery(latlng, radius, isin, $("#query-isin"), compareSize);
282 function clickHandler(e) {
283 var precision = OSM.zoomPrecision(map.getZoom()),
284 lat = e.latlng.lat.toFixed(precision),
285 lng = e.latlng.lng.toFixed(precision);
287 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
290 function enableQueryMode() {
291 queryButton.addClass("active");
292 map.on("click", clickHandler);
293 $(map.getContainer()).addClass("query-active");
296 function disableQueryMode() {
297 if (marker) map.removeLayer(marker);
298 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
299 map.off("click", clickHandler);
300 queryButton.removeClass("active");
305 page.pushstate = page.popstate = function(path) {
306 OSM.loadSidebarContent(path, function () {
307 page.load(path, true);
311 page.load = function(path, noCentre) {
312 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
313 latlng = L.latLng(params.lat, params.lon);
315 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
316 OSM.router.withoutMoveListener(function () {
317 map.setView(latlng, 15);
321 queryOverpass(params.lat, params.lon);
325 page.unload = function() {