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")) {
27 OSM.router.route("/");
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 if (!$(e.target).is('a')) {
58 $(this).find("a").simulate("click", e);
62 function interestingFeature(feature, origin, radius) {
64 if (feature.type === "node" &&
65 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > radius) {
69 for (var key in feature.tags) {
70 if (uninterestingTags.indexOf(key) < 0) {
79 function featurePrefix(feature) {
80 var tags = feature.tags;
83 if (tags.boundary === "administrative") {
84 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
86 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
88 for (var key in tags) {
89 var value = tags[key];
92 if (prefixes[key][value]) {
93 return prefixes[key][value];
95 var first = value.substr(0, 1).toUpperCase(),
96 rest = value.substr(1).replace(/_/g, " ");
105 prefix = I18n.t("javascripts.query." + feature.type);
111 function featureName(feature) {
112 var tags = feature.tags;
116 } else if (tags["ref"]) {
118 } else if (tags["addr:housename"]) {
119 return tags["addr:housename"];
120 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
121 return tags["addr:housenumber"] + " " + tags["addr:street"];
123 return "#" + feature.id;
127 function featureGeometry(feature, features) {
130 if (feature.type === "node") {
131 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
132 } else if (feature.type === "way") {
133 geometry = L.polyline(feature.nodes.map(function (node) {
134 return features["node" + node].getLatLng();
136 } else if (feature.type === "relation") {
137 geometry = L.featureGroup();
139 feature.members.forEach(function (member) {
140 if (features[member.type + member.ref]) {
141 geometry.addLayer(features[member.type + member.ref]);
147 features[feature.type + feature.id] = geometry;
153 function runQuery(latlng, radius, query, $section) {
154 var $ul = $section.find("ul");
159 $section.find(".loader").oneTime(1000, "loading", function () {
163 if ($section.data("ajax")) {
164 $section.data("ajax").abort();
167 $section.data("ajax", $.ajax({
171 data: "[timeout:5][out:json];" + query,
173 success: function(results) {
176 $section.find(".loader").stopTime("loading").hide();
178 for (var i = 0; i < results.elements.length; i++) {
179 var element = results.elements[i],
180 geometry = featureGeometry(element, features);
182 if (interestingFeature(element, latlng, radius)) {
184 .addClass("query-result")
185 .data("geometry", geometry)
188 .text(featurePrefix(element) + " ")
192 .attr("href", "/" + element.type + "/" + element.id)
193 .text(featureName(element))
198 if ($ul.find("li").length == 0) {
200 .text(I18n.t("javascripts.query.nothing_found"))
204 error: function(xhr, status, error) {
205 $section.find(".loader").stopTime("loading").hide();
208 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
215 * To find nearby objects we ask overpass for the union of the
218 * node(around:<radius>,<lat>,lng>)
219 * way(around:<radius>,<lat>,lng>)
221 * relation(around:<radius>,<lat>,lng>)
223 * to find enclosing objects we first find all the enclosing areas:
225 * is_in(<lat>,<lng>)->.a
227 * and then return the union of the following sets:
233 * In order to avoid overly large responses we don't currently
234 * attempt to complete any relations and instead just show those
235 * ways and nodes which are returned for other reasons.
237 function queryOverpass(lat, lng) {
238 var latlng = L.latLng(lat, lng),
239 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
240 around = "around:" + radius + "," + lat + "," + lng,
241 nodes = "node(" + around + ")",
242 ways = "way(" + around + ");node(w)",
243 relations = "relation(" + around + ")",
244 nearby = "(" + nodes + ";" + ways + ";" + relations + ");out;",
245 isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a);node(w));out;";
247 $("#sidebar_content .query-intro")
250 if (marker) map.removeLayer(marker);
251 marker = L.circle(latlng, radius, featureStyle).addTo(map);
253 $(document).everyTime(75, "fadeQueryMarker", function (i) {
255 map.removeLayer(marker);
258 opacity: 1 - i * 0.1,
259 fillOpacity: 0.5 - i * 0.05
264 runQuery(latlng, radius, nearby, $("#query-nearby"));
265 runQuery(latlng, radius, isin, $("#query-isin"));
268 function clickHandler(e) {
269 var precision = OSM.zoomPrecision(map.getZoom()),
270 lat = e.latlng.lat.toFixed(precision),
271 lng = e.latlng.lng.toFixed(precision);
273 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
276 function enableQueryMode() {
277 queryButton.addClass("active");
278 map.on("click", clickHandler);
279 $(map.getContainer()).addClass("query-active");
282 function disableQueryMode() {
283 if (marker) map.removeLayer(marker);
284 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
285 map.off("click", clickHandler);
286 queryButton.removeClass("active");
291 page.pushstate = page.popstate = function(path) {
292 OSM.loadSidebarContent(path, function () {
293 page.load(path, true);
297 page.load = function(path, noCentre) {
298 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
299 latlng = L.latLng(params.lat, params.lon);
301 if (!window.location.hash &&
302 (!noCentre || !map.getBounds().contains(latlng))) {
303 OSM.router.withoutMoveListener(function () {
304 map.setView(latlng, 15);
308 queryOverpass(params.lat, params.lon);
312 page.unload = function() {