1 //= require jquery.simulate
3 OSM.Query = function(map) {
4 var queryButton = $(".control-query .control-button"),
5 uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
8 queryButton.on("click", function (e) {
12 if (queryButton.hasClass("active")) {
15 OSM.router.route("/");
22 .on("mouseover", ".query-results li.query-result", function () {
23 var geometry = $(this).data("geometry")
24 if (geometry) map.addLayer(geometry);
25 $(this).addClass("selected");
27 .on("mouseout", ".query-results li.query-result", function () {
28 var geometry = $(this).data("geometry")
29 if (geometry) map.removeLayer(geometry);
30 $(this).removeClass("selected");
32 .on("click", ".query-results li.query-result", function (e) {
33 if (!$(e.target).is('a')) {
34 $(this).find("a").simulate("click", e);
38 function interestingFeature(feature, origin, radius) {
40 if (feature.type === "node" &&
41 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > radius) {
45 for (var key in feature.tags) {
46 if (uninterestingTags.indexOf(key) < 0) {
55 function featurePrefix(feature) {
56 var tags = feature.tags;
59 if (tags.boundary === "administrative") {
60 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
62 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
64 for (var key in tags) {
65 var value = tags[key];
68 if (prefixes[key][value]) {
69 return prefixes[key][value];
71 var first = value.substr(0, 1).toUpperCase(),
72 rest = value.substr(1).replace(/_/g, " ");
81 prefix = I18n.t("javascripts.query." + feature.type);
87 function featureName(feature) {
88 var tags = feature.tags;
92 } else if (tags["ref"]) {
94 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
95 return tags["addr:housenumber"] + " " + tags["addr:street"];
97 return "#" + feature.id;
101 function featureLink(feature) {
102 if (feature.type === "area") {
103 if (feature.id >= 3600000000) {
104 var id = feature.id - 3600000000;
106 return "/browse/relation/" + id;
107 } else if (feature.id >= 2400000000) {
108 var id = feature.id - 2400000000;
110 return "/browse/way/" + id;
112 return "/browse/node/" + feature.id;
115 return "/browse/" + feature.type + "/" + feature.id;
119 function featureGeometry(feature, nodes) {
122 if (feature.type === "node") {
123 geometry = L.circleMarker([feature.lat, feature.lon]);
124 } else if (feature.type === "way") {
125 geometry = L.polyline(feature.nodes.map(function (node) {
133 function runQuery(latlng, radius, query, $section) {
134 var $ul = $section.find("ul");
139 $section.find(".loader").oneTime(1000, "loading", function () {
144 url: "http://overpass-api.de/api/interpreter",
147 data: "[timeout:5][out:json];" + query,
149 success: function(results) {
152 $section.find(".loader").stopTime("loading").hide();
154 results.elements.forEach(function (element) {
155 if (element.type === "node") {
156 nodes[element.id] = [element.lat, element.lon];
160 for (var i = 0; i < results.elements.length; i++) {
161 var element = results.elements[i];
163 if (interestingFeature(element, latlng, radius)) {
165 .addClass("query-result")
166 .data("geometry", featureGeometry(element, nodes))
169 .text(featurePrefix(element) + " ")
173 .attr("href", featureLink(element))
174 .text(featureName(element))
179 if ($ul.find("li").length == 0) {
181 .text(I18n.t("javascripts.query.nothing_found"))
188 function queryOverpass(lat, lng) {
189 var latlng = L.latLng(lat, lng),
190 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
191 around = "around:" + radius + "," + lat + "," + lng,
192 features = "(node(" + around + ");way(" + around + ");relation(" + around + "))",
193 nearby = "((" + features + ";way(bn));node(w));out;",
194 isin = "(is_in(" + lat + "," + lng + ");>);out;";
196 $("#sidebar_content .query-intro")
199 if (marker) map.removeLayer(marker);
200 marker = L.circle(latlng, radius, { clickable: false }).addTo(map);
202 $(document).everyTime(75, "fadeQueryMarker", function (i) {
204 map.removeLayer(marker);
207 opacity: 0.5 - i * 0.05,
208 fillOpacity: 0.2 - i * 0.02
213 runQuery(latlng, radius, nearby, $("#query-nearby"));
214 runQuery(latlng, radius, isin, $("#query-isin"));
217 function clickHandler(e) {
218 var precision = OSM.zoomPrecision(map.getZoom()),
219 lat = e.latlng.lat.toFixed(precision),
220 lng = e.latlng.lng.toFixed(precision);
222 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
225 function enableQueryMode() {
226 queryButton.addClass("active");
227 map.on("click", clickHandler);
228 $(map.getContainer()).addClass("query-active");
231 function disableQueryMode() {
232 if (marker) map.removeLayer(marker);
233 $(map.getContainer()).removeClass("query-active");
234 map.off("click", clickHandler);
235 queryButton.removeClass("active");
240 page.pushstate = page.popstate = function(path) {
241 OSM.loadSidebarContent(path, function () {
246 page.load = function(path) {
247 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
249 queryOverpass(params.lat, params.lon);
252 return map.getState();
255 page.unload = function() {