1 OSM.Query = function (map) {
2 var url = OSM.OVERPASS_URL,
3 credentials = OSM.OVERPASS_CREDENTIALS,
4 queryButton = $(".control-query .control-button"),
5 uninterestingTags = ["source", "source_ref", "source:ref", "history", "attribution", "created_by", "tiger:county", "tiger:tlid", "tiger:upload_uuid", "KSJ2:curve_id", "KSJ2:lat", "KSJ2:lon", "KSJ2:coordinate", "KSJ2:filename", "note:ja"],
16 queryButton.on("click", function (e) {
20 if (queryButton.hasClass("active")) {
22 } else if (!queryButton.hasClass("disabled")) {
25 }).on("disabled", function () {
26 if (queryButton.hasClass("active")) {
27 map.off("click", clickHandler);
28 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
29 $(this).tooltip("show");
31 }).on("enabled", function () {
32 if (queryButton.hasClass("active")) {
33 map.on("click", clickHandler);
34 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
35 $(this).tooltip("hide");
39 function showResultGeometry() {
40 var geometry = $(this).data("geometry");
41 if (geometry) map.addLayer(geometry);
42 $(this).addClass("selected");
45 function hideResultGeometry() {
46 var geometry = $(this).data("geometry");
47 if (geometry) map.removeLayer(geometry);
48 $(this).removeClass("selected");
52 .on("mouseover", ".query-results a", showResultGeometry)
53 .on("mouseout", ".query-results a", hideResultGeometry);
55 function interestingFeature(feature) {
57 for (var key in feature.tags) {
58 if (uninterestingTags.indexOf(key) < 0) {
67 function featurePrefix(feature) {
68 var tags = feature.tags;
71 if (tags.boundary === "administrative" && tags.admin_level) {
72 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
73 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
76 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
83 if (prefixes[key][value]) {
84 return prefixes[key][value];
93 var first = value.slice(0, 1).toUpperCase(),
94 rest = value.slice(1).replace(/_/g, " ");
102 prefix = I18n.t("javascripts.query." + feature.type);
108 function featureName(feature) {
109 var tags = feature.tags,
110 locales = OSM.preferred_languages;
112 for (const locale of locales) {
113 if (tags["name:" + locale]) {
114 return tags["name:" + locale];
120 } else if (tags.ref) {
122 } else if (tags["addr:housename"]) {
123 return tags["addr:housename"];
124 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
125 return tags["addr:housenumber"] + " " + tags["addr:street"];
127 return "#" + feature.id;
131 function featureGeometry(feature) {
134 if (feature.type === "node" && feature.lat && feature.lon) {
135 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
136 } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
137 geometry = L.polyline(feature.geometry.filter(function (point) {
138 return point !== null;
139 }).map(function (point) {
140 return [point.lat, point.lon];
142 } else if (feature.type === "relation" && feature.members) {
143 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
144 return typeof geometry !== "undefined";
151 function runQuery(latlng, radius, query, $section, merge, compare) {
152 var $ul = $section.find("ul");
157 if ($section.data("ajax")) {
158 $section.data("ajax").abort();
161 $section.data("ajax", $.ajax({
165 data: "[timeout:10][out:json];" + query
168 withCredentials: credentials
170 success: function (results) {
173 $section.find(".loader").hide();
176 elements = results.elements.reduce(function (hash, element) {
177 var key = element.type + element.id;
178 if ("geometry" in element) {
179 delete element.bounds;
181 hash[key] = $.extend({}, hash[key], element);
185 elements = Object.keys(elements).map(function (key) {
186 return elements[key];
189 elements = results.elements;
193 elements = elements.sort(compare);
196 for (const element of elements) {
197 if (!interestingFeature(element)) continue;
200 .addClass("list-group-item list-group-item-action")
201 .text(featurePrefix(element) + " ")
205 .addClass("stretched-link")
206 .attr("href", "/" + element.type + "/" + element.id)
207 .data("geometry", featureGeometry(element))
208 .text(featureName(element))
212 if (results.remark) {
214 .addClass("list-group-item")
215 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
219 if ($ul.find("li").length === 0) {
221 .addClass("list-group-item")
222 .text(I18n.t("javascripts.query.nothing_found"))
226 error: function (xhr, status, error) {
227 $section.find(".loader").hide();
230 .addClass("list-group-item")
231 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
237 function compareSize(feature1, feature2) {
238 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
239 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
240 area1 = width1 * height1,
241 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
242 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
243 area2 = width2 * height2;
245 return area1 - area2;
249 * To find nearby objects we ask overpass for the union of the
252 * node(around:<radius>,<lat>,<lng>)
253 * way(around:<radius>,<lat>,<lng>)
254 * relation(around:<radius>,<lat>,<lng>)
256 * to find enclosing objects we first find all the enclosing areas:
258 * is_in(<lat>,<lng>)->.a
260 * and then return the union of the following sets:
265 * In both cases we then ask to retrieve tags and the geometry
268 function queryOverpass(lat, lng) {
269 var latlng = L.latLng(lat, lng).wrap(),
270 bounds = map.getBounds().wrap(),
271 zoom = map.getZoom(),
272 bbox = [bounds.getSouthWest(), bounds.getNorthEast()]
273 .map(c => OSM.cropLocation(c, zoom))
275 geombbox = "geom(" + bbox + ");",
276 radius = 10 * Math.pow(1.5, 19 - zoom),
277 around = "(around:" + radius + "," + lat + "," + lng + ")",
278 nodes = "node" + around,
279 ways = "way" + around,
280 relations = "relation" + around,
281 nearby = "(" + nodes + ";" + ways + ";);out tags " + geombbox + relations + ";out " + geombbox,
282 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids " + geombbox + "relation(pivot.a);out tags bb;";
284 $("#sidebar_content .query-intro")
287 if (marker) map.removeLayer(marker);
288 marker = L.circle(latlng, Object.assign({
290 className: "query-marker"
291 }, featureStyle)).addTo(map);
293 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
294 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
297 function clickHandler(e) {
298 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
300 OSM.router.route("/query?" + new URLSearchParams({ lat, lon }));
303 function enableQueryMode() {
304 queryButton.addClass("active");
305 map.on("click", clickHandler);
306 $(map.getContainer()).addClass("query-active");
309 function disableQueryMode() {
310 if (marker) map.removeLayer(marker);
311 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
312 map.off("click", clickHandler);
313 queryButton.removeClass("active");
318 page.pushstate = page.popstate = function (path) {
319 OSM.loadSidebarContent(path, function () {
320 page.load(path, true);
324 page.load = function (path, noCentre) {
325 const params = new URLSearchParams(path.substring(path.indexOf("?"))),
326 latlng = L.latLng(params.get("lat"), params.get("lon"));
328 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
329 OSM.router.withoutMoveListener(function () {
330 map.setView(latlng, 15);
334 queryOverpass(params.get("lat"), params.get("lon"));
337 page.unload = function (sameController) {
338 if (!sameController) {
340 $("#sidebar_content .query-results a.selected").each(hideResultGeometry);