1 //= require jquery.simulate
2 //= require querystring
4 OSM.Query = function (map) {
5 var querystring = require("querystring-component");
7 var url = OSM.OVERPASS_URL,
8 queryButton = $(".control-query .control-button"),
9 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"],
20 queryButton.on("click", function (e) {
24 if (queryButton.hasClass("active")) {
26 } else if (!queryButton.hasClass("disabled")) {
29 }).on("disabled", function () {
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 () {
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 () {
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) {
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, {
88 defaultValue: I18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative")
91 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
98 if (prefixes[key][value]) {
99 return prefixes[key][value];
108 var first = value.substr(0, 1).toUpperCase(),
109 rest = value.substr(1).replace(/_/g, " ");
117 prefix = I18n.t("javascripts.query." + feature.type);
123 function featureName(feature) {
124 var tags = feature.tags,
125 locales = I18n.locales.get();
127 for (var i = 0; i < locales.length; i++) {
128 if (tags["name:" + locales[i]]) {
129 return tags["name:" + locales[i]];
135 } else if (tags.ref) {
137 } else if (tags["addr:housename"]) {
138 return tags["addr:housename"];
139 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
140 return tags["addr:housenumber"] + " " + tags["addr:street"];
142 return "#" + feature.id;
146 function featureGeometry(feature) {
149 if (feature.type === "node" && feature.lat && feature.lon) {
150 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
151 } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
152 geometry = L.polyline(feature.geometry.filter(function (point) {
153 return point !== null;
154 }).map(function (point) {
155 return [point.lat, point.lon];
157 } else if (feature.type === "relation" && feature.members) {
158 geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
159 return typeof geometry !== "undefined";
166 function runQuery(latlng, radius, query, $section, merge, compare) {
167 var $ul = $section.find("ul");
172 $section.find(".loader").oneTime(1000, "loading", function () {
176 if ($section.data("ajax")) {
177 $section.data("ajax").abort();
180 $section.data("ajax", $.ajax({
184 data: "[timeout:10][out:json];" + query
186 success: function (results) {
189 $section.find(".loader").stopTime("loading").hide();
192 elements = results.elements.reduce(function (hash, element) {
193 var key = element.type + element.id;
194 if ("geometry" in element) {
195 delete element.bounds;
197 hash[key] = $.extend({}, hash[key], element);
201 elements = Object.keys(elements).map(function (key) {
202 return elements[key];
205 elements = results.elements;
209 elements = elements.sort(compare);
212 for (var i = 0; i < elements.length; i++) {
213 var element = elements[i];
215 if (interestingFeature(element)) {
217 .addClass("query-result list-group-item")
218 .data("geometry", featureGeometry(element))
219 .text(featurePrefix(element) + " ")
223 .attr("href", "/" + element.type + "/" + element.id)
224 .text(featureName(element))
229 if (results.remark) {
231 .addClass("query-result list-group-item")
232 .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
236 if ($ul.find("li").length === 0) {
238 .addClass("query-result list-group-item")
239 .text(I18n.t("javascripts.query.nothing_found"))
243 error: function (xhr, status, error) {
244 $section.find(".loader").stopTime("loading").hide();
247 .addClass("query-result list-group-item")
248 .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
254 function compareSize(feature1, feature2) {
255 var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
256 height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
257 area1 = width1 * height1,
258 width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
259 height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
260 area2 = width2 * height2;
262 return area1 - area2;
266 * To find nearby objects we ask overpass for the union of the
269 * node(around:<radius>,<lat>,lng>)
270 * way(around:<radius>,<lat>,lng>)
271 * relation(around:<radius>,<lat>,lng>)
273 * to find enclosing objects we first find all the enclosing areas:
275 * is_in(<lat>,<lng>)->.a
277 * and then return the union of the following sets:
282 * In both cases we then ask to retrieve tags and the geometry
285 function queryOverpass(lat, lng) {
286 var latlng = L.latLng(lat, lng).wrap(),
287 bounds = map.getBounds().wrap(),
288 precision = OSM.zoomPrecision(map.getZoom()),
289 bbox = bounds.getSouth().toFixed(precision) + "," +
290 bounds.getWest().toFixed(precision) + "," +
291 bounds.getNorth().toFixed(precision) + "," +
292 bounds.getEast().toFixed(precision),
293 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
294 around = "around:" + radius + "," + lat + "," + lng,
295 nodes = "node(" + around + ")",
296 ways = "way(" + around + ")",
297 relations = "relation(" + around + ")",
298 nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
299 isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
301 $("#sidebar_content .query-intro")
304 if (marker) map.removeLayer(marker);
305 marker = L.circle(latlng, radius, featureStyle).addTo(map);
307 $(document).everyTime(75, "fadeQueryMarker", function (i) {
309 map.removeLayer(marker);
312 opacity: 1 - (i * 0.1),
313 fillOpacity: 0.5 - (i * 0.05)
318 runQuery(latlng, radius, nearby, $("#query-nearby"), false);
319 runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
322 function clickHandler(e) {
323 var precision = OSM.zoomPrecision(map.getZoom()),
324 latlng = e.latlng.wrap(),
325 lat = latlng.lat.toFixed(precision),
326 lng = latlng.lng.toFixed(precision);
328 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
331 function enableQueryMode() {
332 queryButton.addClass("active");
333 map.on("click", clickHandler);
334 $(map.getContainer()).addClass("query-active");
337 function disableQueryMode() {
338 if (marker) map.removeLayer(marker);
339 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
340 map.off("click", clickHandler);
341 queryButton.removeClass("active");
346 page.pushstate = page.popstate = function (path) {
347 OSM.loadSidebarContent(path, function () {
348 page.load(path, true);
352 page.load = function (path, noCentre) {
353 var params = querystring.parse(path.substring(path.indexOf("?") + 1)),
354 latlng = L.latLng(params.lat, params.lon);
356 if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
357 OSM.router.withoutMoveListener(function () {
358 map.setView(latlng, 15);
362 queryOverpass(params.lat, params.lon);
365 page.unload = function (sameController) {
366 if (!sameController) {