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', 'KSJ2:curve_id', 'KSJ2:lat', 'KSJ2:lon', 'KSJ2:coordinate', 'KSJ2:filename', 'note:ja'],
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" && tags.admin_level) {
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,
116 locales = I18n.locales.get();
118 for (var i = 0; i < locales.length; i++) {
119 if (tags["name:" + locales[i]]) {
120 return tags["name:" + locales[i]];
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) {
140 if (feature.type === "node" && feature.lat && feature.lon) {
141 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
142 } else if (feature.type === "way" && feature.geometry) {
143 geometry = L.polyline(feature.geometry.filter(function (point) {
144 return point !== null;
145 }).map(function (point) {
146 return [point.lat, point.lon];
148 } else if (feature.type === "relation" && feature.members) {
149 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
150 return geometry !== undefined;
157 function runQuery(latlng, radius, query, $section, compare) {
158 var $ul = $section.find("ul");
163 $section.find(".loader").oneTime(1000, "loading", function () {
167 if ($section.data("ajax")) {
168 $section.data("ajax").abort();
171 $section.data("ajax", $.ajax({
175 data: "[timeout:5][out:json];" + query,
177 success: function(results) {
180 $section.find(".loader").stopTime("loading").hide();
183 elements = results.elements.sort(compare);
185 elements = results.elements;
188 for (var i = 0; i < elements.length; i++) {
189 var element = elements[i];
191 if (interestingFeature(element, latlng, radius)) {
193 .addClass("query-result")
194 .data("geometry", featureGeometry(element))
197 .text(featurePrefix(element) + " ")
201 .attr("href", "/" + element.type + "/" + element.id)
202 .text(featureName(element))
207 if ($ul.find("li").length == 0) {
209 .text(I18n.t("javascripts.query.nothing_found"))
213 error: function(xhr, status, error) {
214 $section.find(".loader").stopTime("loading").hide();
217 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
223 function compareSize(feature1, feature2) {
224 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
225 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
226 area1 = width1 * height1,
227 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
228 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
229 area2 = width2 * height2;
231 return area1 - area2;
235 * To find nearby objects we ask overpass for the union of the
238 * node(around:<radius>,<lat>,lng>)
239 * way(around:<radius>,<lat>,lng>)
240 * relation(around:<radius>,<lat>,lng>)
242 * to find enclosing objects we first find all the enclosing areas:
244 * is_in(<lat>,<lng>)->.a
246 * and then return the union of the following sets:
251 * In both cases we then ask to retrieve tags and the geometry
254 function queryOverpass(lat, lng) {
255 var latlng = L.latLng(lat, lng),
256 bounds = map.getBounds(),
257 bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
258 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
259 around = "around:" + radius + "," + lat + "," + lng,
260 nodes = "node(" + around + ")",
261 ways = "way(" + around + ")",
262 relations = "relation(" + around + ")",
263 nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
264 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags geom(" + bbox + ");relation(pivot.a);out tags bb;";
266 $("#sidebar_content .query-intro")
269 if (marker) map.removeLayer(marker);
270 marker = L.circle(latlng, radius, featureStyle).addTo(map);
272 $(document).everyTime(75, "fadeQueryMarker", function (i) {
274 map.removeLayer(marker);
277 opacity: 1 - i * 0.1,
278 fillOpacity: 0.5 - i * 0.05
283 runQuery(latlng, radius, nearby, $("#query-nearby"));
284 runQuery(latlng, radius, isin, $("#query-isin"), compareSize);
287 function clickHandler(e) {
288 var precision = OSM.zoomPrecision(map.getZoom()),
289 lat = e.latlng.lat.toFixed(precision),
290 lng = e.latlng.lng.toFixed(precision);
292 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
295 function enableQueryMode() {
296 queryButton.addClass("active");
297 map.on("click", clickHandler);
298 $(map.getContainer()).addClass("query-active");
301 function disableQueryMode() {
302 if (marker) map.removeLayer(marker);
303 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
304 map.off("click", clickHandler);
305 queryButton.removeClass("active");
310 page.pushstate = page.popstate = function(path) {
311 OSM.loadSidebarContent(path, function () {
312 page.load(path, true);
316 page.load = function(path, noCentre) {
317 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
318 latlng = L.latLng(params.lat, params.lon);
320 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
321 OSM.router.withoutMoveListener(function () {
322 map.setView(latlng, 15);
326 queryOverpass(params.lat, params.lon);
329 page.unload = function(sameController) {
330 if (!sameController) {