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")) {
29 }).on("disabled", function (e) {
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 (e) {
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 (e) {
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, origin, 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) {
133 if (feature.type === "node" && feature.lat && feature.lon) {
134 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
135 } else if (feature.type === "way" && feature.geometry) {
136 geometry = L.polyline(feature.geometry.filter(function (point) {
137 return point !== null;
138 }).map(function (point) {
139 return [point.lat, point.lon];
141 } else if (feature.type === "relation" && feature.members) {
142 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
143 return geometry !== undefined;
150 function runQuery(latlng, radius, query, $section, compare) {
151 var $ul = $section.find("ul");
156 $section.find(".loader").oneTime(1000, "loading", function () {
160 if ($section.data("ajax")) {
161 $section.data("ajax").abort();
164 $section.data("ajax", $.ajax({
168 data: "[timeout:5][out:json];" + query,
170 success: function(results) {
173 $section.find(".loader").stopTime("loading").hide();
176 elements = results.elements.sort(compare);
178 elements = results.elements;
181 for (var i = 0; i < elements.length; i++) {
182 var element = elements[i];
184 if (interestingFeature(element, latlng, radius)) {
186 .addClass("query-result")
187 .data("geometry", featureGeometry(element))
190 .text(featurePrefix(element) + " ")
194 .attr("href", "/" + element.type + "/" + element.id)
195 .text(featureName(element))
200 if ($ul.find("li").length == 0) {
202 .text(I18n.t("javascripts.query.nothing_found"))
206 error: function(xhr, status, error) {
207 $section.find(".loader").stopTime("loading").hide();
210 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
216 function compareSize(feature1, feature2) {
217 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
218 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
219 area1 = width1 * height1,
220 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
221 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
222 area2 = width2 * height2;
224 return area1 - area2;
228 * To find nearby objects we ask overpass for the union of the
231 * node(around:<radius>,<lat>,lng>)
232 * way(around:<radius>,<lat>,lng>)
233 * relation(around:<radius>,<lat>,lng>)
235 * to find enclosing objects we first find all the enclosing areas:
237 * is_in(<lat>,<lng>)->.a
239 * and then return the union of the following sets:
244 * In both cases we then ask to retrieve tags and the geometry
247 function queryOverpass(lat, lng) {
248 var latlng = L.latLng(lat, lng),
249 bounds = map.getBounds(),
250 bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
251 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
252 around = "around:" + radius + "," + lat + "," + lng,
253 nodes = "node(" + around + ")",
254 ways = "way(" + around + ")",
255 relations = "relation(" + around + ")",
256 nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
257 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags geom(" + bbox + ");relation(pivot.a);out tags bb;";
259 $("#sidebar_content .query-intro")
262 if (marker) map.removeLayer(marker);
263 marker = L.circle(latlng, radius, featureStyle).addTo(map);
265 $(document).everyTime(75, "fadeQueryMarker", function (i) {
267 map.removeLayer(marker);
270 opacity: 1 - i * 0.1,
271 fillOpacity: 0.5 - i * 0.05
276 runQuery(latlng, radius, nearby, $("#query-nearby"));
277 runQuery(latlng, radius, isin, $("#query-isin"), compareSize);
280 function clickHandler(e) {
281 var precision = OSM.zoomPrecision(map.getZoom()),
282 lat = e.latlng.lat.toFixed(precision),
283 lng = e.latlng.lng.toFixed(precision);
285 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
288 function enableQueryMode() {
289 queryButton.addClass("active");
290 map.on("click", clickHandler);
291 $(map.getContainer()).addClass("query-active");
294 function disableQueryMode() {
295 if (marker) map.removeLayer(marker);
296 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
297 map.off("click", clickHandler);
298 queryButton.removeClass("active");
303 page.pushstate = page.popstate = function(path) {
304 OSM.loadSidebarContent(path, function () {
305 page.load(path, true);
309 page.load = function(path, noCentre) {
310 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
311 latlng = L.latLng(params.lat, params.lon);
313 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
314 OSM.router.withoutMoveListener(function () {
315 map.setView(latlng, 15);
319 queryOverpass(params.lat, params.lon);
322 page.unload = function(sameController) {
323 if (!sameController) {