1 //= require jquery.simulate
3 OSM.Query = function(map) {
4 var url = OSM.OVERPASS_URL,
5 queryButton = $(".control-query .control-button"),
6 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'],
17 queryButton.on("click", function (e) {
21 if (queryButton.hasClass("active")) {
23 } else if (!queryButton.hasClass("disabled")) {
26 }).on("disabled", function () {
27 if (queryButton.hasClass("active")) {
28 map.off("click", clickHandler);
29 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
30 $(this).tooltip("show");
32 }).on("enabled", function () {
33 if (queryButton.hasClass("active")) {
34 map.on("click", clickHandler);
35 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
36 $(this).tooltip("hide");
41 .on("mouseover", ".query-results li.query-result", function () {
42 var geometry = $(this).data("geometry");
43 if (geometry) map.addLayer(geometry);
44 $(this).addClass("selected");
46 .on("mouseout", ".query-results li.query-result", function () {
47 var geometry = $(this).data("geometry");
48 if (geometry) map.removeLayer(geometry);
49 $(this).removeClass("selected");
51 .on("mousedown", ".query-results li.query-result", function () {
53 $(this).one("click", function (e) {
55 var geometry = $(this).data("geometry");
56 if (geometry) map.removeLayer(geometry);
58 if (!$(e.target).is('a')) {
59 $(this).find("a").simulate("click", e);
62 }).one("mousemove", function () {
67 function interestingFeature(feature) {
69 for (var key in feature.tags) {
70 if (uninterestingTags.indexOf(key) < 0) {
79 function featurePrefix(feature) {
80 var tags = feature.tags;
83 if (tags.boundary === "administrative" && tags.admin_level) {
84 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
85 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
88 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
95 if (prefixes[key][value]) {
96 return prefixes[key][value];
105 var first = value.substr(0, 1).toUpperCase(),
106 rest = value.substr(1).replace(/_/g, " ");
114 prefix = I18n.t("javascripts.query." + feature.type);
120 function featureName(feature) {
121 var tags = feature.tags,
122 locales = I18n.locales.get();
124 for (var i = 0; i < locales.length; i++) {
125 if (tags["name:" + locales[i]]) {
126 return tags["name:" + locales[i]];
132 } else if (tags.ref) {
134 } else if (tags["addr:housename"]) {
135 return tags["addr:housename"];
136 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
137 return tags["addr:housenumber"] + " " + tags["addr:street"];
139 return "#" + feature.id;
143 function featureGeometry(feature) {
146 if (feature.type === "node" && feature.lat && feature.lon) {
147 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
148 } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
149 geometry = L.polyline(feature.geometry.filter(function (point) {
150 return point !== null;
151 }).map(function (point) {
152 return [point.lat, point.lon];
154 } else if (feature.type === "relation" && feature.members) {
155 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
156 return geometry !== undefined;
163 function runQuery(latlng, radius, query, $section, merge, compare) {
164 var $ul = $section.find("ul");
169 $section.find(".loader").oneTime(1000, "loading", function () {
173 if ($section.data("ajax")) {
174 $section.data("ajax").abort();
177 $section.data("ajax", $.ajax({
181 data: "[timeout:10][out:json];" + query,
183 success: function(results) {
186 $section.find(".loader").stopTime("loading").hide();
189 elements = results.elements.reduce(function (hash, element) {
190 var key = element.type + element.id;
191 if ("geometry" in element) {
192 delete element.bounds;
194 hash[key] = $.extend({}, hash[key], element);
198 elements = Object.keys(elements).map(function (key) {
199 return elements[key];
202 elements = results.elements;
206 elements = elements.sort(compare);
209 for (var i = 0; i < elements.length; i++) {
210 var element = elements[i];
212 if (interestingFeature(element)) {
214 .addClass("query-result")
215 .data("geometry", featureGeometry(element))
218 .text(featurePrefix(element) + " ")
222 .attr("href", "/" + element.type + "/" + element.id)
223 .text(featureName(element))
228 if (results.remark) {
230 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
234 if ($ul.find("li").length === 0) {
236 .text(I18n.t("javascripts.query.nothing_found"))
240 error: function(xhr, status, error) {
241 $section.find(".loader").stopTime("loading").hide();
244 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
250 function compareSize(feature1, feature2) {
251 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
252 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
253 area1 = width1 * height1,
254 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
255 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
256 area2 = width2 * height2;
258 return area1 - area2;
262 * To find nearby objects we ask overpass for the union of the
265 * node(around:<radius>,<lat>,lng>)
266 * way(around:<radius>,<lat>,lng>)
267 * relation(around:<radius>,<lat>,lng>)
269 * to find enclosing objects we first find all the enclosing areas:
271 * is_in(<lat>,<lng>)->.a
273 * and then return the union of the following sets:
278 * In both cases we then ask to retrieve tags and the geometry
281 function queryOverpass(lat, lng) {
282 var latlng = L.latLng(lat, lng).wrap(),
283 bounds = map.getBounds().wrap(),
284 bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
285 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
286 around = "around:" + radius + "," + lat + "," + lng,
287 nodes = "node(" + around + ")",
288 ways = "way(" + around + ")",
289 relations = "relation(" + around + ")",
290 nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
291 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
293 $("#sidebar_content .query-intro")
296 if (marker) map.removeLayer(marker);
297 marker = L.circle(latlng, radius, featureStyle).addTo(map);
299 $(document).everyTime(75, "fadeQueryMarker", function (i) {
301 map.removeLayer(marker);
304 opacity: 1 - i * 0.1,
305 fillOpacity: 0.5 - i * 0.05
310 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
311 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
314 function clickHandler(e) {
315 var precision = OSM.zoomPrecision(map.getZoom()),
316 latlng = e.latlng.wrap(),
317 lat = latlng.lat.toFixed(precision),
318 lng = latlng.lng.toFixed(precision);
320 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
323 function enableQueryMode() {
324 queryButton.addClass("active");
325 map.on("click", clickHandler);
326 $(map.getContainer()).addClass("query-active");
329 function disableQueryMode() {
330 if (marker) map.removeLayer(marker);
331 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
332 map.off("click", clickHandler);
333 queryButton.removeClass("active");
338 page.pushstate = page.popstate = function(path) {
339 OSM.loadSidebarContent(path, function () {
340 page.load(path, true);
344 page.load = function(path, noCentre) {
345 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
346 latlng = L.latLng(params.lat, params.lon);
348 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
349 OSM.router.withoutMoveListener(function () {
350 map.setView(latlng, 15);
354 queryOverpass(params.lat, params.lon);
357 page.unload = function(sameController) {
358 if (!sameController) {