3 OSM.Query = function (map) {
4 var url = OSM.OVERPASS_URL,
5 credentials = OSM.OVERPASS_CREDENTIALS,
6 queryButton = $(".control-query .control-button"),
7 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"],
18 queryButton.on("click", function (e) {
22 if (queryButton.hasClass("active")) {
24 } else if (!queryButton.hasClass("disabled")) {
27 }).on("disabled", function () {
28 if (queryButton.hasClass("active")) {
29 map.off("click", clickHandler);
30 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
31 $(this).tooltip("show");
33 }).on("enabled", function () {
34 if (queryButton.hasClass("active")) {
35 map.on("click", clickHandler);
36 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
37 $(this).tooltip("hide");
41 function showResultGeometry() {
42 var geometry = $(this).data("geometry");
43 if (geometry) map.addLayer(geometry);
44 $(this).addClass("selected");
47 function hideResultGeometry() {
48 var geometry = $(this).data("geometry");
49 if (geometry) map.removeLayer(geometry);
50 $(this).removeClass("selected");
54 .on("mouseover", ".query-results a", showResultGeometry)
55 .on("mouseout", ".query-results a", hideResultGeometry);
57 function interestingFeature(feature) {
59 for (var key in feature.tags) {
60 if (uninterestingTags.indexOf(key) < 0) {
69 function featurePrefix(feature) {
70 var tags = feature.tags;
73 if (tags.boundary === "administrative" && tags.admin_level) {
74 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
75 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
78 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
85 if (prefixes[key][value]) {
86 return prefixes[key][value];
95 var first = value.slice(0, 1).toUpperCase(),
96 rest = value.slice(1).replace(/_/g, " ");
104 prefix = I18n.t("javascripts.query." + feature.type);
110 function featureName(feature) {
111 var tags = feature.tags,
112 locales = OSM.preferred_languages;
114 for (var i = 0; i < locales.length; i++) {
115 if (tags["name:" + locales[i]]) {
116 return tags["name:" + locales[i]];
122 } else if (tags.ref) {
124 } else if (tags["addr:housename"]) {
125 return tags["addr:housename"];
126 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
127 return tags["addr:housenumber"] + " " + tags["addr:street"];
129 return "#" + feature.id;
133 function featureGeometry(feature) {
136 if (feature.type === "node" && feature.lat && feature.lon) {
137 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
138 } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
139 geometry = L.polyline(feature.geometry.filter(function (point) {
140 return point !== null;
141 }).map(function (point) {
142 return [point.lat, point.lon];
144 } else if (feature.type === "relation" && feature.members) {
145 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
146 return typeof geometry !== "undefined";
153 function runQuery(latlng, radius, query, $section, merge, compare) {
154 var $ul = $section.find("ul");
159 if ($section.data("ajax")) {
160 $section.data("ajax").abort();
163 $section.data("ajax", $.ajax({
167 data: "[timeout:10][out:json];" + query
170 withCredentials: credentials
172 success: function (results) {
175 $section.find(".loader").hide();
178 elements = results.elements.reduce(function (hash, element) {
179 var key = element.type + element.id;
180 if ("geometry" in element) {
181 delete element.bounds;
183 hash[key] = $.extend({}, hash[key], element);
187 elements = Object.keys(elements).map(function (key) {
188 return elements[key];
191 elements = results.elements;
195 elements = elements.sort(compare);
198 for (var i = 0; i < elements.length; i++) {
199 var element = elements[i];
201 if (interestingFeature(element)) {
203 .addClass("list-group-item list-group-item-action")
204 .text(featurePrefix(element) + " ")
208 .addClass("stretched-link")
209 .attr("href", "/" + element.type + "/" + element.id)
210 .data("geometry", featureGeometry(element))
211 .text(featureName(element))
216 if (results.remark) {
218 .addClass("list-group-item")
219 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
223 if ($ul.find("li").length === 0) {
225 .addClass("list-group-item")
226 .text(I18n.t("javascripts.query.nothing_found"))
230 error: function (xhr, status, error) {
231 $section.find(".loader").hide();
234 .addClass("list-group-item")
235 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
241 function compareSize(feature1, feature2) {
242 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
243 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
244 area1 = width1 * height1,
245 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
246 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
247 area2 = width2 * height2;
249 return area1 - area2;
253 * To find nearby objects we ask overpass for the union of the
256 * node(around:<radius>,<lat>,<lng>)
257 * way(around:<radius>,<lat>,<lng>)
258 * relation(around:<radius>,<lat>,<lng>)
260 * to find enclosing objects we first find all the enclosing areas:
262 * is_in(<lat>,<lng>)->.a
264 * and then return the union of the following sets:
269 * In both cases we then ask to retrieve tags and the geometry
272 function queryOverpass(lat, lng) {
273 var latlng = L.latLng(lat, lng).wrap(),
274 bounds = map.getBounds().wrap(),
275 precision = OSM.zoomPrecision(map.getZoom()),
276 bbox = bounds.getSouth().toFixed(precision) + "," +
277 bounds.getWest().toFixed(precision) + "," +
278 bounds.getNorth().toFixed(precision) + "," +
279 bounds.getEast().toFixed(precision),
280 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
281 around = "around:" + radius + "," + lat + "," + lng,
282 nodes = "node(" + around + ")",
283 ways = "way(" + around + ")",
284 relations = "relation(" + around + ")",
285 nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
286 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
288 $("#sidebar_content .query-intro")
291 if (marker) map.removeLayer(marker);
292 marker = L.circle(latlng, Object.assign({
294 className: "query-marker"
295 }, featureStyle)).addTo(map);
297 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
298 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
301 function clickHandler(e) {
302 var precision = OSM.zoomPrecision(map.getZoom()),
303 latlng = e.latlng.wrap(),
304 lat = latlng.lat.toFixed(precision),
305 lng = latlng.lng.toFixed(precision);
307 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
310 function enableQueryMode() {
311 queryButton.addClass("active");
312 map.on("click", clickHandler);
313 $(map.getContainer()).addClass("query-active");
316 function disableQueryMode() {
317 if (marker) map.removeLayer(marker);
318 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
319 map.off("click", clickHandler);
320 queryButton.removeClass("active");
325 page.pushstate = page.popstate = function (path) {
326 OSM.loadSidebarContent(path, function () {
327 page.load(path, true);
331 page.load = function (path, noCentre) {
332 var params = Qs.parse(path.substring(path.indexOf("?") + 1)),
333 latlng = L.latLng(params.lat, params.lon);
335 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
336 OSM.router.withoutMoveListener(function () {
337 map.setView(latlng, 15);
341 queryOverpass(params.lat, params.lon);
344 page.unload = function (sameController) {
345 if (!sameController) {
347 $("#sidebar_content .query-results a.selected").each(hideResultGeometry);