1 //= require jquery.simulate
3 OSM.Query = function(map) {
4 var protocol = document.location.protocol === "https:" ? "https:" : "http:",
5 url = protocol + OSM.OVERPASS_URL,
6 queryButton = $(".control-query .control-button"),
7 uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
18 queryButton.on("click", function (e) {
22 if (queryButton.hasClass("disabled")) return;
24 if (queryButton.hasClass("active")) {
25 if ($("#content").hasClass("overlay-sidebar")) {
31 }).on("disabled", function (e) {
32 if (queryButton.hasClass("active")) {
33 map.off("click", clickHandler);
34 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
35 $(this).tooltip("show");
37 }).on("enabled", function (e) {
38 if (queryButton.hasClass("active")) {
39 map.on("click", clickHandler);
40 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
41 $(this).tooltip("hide");
46 .on("mouseover", ".query-results li.query-result", function () {
47 var geometry = $(this).data("geometry")
48 if (geometry) map.addLayer(geometry);
49 $(this).addClass("selected");
51 .on("mouseout", ".query-results li.query-result", function () {
52 var geometry = $(this).data("geometry")
53 if (geometry) map.removeLayer(geometry);
54 $(this).removeClass("selected");
56 .on("mousedown", ".query-results li.query-result", function (e) {
58 $(this).one("click", function (e) {
60 var geometry = $(this).data("geometry")
61 if (geometry) map.removeLayer(geometry);
63 if (!$(e.target).is('a')) {
64 $(this).find("a").simulate("click", e);
67 }).one("mousemove", function () {
72 function interestingFeature(feature, origin, radius) {
74 for (var key in feature.tags) {
75 if (uninterestingTags.indexOf(key) < 0) {
84 function featurePrefix(feature) {
85 var tags = feature.tags;
88 if (tags.boundary === "administrative") {
89 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
91 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
93 for (var key in tags) {
94 var value = tags[key];
97 if (prefixes[key][value]) {
98 return prefixes[key][value];
100 var first = value.substr(0, 1).toUpperCase(),
101 rest = value.substr(1).replace(/_/g, " ");
110 prefix = I18n.t("javascripts.query." + feature.type);
116 function featureName(feature) {
117 var tags = feature.tags;
121 } else if (tags["ref"]) {
123 } else if (tags["addr:housename"]) {
124 return tags["addr:housename"];
125 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
126 return tags["addr:housenumber"] + " " + tags["addr:street"];
128 return "#" + feature.id;
132 function featureGeometry(feature) {
135 if (feature.type === "node" && feature.lat && feature.lon) {
136 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
137 } else if (feature.type === "way" && feature.geometry) {
138 geometry = L.polyline(feature.geometry.filter(function (point) {
139 return point !== null;
140 }).map(function (point) {
141 return [point.lat, point.lon];
143 } else if (feature.type === "relation" && feature.members) {
144 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
145 return geometry !== undefined;
152 function runQuery(latlng, radius, query, $section) {
153 var $ul = $section.find("ul");
158 $section.find(".loader").oneTime(1000, "loading", function () {
162 if ($section.data("ajax")) {
163 $section.data("ajax").abort();
166 $section.data("ajax", $.ajax({
170 data: "[timeout:5][out:json];" + query,
172 success: function(results) {
173 $section.find(".loader").stopTime("loading").hide();
175 for (var i = 0; i < results.elements.length; i++) {
176 var element = results.elements[i];
178 if (interestingFeature(element, latlng, radius)) {
180 .addClass("query-result")
181 .data("geometry", featureGeometry(element))
184 .text(featurePrefix(element) + " ")
188 .attr("href", "/" + element.type + "/" + element.id)
189 .text(featureName(element))
194 if ($ul.find("li").length == 0) {
196 .text(I18n.t("javascripts.query.nothing_found"))
200 error: function(xhr, status, error) {
201 $section.find(".loader").stopTime("loading").hide();
204 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
211 * To find nearby objects we ask overpass for the union of the
214 * node(around:<radius>,<lat>,lng>)
215 * way(around:<radius>,<lat>,lng>)
216 * relation(around:<radius>,<lat>,lng>)
218 * to find enclosing objects we first find all the enclosing areas:
220 * is_in(<lat>,<lng>)->.a
222 * and then return the union of the following sets:
227 * In both cases we then ask to retrieve tags and the geometry
230 function queryOverpass(lat, lng) {
231 var latlng = L.latLng(lat, lng),
232 bounds = map.getBounds(),
233 bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
234 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
235 around = "around:" + radius + "," + lat + "," + lng,
236 nodes = "node(" + around + ")",
237 ways = "way(" + around + ")",
238 relations = "relation(" + around + ")",
239 nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
240 isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a));out geom(" + bbox + ");";
242 $("#sidebar_content .query-intro")
245 if (marker) map.removeLayer(marker);
246 marker = L.circle(latlng, radius, featureStyle).addTo(map);
248 $(document).everyTime(75, "fadeQueryMarker", function (i) {
250 map.removeLayer(marker);
253 opacity: 1 - i * 0.1,
254 fillOpacity: 0.5 - i * 0.05
259 runQuery(latlng, radius, nearby, $("#query-nearby"));
260 runQuery(latlng, radius, isin, $("#query-isin"));
263 function clickHandler(e) {
264 var precision = OSM.zoomPrecision(map.getZoom()),
265 lat = e.latlng.lat.toFixed(precision),
266 lng = e.latlng.lng.toFixed(precision);
268 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
271 function enableQueryMode() {
272 queryButton.addClass("active");
273 map.on("click", clickHandler);
274 $(map.getContainer()).addClass("query-active");
277 function disableQueryMode() {
278 if (marker) map.removeLayer(marker);
279 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
280 map.off("click", clickHandler);
281 queryButton.removeClass("active");
286 page.pushstate = page.popstate = function(path) {
287 OSM.loadSidebarContent(path, function () {
288 page.load(path, true);
292 page.load = function(path, noCentre) {
293 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
294 latlng = L.latLng(params.lat, params.lon);
296 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
297 OSM.router.withoutMoveListener(function () {
298 map.setView(latlng, 15);
302 queryOverpass(params.lat, params.lon);
306 page.unload = function() {