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 if (feature.type === "node" &&
75 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > radius) {
79 for (var key in feature.tags) {
80 if (uninterestingTags.indexOf(key) < 0) {
89 function featurePrefix(feature) {
90 var tags = feature.tags;
93 if (tags.boundary === "administrative") {
94 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
96 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
98 for (var key in tags) {
99 var value = tags[key];
102 if (prefixes[key][value]) {
103 return prefixes[key][value];
105 var first = value.substr(0, 1).toUpperCase(),
106 rest = value.substr(1).replace(/_/g, " ");
115 prefix = I18n.t("javascripts.query." + feature.type);
121 function featureName(feature) {
122 var tags = feature.tags;
126 } else if (tags["ref"]) {
128 } else if (tags["addr:housename"]) {
129 return tags["addr:housename"];
130 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
131 return tags["addr:housenumber"] + " " + tags["addr:street"];
133 return "#" + feature.id;
137 function featureGeometry(feature, features) {
140 if (feature.type === "node") {
141 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
142 } else if (feature.type === "way") {
143 geometry = L.polyline(feature.nodes.map(function (node) {
144 return features["node" + node].getLatLng();
146 } else if (feature.type === "relation") {
147 geometry = L.featureGroup();
149 feature.members.forEach(function (member) {
150 if (features[member.type + member.ref]) {
151 geometry.addLayer(features[member.type + member.ref]);
157 features[feature.type + feature.id] = geometry;
163 function runQuery(latlng, radius, query, $section) {
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:5][out:json];" + query,
183 success: function(results) {
186 $section.find(".loader").stopTime("loading").hide();
188 for (var i = 0; i < results.elements.length; i++) {
189 var element = results.elements[i],
190 geometry = featureGeometry(element, features);
192 if (interestingFeature(element, latlng, radius)) {
194 .addClass("query-result")
195 .data("geometry", geometry)
198 .text(featurePrefix(element) + " ")
202 .attr("href", "/" + element.type + "/" + element.id)
203 .text(featureName(element))
208 if ($ul.find("li").length == 0) {
210 .text(I18n.t("javascripts.query.nothing_found"))
214 error: function(xhr, status, error) {
215 $section.find(".loader").stopTime("loading").hide();
218 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
225 * To find nearby objects we ask overpass for the union of the
228 * node(around:<radius>,<lat>,lng>)
229 * way(around:<radius>,<lat>,lng>)
231 * relation(around:<radius>,<lat>,lng>)
233 * to find enclosing objects we first find all the enclosing areas:
235 * is_in(<lat>,<lng>)->.a
237 * and then return the union of the following sets:
243 * In order to avoid overly large responses we don't currently
244 * attempt to complete any relations and instead just show those
245 * ways and nodes which are returned for other reasons.
247 function queryOverpass(lat, lng) {
248 var latlng = L.latLng(lat, lng),
249 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
250 around = "around:" + radius + "," + lat + "," + lng,
251 nodes = "node(" + around + ")",
252 ways = "way(" + around + ");node(w)",
253 relations = "relation(" + around + ")",
254 nearby = "(" + nodes + ";" + ways + ";" + relations + ");out;",
255 isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a);node(w));out;";
257 $("#sidebar_content .query-intro")
260 if (marker) map.removeLayer(marker);
261 marker = L.circle(latlng, radius, featureStyle).addTo(map);
263 $(document).everyTime(75, "fadeQueryMarker", function (i) {
265 map.removeLayer(marker);
268 opacity: 1 - i * 0.1,
269 fillOpacity: 0.5 - i * 0.05
274 runQuery(latlng, radius, nearby, $("#query-nearby"));
275 runQuery(latlng, radius, isin, $("#query-isin"));
278 function clickHandler(e) {
279 var precision = OSM.zoomPrecision(map.getZoom()),
280 lat = e.latlng.lat.toFixed(precision),
281 lng = e.latlng.lng.toFixed(precision);
283 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
286 function enableQueryMode() {
287 queryButton.addClass("active");
288 map.on("click", clickHandler);
289 $(map.getContainer()).addClass("query-active");
292 function disableQueryMode() {
293 if (marker) map.removeLayer(marker);
294 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
295 map.off("click", clickHandler);
296 queryButton.removeClass("active");
301 page.pushstate = page.popstate = function(path) {
302 OSM.loadSidebarContent(path, function () {
303 page.load(path, true);
307 page.load = function(path, noCentre) {
308 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
309 latlng = L.latLng(params.lat, params.lon);
311 if (!window.location.hash &&
312 (!noCentre || !map.getBounds().contains(latlng))) {
313 OSM.router.withoutMoveListener(function () {
314 map.setView(latlng, 15);
318 queryOverpass(params.lat, params.lon);
322 page.unload = function() {