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("active")) {
24 } else if (!queryButton.hasClass("disabled")) {
27 }).on("disabled", function () {
28 if (queryButton.hasClass("active")) {
29 map.off("click", clickHandler);
30 $(map.getContainer()).removeClass("query-active").addClass("query-disabled");
31 $(this).tooltip("show");
33 }).on("enabled", function () {
34 if (queryButton.hasClass("active")) {
35 map.on("click", clickHandler);
36 $(map.getContainer()).removeClass("query-disabled").addClass("query-active");
37 $(this).tooltip("hide");
42 .on("mouseover", ".query-results li.query-result", function () {
43 var geometry = $(this).data("geometry");
44 if (geometry) map.addLayer(geometry);
45 $(this).addClass("selected");
47 .on("mouseout", ".query-results li.query-result", function () {
48 var geometry = $(this).data("geometry");
49 if (geometry) map.removeLayer(geometry);
50 $(this).removeClass("selected");
52 .on("mousedown", ".query-results li.query-result", function () {
54 $(this).one("click", function (e) {
56 var geometry = $(this).data("geometry");
57 if (geometry) map.removeLayer(geometry);
59 if (!$(e.target).is('a')) {
60 $(this).find("a").simulate("click", e);
63 }).one("mousemove", function () {
68 function interestingFeature(feature) {
70 for (var key in feature.tags) {
71 if (uninterestingTags.indexOf(key) < 0) {
80 function featurePrefix(feature) {
81 var tags = feature.tags;
84 if (tags.boundary === "administrative" && tags.admin_level) {
85 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, {
86 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
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];
101 for (var key in tags) {
102 var value = tags[key];
105 var first = value.substr(0, 1).toUpperCase(),
106 rest = value.substr(1).replace(/_/g, " ");
114 prefix = I18n.t("javascripts.query." + feature.type);
120 function featureName(feature) {
121 var tags = feature.tags,
122 locales = I18n.locales.get();
124 for (var i = 0; i < locales.length; i++) {
125 if (tags["name:" + locales[i]]) {
126 return tags["name:" + locales[i]];
132 } else if (tags.ref) {
134 } else if (tags["addr:housename"]) {
135 return tags["addr:housename"];
136 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
137 return tags["addr:housenumber"] + " " + tags["addr:street"];
139 return "#" + feature.id;
143 function featureGeometry(feature) {
146 if (feature.type === "node" && feature.lat && feature.lon) {
147 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
148 } else if (feature.type === "way" && feature.geometry) {
149 geometry = L.polyline(feature.geometry.filter(function (point) {
150 return point !== null;
151 }).map(function (point) {
152 return [point.lat, point.lon];
154 } else if (feature.type === "relation" && feature.members) {
155 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
156 return geometry !== undefined;
163 function runQuery(latlng, radius, query, $section, compare) {
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();
189 elements = results.elements.sort(compare);
191 elements = results.elements;
194 for (var i = 0; i < elements.length; i++) {
195 var element = elements[i];
197 if (interestingFeature(element)) {
199 .addClass("query-result")
200 .data("geometry", featureGeometry(element))
203 .text(featurePrefix(element) + " ")
207 .attr("href", "/" + element.type + "/" + element.id)
208 .text(featureName(element))
213 if ($ul.find("li").length === 0) {
215 .text(I18n.t("javascripts.query.nothing_found"))
219 error: function(xhr, status, error) {
220 $section.find(".loader").stopTime("loading").hide();
223 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
229 function compareSize(feature1, feature2) {
230 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
231 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
232 area1 = width1 * height1,
233 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
234 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
235 area2 = width2 * height2;
237 return area1 - area2;
241 * To find nearby objects we ask overpass for the union of the
244 * node(around:<radius>,<lat>,lng>)
245 * way(around:<radius>,<lat>,lng>)
246 * relation(around:<radius>,<lat>,lng>)
248 * to find enclosing objects we first find all the enclosing areas:
250 * is_in(<lat>,<lng>)->.a
252 * and then return the union of the following sets:
257 * In both cases we then ask to retrieve tags and the geometry
260 function queryOverpass(lat, lng) {
261 var latlng = L.latLng(lat, lng),
262 bounds = map.getBounds(),
263 bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
264 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
265 around = "around:" + radius + "," + lat + "," + lng,
266 nodes = "node(" + around + ")",
267 ways = "way(" + around + ")",
268 relations = "relation(" + around + ")",
269 nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
270 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags geom(" + bbox + ");relation(pivot.a);out tags bb;";
272 $("#sidebar_content .query-intro")
275 if (marker) map.removeLayer(marker);
276 marker = L.circle(latlng, radius, featureStyle).addTo(map);
278 $(document).everyTime(75, "fadeQueryMarker", function (i) {
280 map.removeLayer(marker);
283 opacity: 1 - i * 0.1,
284 fillOpacity: 0.5 - i * 0.05
289 runQuery(latlng, radius, nearby, $("#query-nearby"));
290 runQuery(latlng, radius, isin, $("#query-isin"), compareSize);
293 function clickHandler(e) {
294 var precision = OSM.zoomPrecision(map.getZoom()),
295 lat = e.latlng.lat.toFixed(precision),
296 lng = e.latlng.lng.toFixed(precision);
298 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
301 function enableQueryMode() {
302 queryButton.addClass("active");
303 map.on("click", clickHandler);
304 $(map.getContainer()).addClass("query-active");
307 function disableQueryMode() {
308 if (marker) map.removeLayer(marker);
309 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
310 map.off("click", clickHandler);
311 queryButton.removeClass("active");
316 page.pushstate = page.popstate = function(path) {
317 OSM.loadSidebarContent(path, function () {
318 page.load(path, true);
322 page.load = function(path, noCentre) {
323 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
324 latlng = L.latLng(params.lat, params.lon);
326 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
327 OSM.router.withoutMoveListener(function () {
328 map.setView(latlng, 15);
332 queryOverpass(params.lat, params.lon);
335 page.unload = function(sameController) {
336 if (!sameController) {