1 OSM.initializations.push(function (map) {
2 const control = $(".control-query"),
3 queryButton = control.find(".control-button");
5 queryButton.on("click", function (e) {
9 if (control.hasClass("active")) {
11 } else if (!queryButton.hasClass("disabled")) {
14 }).on("disabled", function () {
15 if (control.hasClass("active")) {
16 map.off("click", clickHandler);
17 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
18 $(this).tooltip("show");
20 }).on("enabled", function () {
21 if (control.hasClass("active")) {
22 map.on("click", clickHandler);
23 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
24 $(this).tooltip("hide");
28 function clickHandler(e) {
29 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
31 OSM.router.route("/query?" + new URLSearchParams({ lat, lon }));
34 function enableQueryMode() {
35 $(".control-query").addClass("active");
36 map.on("click", clickHandler);
37 $(map.getContainer()).addClass("query-active");
40 function disableQueryMode() {
41 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
42 map.off("click", clickHandler);
43 $(".control-query").removeClass("active");
47 OSM.Query = function (map) {
48 const 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"];
51 const featureStyle = {
59 function showResultGeometry() {
60 const geometry = $(this).data("geometry");
61 if (geometry) map.addLayer(geometry);
62 $(this).addClass("selected");
65 function hideResultGeometry() {
66 const geometry = $(this).data("geometry");
67 if (geometry) map.removeLayer(geometry);
68 $(this).removeClass("selected");
72 .on("mouseover", ".query-results a", showResultGeometry)
73 .on("mouseout", ".query-results a", hideResultGeometry);
75 function interestingFeature(feature) {
77 for (const key in feature.tags) {
78 if (uninterestingTags.indexOf(key) < 0) {
87 function featurePrefix(feature) {
88 const tags = feature.tags;
91 if (tags.boundary === "administrative" && (tags.border_type || tags.admin_level)) {
92 prefix = OSM.i18n.t("geocoder.search_osm_nominatim.border_types." + tags.border_type, {
93 defaultValue: OSM.i18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
94 defaultValue: OSM.i18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
98 const prefixes = OSM.i18n.t("geocoder.search_osm_nominatim.prefix");
100 for (const key in tags) {
101 const value = tags[key];
104 if (prefixes[key][value]) {
105 return prefixes[key][value];
110 for (const key in tags) {
111 const value = tags[key];
114 const first = value.slice(0, 1).toUpperCase(),
115 rest = value.slice(1).replace(/_/g, " ");
123 prefix = OSM.i18n.t("javascripts.query." + feature.type);
129 function featureName(feature) {
130 const tags = feature.tags,
131 localeKeys = OSM.preferred_languages.map(locale => `name:${locale}`);
133 for (const key of [...localeKeys, "name", "ref", "addr:housename"]) {
134 if (tags[key]) return tags[key];
136 if (tags["addr:housenumber"] && tags["addr:street"]) return `${tags["addr:housenumber"]} ${tags["addr:street"]}`;
138 return "#" + feature.id;
141 function featureGeometry(feature) {
142 switch (feature.type) {
144 if (!feature.lat || !feature.lon) return;
145 return L.circleMarker([feature.lat, feature.lon], featureStyle);
147 if (!feature.geometry?.length) return;
148 return L.polyline(feature.geometry.filter(p => p).map(p => [p.lat, p.lon]), featureStyle);
150 if (!feature.members?.length) return;
151 return L.featureGroup(feature.members.map(featureGeometry).filter(g => g));
155 function runQuery(query, $section, merge, compare) {
156 const $ul = $section.find("ul");
161 if ($section.data("ajax")) {
162 $section.data("ajax").abort();
165 $section.data("ajax", new AbortController());
166 fetch(OSM.OVERPASS_URL, {
168 body: new URLSearchParams({
169 data: "[timeout:10][out:json];" + query
171 credentials: OSM.OVERPASS_CREDENTIALS ? "include" : "same-origin",
172 signal: $section.data("ajax").signal
174 .then(response => response.json())
175 .then(function (results) {
176 let elements = results.elements;
178 $section.find(".loader").hide();
180 // Make Overpass-specific bounds to Leaflet compatible
181 for (const element of elements) {
182 if (!element.bounds) continue;
183 if (element.bounds.maxlon >= element.bounds.minlon) continue;
184 element.bounds.maxlon += 360;
188 elements = Object.values(elements.reduce(function (hash, element) {
189 const key = element.type + element.id;
190 if ("geometry" in element) delete element.bounds;
191 hash[key] = { ...hash[key], ...element };
197 elements = elements.sort(compare);
200 for (const element of elements) {
201 if (!interestingFeature(element)) continue;
203 const $li = $("<li>")
204 .addClass("list-group-item list-group-item-action")
205 .text(featurePrefix(element) + " ")
209 .addClass("stretched-link")
210 .attr("href", "/" + element.type + "/" + element.id)
211 .data("geometry", featureGeometry(element))
212 .text(featureName(element))
216 if (results.remark) renderError($ul, results.remark);
218 if ($ul.find("li").length === 0) {
220 .addClass("list-group-item")
221 .text(OSM.i18n.t("javascripts.query.nothing_found"))
225 .catch(function (error) {
226 if (error.name === "AbortError") return;
228 $section.find(".loader").hide();
230 renderError($ul, error.message);
234 function renderError($ul, errorMessage) {
236 .addClass("list-group-item")
237 .text(OSM.i18n.t("javascripts.query.error", { server: OSM.OVERPASS_URL, error: errorMessage }))
241 function size({ maxlon, minlon, maxlat, minlat }) {
242 return (maxlon - minlon) * (maxlat - minlat);
246 * To find nearby objects we ask overpass for the union of the
249 * node(around:<radius>,<lat>,<lng>)
250 * way(around:<radius>,<lat>,<lng>)
251 * relation(around:<radius>,<lat>,<lng>)
253 * to find enclosing objects we first find all the enclosing areas:
255 * is_in(<lat>,<lng>)->.a
257 * and then return the union of the following sets:
262 * In both cases we then ask to retrieve tags and the geometry
265 function queryOverpass(latlng) {
266 const bounds = map.getBounds(),
267 zoom = map.getZoom(),
268 bbox = [bounds.getSouthWest(), bounds.getNorthEast()]
269 .map(c => OSM.cropLocation(c, zoom))
271 geom = `geom(${bbox})`,
272 radius = 10 * Math.pow(1.5, 19 - zoom),
273 here = `(around:${radius},${latlng})`,
274 enclosed = "(pivot.a);out tags bb",
275 nearby = `(node${here};way${here};);out tags ${geom};relation${here};out ${geom};`,
276 isin = `is_in(${latlng})->.a;way${enclosed};out ids ${geom};relation${enclosed};`;
278 $("#sidebar_content .query-intro")
281 if (marker) map.removeLayer(marker);
282 marker = L.circle(L.latLng(latlng).wrap(), {
284 className: "query-marker",
288 runQuery(nearby, $("#query-nearby"), false);
289 runQuery(isin, $("#query-isin"), true, (feature1, feature2) => size(feature1.bounds) - size(feature2.bounds));
294 page.pushstate = page.popstate = function (path) {
295 OSM.loadSidebarContent(path, function () {
296 page.load(path, true);
300 page.load = function (path, noCentre) {
301 const params = new URLSearchParams(path.substring(path.indexOf("?"))),
302 latlng = L.latLng(params.get("lat"), params.get("lon"));
304 if (!location.hash && !noCentre && !map.getBounds().contains(latlng)) {
305 OSM.router.withoutMoveListener(function () {
306 map.setView(latlng, 15);
310 queryOverpass([params.get("lat"), params.get("lon")]);
313 page.unload = function (sameController) {
314 if (!sameController) {
315 $("#sidebar_content .query-results a.selected").each(hideResultGeometry);