1 //= require jquery.simulate
2 //= require querystring
4 OSM.Query = function (map) {
5 var querystring = require("querystring-component");
7 var url = OSM.OVERPASS_URL,
8 queryButton = $(".control-query .control-button"),
9 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"],
20 queryButton.on("click", function (e) {
24 if (queryButton.hasClass("active")) {
26 } else if (!queryButton.hasClass("disabled")) {
29 }).on("disabled", function () {
30 if (queryButton.hasClass("active")) {
31 map.off("click", clickHandler);
32 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
33 $(this).tooltip("show");
35 }).on("enabled", function () {
36 if (queryButton.hasClass("active")) {
37 map.on("click", clickHandler);
38 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
39 $(this).tooltip("hide");
44 .on("mouseover", ".query-results li.query-result", function () {
45 var geometry = $(this).data("geometry");
46 if (geometry) map.addLayer(geometry);
47 $(this).addClass("selected");
49 .on("mouseout", ".query-results li.query-result", function () {
50 var geometry = $(this).data("geometry");
51 if (geometry) map.removeLayer(geometry);
52 $(this).removeClass("selected");
54 .on("mousedown", ".query-results li.query-result", function () {
56 $(this).one("click", function (e) {
58 var geometry = $(this).data("geometry");
59 if (geometry) map.removeLayer(geometry);
61 if (!$(e.target).is("a")) {
62 $(this).find("a").simulate("click", e);
65 }).one("mousemove", function () {
70 function interestingFeature(feature) {
72 for (var key in feature.tags) {
73 if (uninterestingTags.indexOf(key) < 0) {
82 function featurePrefix(feature) {
83 var tags = feature.tags;
86 if (tags.boundary === "administrative" && tags.admin_level) {
87 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
88 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
91 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
98 if (prefixes[key][value]) {
99 return prefixes[key][value];
108 var first = value.substr(0, 1).toUpperCase(),
109 rest = value.substr(1).replace(/_/g, " ");
117 prefix = I18n.t("javascripts.query." + feature.type);
123 function featureName(feature) {
124 var tags = feature.tags,
125 locales = I18n.locales.get();
127 for (var i = 0; i < locales.length; i++) {
128 if (tags["name:" + locales[i]]) {
129 return tags["name:" + locales[i]];
135 } else if (tags.ref) {
137 } else if (tags["addr:housename"]) {
138 return tags["addr:housename"];
139 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
140 return tags["addr:housenumber"] + " " + tags["addr:street"];
142 return "#" + feature.id;
146 function featureGeometry(feature) {
149 if (feature.type === "node" && feature.lat && feature.lon) {
150 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
151 } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
152 geometry = L.polyline(feature.geometry.filter(function (point) {
153 return point !== null;
154 }).map(function (point) {
155 return [point.lat, point.lon];
157 } else if (feature.type === "relation" && feature.members) {
158 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
159 return typeof geometry !== "undefined";
166 function runQuery(latlng, radius, query, $section, merge, compare) {
167 var $ul = $section.find("ul");
172 $section.find(".loader").oneTime(1000, "loading", function () {
176 if ($section.data("ajax")) {
177 $section.data("ajax").abort();
180 $section.data("ajax", $.ajax({
184 data: "[timeout:10][out:json];" + query
186 success: function (results) {
189 $section.find(".loader").stopTime("loading").hide();
192 elements = results.elements.reduce(function (hash, element) {
193 var key = element.type + element.id;
194 if ("geometry" in element) {
195 delete element.bounds;
197 hash[key] = $.extend({}, hash[key], element);
201 elements = Object.keys(elements).map(function (key) {
202 return elements[key];
205 elements = results.elements;
209 elements = elements.sort(compare);
212 for (var i = 0; i < elements.length; i++) {
213 var element = elements[i];
215 if (interestingFeature(element)) {
217 .addClass("query-result")
218 .data("geometry", featureGeometry(element))
221 .text(featurePrefix(element) + " ")
225 .attr("href", "/" + element.type + "/" + element.id)
226 .text(featureName(element))
231 if (results.remark) {
233 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
237 if ($ul.find("li").length === 0) {
239 .text(I18n.t("javascripts.query.nothing_found"))
243 error: function (xhr, status, error) {
244 $section.find(".loader").stopTime("loading").hide();
247 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
253 function compareSize(feature1, feature2) {
254 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
255 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
256 area1 = width1 * height1,
257 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
258 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
259 area2 = width2 * height2;
261 return area1 - area2;
265 * To find nearby objects we ask overpass for the union of the
268 * node(around:<radius>,<lat>,lng>)
269 * way(around:<radius>,<lat>,lng>)
270 * relation(around:<radius>,<lat>,lng>)
272 * to find enclosing objects we first find all the enclosing areas:
274 * is_in(<lat>,<lng>)->.a
276 * and then return the union of the following sets:
281 * In both cases we then ask to retrieve tags and the geometry
284 function queryOverpass(lat, lng) {
285 var latlng = L.latLng(lat, lng).wrap(),
286 bounds = map.getBounds().wrap(),
287 precision = OSM.zoomPrecision(map.getZoom()),
288 bbox = bounds.getSouth().toFixed(precision) + "," +
289 bounds.getWest().toFixed(precision) + "," +
290 bounds.getNorth().toFixed(precision) + "," +
291 bounds.getEast().toFixed(precision),
292 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
293 around = "around:" + radius + "," + lat + "," + lng,
294 nodes = "node(" + around + ")",
295 ways = "way(" + around + ")",
296 relations = "relation(" + around + ")",
297 nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
298 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
300 $("#sidebar_content .query-intro")
303 if (marker) map.removeLayer(marker);
304 marker = L.circle(latlng, radius, featureStyle).addTo(map);
306 $(document).everyTime(75, "fadeQueryMarker", function (i) {
308 map.removeLayer(marker);
311 opacity: 1 - (i * 0.1),
312 fillOpacity: 0.5 - (i * 0.05)
317 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
318 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
321 function clickHandler(e) {
322 var precision = OSM.zoomPrecision(map.getZoom()),
323 latlng = e.latlng.wrap(),
324 lat = latlng.lat.toFixed(precision),
325 lng = latlng.lng.toFixed(precision);
327 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
330 function enableQueryMode() {
331 queryButton.addClass("active");
332 map.on("click", clickHandler);
333 $(map.getContainer()).addClass("query-active");
336 function disableQueryMode() {
337 if (marker) map.removeLayer(marker);
338 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
339 map.off("click", clickHandler);
340 queryButton.removeClass("active");
345 page.pushstate = page.popstate = function (path) {
346 OSM.loadSidebarContent(path, function () {
347 page.load(path, true);
351 page.load = function (path, noCentre) {
352 var params = querystring.parse(path.substring(path.indexOf("?") + 1)),
353 latlng = L.latLng(params.lat, params.lon);
355 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
356 OSM.router.withoutMoveListener(function () {
357 map.setView(latlng, 15);
361 queryOverpass(params.lat, params.lon);
364 page.unload = function (sameController) {
365 if (!sameController) {