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 list-group-item")
218 .data("geometry", featureGeometry(element))
219 .text(featurePrefix(element) + " ")
223 .attr("href", "/" + element.type + "/" + element.id)
224 .text(featureName(element))
229 if (results.remark) {
231 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
235 if ($ul.find("li").length === 0) {
237 .text(I18n.t("javascripts.query.nothing_found"))
241 error: function (xhr, status, error) {
242 $section.find(".loader").stopTime("loading").hide();
245 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
251 function compareSize(feature1, feature2) {
252 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
253 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
254 area1 = width1 * height1,
255 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
256 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
257 area2 = width2 * height2;
259 return area1 - area2;
263 * To find nearby objects we ask overpass for the union of the
266 * node(around:<radius>,<lat>,lng>)
267 * way(around:<radius>,<lat>,lng>)
268 * relation(around:<radius>,<lat>,lng>)
270 * to find enclosing objects we first find all the enclosing areas:
272 * is_in(<lat>,<lng>)->.a
274 * and then return the union of the following sets:
279 * In both cases we then ask to retrieve tags and the geometry
282 function queryOverpass(lat, lng) {
283 var latlng = L.latLng(lat, lng).wrap(),
284 bounds = map.getBounds().wrap(),
285 precision = OSM.zoomPrecision(map.getZoom()),
286 bbox = bounds.getSouth().toFixed(precision) + "," +
287 bounds.getWest().toFixed(precision) + "," +
288 bounds.getNorth().toFixed(precision) + "," +
289 bounds.getEast().toFixed(precision),
290 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
291 around = "around:" + radius + "," + lat + "," + lng,
292 nodes = "node(" + around + ")",
293 ways = "way(" + around + ")",
294 relations = "relation(" + around + ")",
295 nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
296 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
298 $("#sidebar_content .query-intro")
301 if (marker) map.removeLayer(marker);
302 marker = L.circle(latlng, radius, featureStyle).addTo(map);
304 $(document).everyTime(75, "fadeQueryMarker", function (i) {
306 map.removeLayer(marker);
309 opacity: 1 - (i * 0.1),
310 fillOpacity: 0.5 - (i * 0.05)
315 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
316 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
319 function clickHandler(e) {
320 var precision = OSM.zoomPrecision(map.getZoom()),
321 latlng = e.latlng.wrap(),
322 lat = latlng.lat.toFixed(precision),
323 lng = latlng.lng.toFixed(precision);
325 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
328 function enableQueryMode() {
329 queryButton.addClass("active");
330 map.on("click", clickHandler);
331 $(map.getContainer()).addClass("query-active");
334 function disableQueryMode() {
335 if (marker) map.removeLayer(marker);
336 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
337 map.off("click", clickHandler);
338 queryButton.removeClass("active");
343 page.pushstate = page.popstate = function (path) {
344 OSM.loadSidebarContent(path, function () {
345 page.load(path, true);
349 page.load = function (path, noCentre) {
350 var params = querystring.parse(path.substring(path.indexOf("?") + 1)),
351 latlng = L.latLng(params.lat, params.lon);
353 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
354 OSM.router.withoutMoveListener(function () {
355 map.setView(latlng, 15);
359 queryOverpass(params.lat, params.lon);
362 page.unload = function (sameController) {
363 if (!sameController) {