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");
 
  96           if (prefixes[key][value]) {
 
  97             return prefixes[key][value];
 
 106           var first = value.substr(0, 1).toUpperCase(),
 
 107             rest = value.substr(1).replace(/_/g, " ");
 
 115       prefix = I18n.t("javascripts.query." + feature.type);
 
 121   function featureName(feature) {
 
 122     var tags = feature.tags,
 
 123       locales = I18n.locales.get();
 
 125     for (var i = 0; i < locales.length; i++) {
 
 126       if (tags["name:" + locales[i]]) {
 
 127         return tags["name:" + locales[i]];
 
 133     } else if (tags.ref) {
 
 135     } else if (tags["addr:housename"]) {
 
 136       return tags["addr:housename"];
 
 137     } else if (tags["addr:housenumber"] && tags["addr:street"]) {
 
 138       return tags["addr:housenumber"] + " " + tags["addr:street"];
 
 140       return "#" + feature.id;
 
 144   function featureGeometry(feature) {
 
 147     if (feature.type === "node" && feature.lat && feature.lon) {
 
 148       geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
 
 149     } else if (feature.type === "way" && feature.geometry) {
 
 150       geometry = L.polyline(feature.geometry.filter(function (point) {
 
 151         return point !== null;
 
 152       }).map(function (point) {
 
 153         return [point.lat, point.lon];
 
 155     } else if (feature.type === "relation" && feature.members) {
 
 156       geometry = L.featureGroup(feature.members.map(featureGeometry).filter(function (geometry) {
 
 157         return geometry !== undefined;
 
 164   function runQuery(latlng, radius, query, $section, merge, compare) {
 
 165     var $ul = $section.find("ul");
 
 170     $section.find(".loader").oneTime(1000, "loading", function () {
 
 174     if ($section.data("ajax")) {
 
 175       $section.data("ajax").abort();
 
 178     $section.data("ajax", $.ajax({
 
 182         data: "[timeout:10][out:json];" + query,
 
 184       success: function(results) {
 
 187         $section.find(".loader").stopTime("loading").hide();
 
 190           elements = results.elements.reduce(function (hash, element) {
 
 191             var key = element.type + element.id;
 
 192             if ("geometry" in element) {
 
 193               delete element.bounds;
 
 195             hash[key] = $.extend({}, hash[key], element);
 
 199           elements = Object.keys(elements).map(function (key) {
 
 200             return elements[key];
 
 203           elements = results.elements;
 
 207           elements = elements.sort(compare);
 
 210         for (var i = 0; i < elements.length; i++) {
 
 211           var element = elements[i];
 
 213           if (interestingFeature(element)) {
 
 215               .addClass("query-result")
 
 216               .data("geometry", featureGeometry(element))
 
 219               .text(featurePrefix(element) + " ")
 
 223               .attr("href", "/" + element.type + "/" + element.id)
 
 224               .text(featureName(element))
 
 229         if (results.remark) {
 
 231             .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
 
 235         if ($ul.find("li").length === 0) {
 
 237             .text(I18n.t("javascripts.query.nothing_found"))
 
 241       error: function(xhr, status, error) {
 
 242         $section.find(".loader").stopTime("loading").hide();
 
 245           .text(I18n.t("javascripts.query." + status, { server: url, error: error }))
 
 251   function compareSize(feature1, feature2) {
 
 252     var width1 = feature1.bounds.maxlon - feature1.bounds.minlon,
 
 253       height1 = feature1.bounds.maxlat - feature1.bounds.minlat,
 
 254       area1 = width1 * height1,
 
 255       width2 = feature2.bounds.maxlat - feature2.bounds.minlat,
 
 256       height2 = feature2.bounds.maxlat - feature2.bounds.minlat,
 
 257       area2 = width2 * height2;
 
 259     return area1 - area2;
 
 263    * To find nearby objects we ask overpass for the union of the
 
 266    *   node(around:<radius>,<lat>,lng>)
 
 267    *   way(around:<radius>,<lat>,lng>)
 
 268    *   relation(around:<radius>,<lat>,lng>)
 
 270    * to find enclosing objects we first find all the enclosing areas:
 
 272    *   is_in(<lat>,<lng>)->.a
 
 274    * and then return the union of the following sets:
 
 279    * In both cases we then ask to retrieve tags and the geometry
 
 282   function queryOverpass(lat, lng) {
 
 283     var latlng = L.latLng(lat, lng).wrap(),
 
 284       bounds = map.getBounds().wrap(),
 
 285       bbox = bounds.getSouth() + "," + bounds.getWest() + "," + bounds.getNorth() + "," + bounds.getEast(),
 
 286       radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
 
 287       around = "around:" + radius + "," + lat + "," + lng,
 
 288       nodes = "node(" + around + ")",
 
 289       ways = "way(" + around + ")",
 
 290       relations = "relation(" + around + ")",
 
 291       nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
 
 292       isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
 
 294     $("#sidebar_content .query-intro")
 
 297     if (marker) map.removeLayer(marker);
 
 298     marker = L.circle(latlng, radius, featureStyle).addTo(map);
 
 300     $(document).everyTime(75, "fadeQueryMarker", function (i) {
 
 302         map.removeLayer(marker);
 
 305           opacity: 1 - i * 0.1,
 
 306           fillOpacity: 0.5 - i * 0.05
 
 311     runQuery(latlng, radius, nearby, $("#query-nearby"), false);
 
 312     runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
 
 315   function clickHandler(e) {
 
 316     var precision = OSM.zoomPrecision(map.getZoom()),
 
 317       latlng = e.latlng.wrap(),
 
 318       lat = latlng.lat.toFixed(precision),
 
 319       lng = latlng.lng.toFixed(precision);
 
 321     OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
 
 324   function enableQueryMode() {
 
 325     queryButton.addClass("active");
 
 326     map.on("click", clickHandler);
 
 327     $(map.getContainer()).addClass("query-active");
 
 330   function disableQueryMode() {
 
 331     if (marker) map.removeLayer(marker);
 
 332     $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
 
 333     map.off("click", clickHandler);
 
 334     queryButton.removeClass("active");
 
 339   page.pushstate = page.popstate = function(path) {
 
 340     OSM.loadSidebarContent(path, function () {
 
 341       page.load(path, true);
 
 345   page.load = function(path, noCentre) {
 
 346     var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
 
 347       latlng = L.latLng(params.lat, params.lon);
 
 349     if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
 
 350       OSM.router.withoutMoveListener(function () {
 
 351         map.setView(latlng, 15);
 
 355     queryOverpass(params.lat, params.lon);
 
 358   page.unload = function(sameController) {
 
 359     if (!sameController) {