1 //= require jquery-simulate/jquery.simulate
4 OSM.Query = function (map) {
5 var url = OSM.OVERPASS_URL,
6 credentials = OSM.OVERPASS_CREDENTIALS,
7 queryButton = $(".control-query .control-button"),
8 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"],
19 queryButton.on("click", function (e) {
23 if (queryButton.hasClass("active")) {
25 } else if (!queryButton.hasClass("disabled")) {
28 }).on("disabled", function () {
29 if (queryButton.hasClass("active")) {
30 map.off("click", clickHandler);
31 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
32 $(this).tooltip("show");
34 }).on("enabled", function () {
35 if (queryButton.hasClass("active")) {
36 map.on("click", clickHandler);
37 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
38 $(this).tooltip("hide");
43 .on("mouseover", ".query-results li.query-result", function () {
44 var geometry = $(this).data("geometry");
45 if (geometry) map.addLayer(geometry);
46 $(this).addClass("selected");
48 .on("mouseout", ".query-results li.query-result", function () {
49 var geometry = $(this).data("geometry");
50 if (geometry) map.removeLayer(geometry);
51 $(this).removeClass("selected");
53 .on("mousedown", ".query-results li.query-result", function () {
55 $(this).one("click", function (e) {
57 var geometry = $(this).data("geometry");
58 if (geometry) map.removeLayer(geometry);
60 if (!$(e.target).is("a")) {
61 $(this).find("a").simulate("click", e);
64 }).one("mousemove", function () {
69 function interestingFeature(feature) {
71 for (var key in feature.tags) {
72 if (uninterestingTags.indexOf(key) < 0) {
81 function featurePrefix(feature) {
82 var tags = feature.tags;
85 if (tags.boundary === "administrative" && tags.admin_level) {
86 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
87 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
90 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
97 if (prefixes[key][value]) {
98 return prefixes[key][value];
107 var first = value.slice(0, 1).toUpperCase(),
108 rest = value.slice(1).replace(/_/g, " ");
116 prefix = I18n.t("javascripts.query." + feature.type);
122 function featureName(feature) {
123 var tags = feature.tags,
124 locales = I18n.locales.get();
126 for (var i = 0; i < locales.length; i++) {
127 if (tags["name:" + locales[i]]) {
128 return tags["name:" + locales[i]];
134 } else if (tags.ref) {
136 } else if (tags["addr:housename"]) {
137 return tags["addr:housename"];
138 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
139 return tags["addr:housenumber"] + " " + tags["addr:street"];
141 return "#" + feature.id;
145 function featureGeometry(feature) {
148 if (feature.type === "node" && feature.lat && feature.lon) {
149 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
150 } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
151 geometry = L.polyline(feature.geometry.filter(function (point) {
152 return point !== null;
153 }).map(function (point) {
154 return [point.lat, point.lon];
156 } else if (feature.type === "relation" && feature.members) {
157 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
158 return typeof geometry !== "undefined";
165 function runQuery(latlng, radius, query, $section, merge, compare) {
166 var $ul = $section.find("ul");
171 if ($section.data("ajax")) {
172 $section.data("ajax").abort();
175 $section.data("ajax", $.ajax({
179 data: "[timeout:10][out:json];" + query
182 withCredentials: credentials
184 success: function (results) {
187 $section.find(".loader").hide();
190 elements = results.elements.reduce(function (hash, element) {
191 var key = element.type + element.id;
192 if ("geometry" in element) {
193 delete element.bounds;
195 hash[key] = $.extend({}, hash[key], element);
199 elements = Object.keys(elements).map(function (key) {
200 return elements[key];
203 elements = results.elements;
207 elements = elements.sort(compare);
210 for (var i = 0; i < elements.length; i++) {
211 var element = elements[i];
213 if (interestingFeature(element)) {
215 .addClass("query-result list-group-item")
216 .data("geometry", featureGeometry(element))
217 .text(featurePrefix(element) + " ")
221 .attr("href", "/" + element.type + "/" + element.id)
222 .text(featureName(element))
227 if (results.remark) {
229 .addClass("query-result list-group-item")
230 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
234 if ($ul.find("li").length === 0) {
236 .addClass("query-result list-group-item")
237 .text(I18n.t("javascripts.query.nothing_found"))
241 error: function (xhr, status, error) {
242 $section.find(".loader").hide();
245 .addClass("query-result list-group-item")
246 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
252 function compareSize(feature1, feature2) {
253 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
254 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
255 area1 = width1 * height1,
256 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
257 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
258 area2 = width2 * height2;
260 return area1 - area2;
264 * To find nearby objects we ask overpass for the union of the
267 * node(around:<radius>,<lat>,lng>)
268 * way(around:<radius>,<lat>,lng>)
269 * relation(around:<radius>,<lat>,lng>)
271 * to find enclosing objects we first find all the enclosing areas:
273 * is_in(<lat>,<lng>)->.a
275 * and then return the union of the following sets:
280 * In both cases we then ask to retrieve tags and the geometry
283 function queryOverpass(lat, lng) {
284 var latlng = L.latLng(lat, lng).wrap(),
285 bounds = map.getBounds().wrap(),
286 precision = OSM.zoomPrecision(map.getZoom()),
287 bbox = bounds.getSouth().toFixed(precision) + "," +
288 bounds.getWest().toFixed(precision) + "," +
289 bounds.getNorth().toFixed(precision) + "," +
290 bounds.getEast().toFixed(precision),
291 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
292 around = "around:" + radius + "," + lat + "," + lng,
293 nodes = "node(" + around + ")",
294 ways = "way(" + around + ")",
295 relations = "relation(" + around + ")",
296 nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
297 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
299 $("#sidebar_content .query-intro")
302 if (marker) map.removeLayer(marker);
303 marker = L.circle(latlng, radius, featureStyle).addTo(map);
305 $(document).everyTime(75, "fadeQueryMarker", function (i) {
307 map.removeLayer(marker);
310 opacity: 1 - (i * 0.1),
311 fillOpacity: 0.5 - (i * 0.05)
316 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
317 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
320 function clickHandler(e) {
321 var precision = OSM.zoomPrecision(map.getZoom()),
322 latlng = e.latlng.wrap(),
323 lat = latlng.lat.toFixed(precision),
324 lng = latlng.lng.toFixed(precision);
326 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
329 function enableQueryMode() {
330 queryButton.addClass("active");
331 map.on("click", clickHandler);
332 $(map.getContainer()).addClass("query-active");
335 function disableQueryMode() {
336 if (marker) map.removeLayer(marker);
337 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
338 map.off("click", clickHandler);
339 queryButton.removeClass("active");
344 page.pushstate = page.popstate = function (path) {
345 OSM.loadSidebarContent(path, function () {
346 page.load(path, true);
350 page.load = function (path, noCentre) {
351 var params = Qs.parse(path.substring(path.indexOf("?") + 1)),
352 latlng = L.latLng(params.lat, params.lon);
354 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
355 OSM.router.withoutMoveListener(function () {
356 map.setView(latlng, 15);
360 queryOverpass(params.lat, params.lon);
363 page.unload = function (sameController) {
364 if (!sameController) {