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("click", ".query-results li.query-result", function (e) {
57 var geometry = $(this).data("geometry")
58 if (geometry) map.removeLayer(geometry);
60 if (!$(e.target).is('a')) {
61 $(this).find("a").simulate("click", e);
65 function interestingFeature(feature, origin, radius) {
67 if (feature.type === "node" &&
68 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > radius) {
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") {
87 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
89 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
91 for (var key in tags) {
92 var value = tags[key];
95 if (prefixes[key][value]) {
96 return prefixes[key][value];
98 var first = value.substr(0, 1).toUpperCase(),
99 rest = value.substr(1).replace(/_/g, " ");
108 prefix = I18n.t("javascripts.query." + feature.type);
114 function featureName(feature) {
115 var tags = feature.tags;
119 } else if (tags["ref"]) {
121 } else if (tags["addr:housename"]) {
122 return tags["addr:housename"];
123 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
124 return tags["addr:housenumber"] + " " + tags["addr:street"];
126 return "#" + feature.id;
130 function featureGeometry(feature, features) {
133 if (feature.type === "node") {
134 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
135 } else if (feature.type === "way") {
136 geometry = L.polyline(feature.nodes.map(function (node) {
137 return features["node" + node].getLatLng();
139 } else if (feature.type === "relation") {
140 geometry = L.featureGroup();
142 feature.members.forEach(function (member) {
143 if (features[member.type + member.ref]) {
144 geometry.addLayer(features[member.type + member.ref]);
150 features[feature.type + feature.id] = geometry;
156 function runQuery(latlng, radius, query, $section) {
157 var $ul = $section.find("ul");
162 $section.find(".loader").oneTime(1000, "loading", function () {
166 if ($section.data("ajax")) {
167 $section.data("ajax").abort();
170 $section.data("ajax", $.ajax({
174 data: "[timeout:5][out:json];" + query,
176 success: function(results) {
179 $section.find(".loader").stopTime("loading").hide();
181 for (var i = 0; i < results.elements.length; i++) {
182 var element = results.elements[i],
183 geometry = featureGeometry(element, features);
185 if (interestingFeature(element, latlng, radius)) {
187 .addClass("query-result")
188 .data("geometry", geometry)
191 .text(featurePrefix(element) + " ")
195 .attr("href", "/" + element.type + "/" + element.id)
196 .text(featureName(element))
201 if ($ul.find("li").length == 0) {
203 .text(I18n.t("javascripts.query.nothing_found"))
207 error: function(xhr, status, error) {
208 $section.find(".loader").stopTime("loading").hide();
211 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
218 * To find nearby objects we ask overpass for the union of the
221 * node(around:<radius>,<lat>,lng>)
222 * way(around:<radius>,<lat>,lng>)
224 * relation(around:<radius>,<lat>,lng>)
226 * to find enclosing objects we first find all the enclosing areas:
228 * is_in(<lat>,<lng>)->.a
230 * and then return the union of the following sets:
236 * In order to avoid overly large responses we don't currently
237 * attempt to complete any relations and instead just show those
238 * ways and nodes which are returned for other reasons.
240 function queryOverpass(lat, lng) {
241 var latlng = L.latLng(lat, lng),
242 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
243 around = "around:" + radius + "," + lat + "," + lng,
244 nodes = "node(" + around + ")",
245 ways = "way(" + around + ");node(w)",
246 relations = "relation(" + around + ")",
247 nearby = "(" + nodes + ";" + ways + ";" + relations + ");out;",
248 isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a);node(w));out;";
250 $("#sidebar_content .query-intro")
253 if (marker) map.removeLayer(marker);
254 marker = L.circle(latlng, radius, featureStyle).addTo(map);
256 $(document).everyTime(75, "fadeQueryMarker", function (i) {
258 map.removeLayer(marker);
261 opacity: 1 - i * 0.1,
262 fillOpacity: 0.5 - i * 0.05
267 runQuery(latlng, radius, nearby, $("#query-nearby"));
268 runQuery(latlng, radius, isin, $("#query-isin"));
271 function clickHandler(e) {
272 var precision = OSM.zoomPrecision(map.getZoom()),
273 lat = e.latlng.lat.toFixed(precision),
274 lng = e.latlng.lng.toFixed(precision);
276 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
279 function enableQueryMode() {
280 queryButton.addClass("active");
281 map.on("click", clickHandler);
282 $(map.getContainer()).addClass("query-active");
285 function disableQueryMode() {
286 if (marker) map.removeLayer(marker);
287 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
288 map.off("click", clickHandler);
289 queryButton.removeClass("active");
294 page.pushstate = page.popstate = function(path) {
295 OSM.loadSidebarContent(path, function () {
296 page.load(path, true);
300 page.load = function(path, noCentre) {
301 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
302 latlng = L.latLng(params.lat, params.lon);
304 if (!window.location.hash &&
305 (!noCentre || !map.getBounds().contains(latlng))) {
306 OSM.router.withoutMoveListener(function () {
307 map.setView(latlng, 15);
311 queryOverpass(params.lat, params.lon);
315 page.unload = function() {