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'],
9 queryButton.on("click", function (e) {
13 if (queryButton.hasClass("active")) {
16 OSM.router.route("/");
23 .on("mouseover", ".query-results li.query-result", function () {
24 var geometry = $(this).data("geometry")
25 if (geometry) map.addLayer(geometry);
26 $(this).addClass("selected");
28 .on("mouseout", ".query-results li.query-result", function () {
29 var geometry = $(this).data("geometry")
30 if (geometry) map.removeLayer(geometry);
31 $(this).removeClass("selected");
33 .on("click", ".query-results li.query-result", function (e) {
34 if (!$(e.target).is('a')) {
35 $(this).find("a").simulate("click", e);
39 function interestingFeature(feature, origin) {
41 if (feature.type === "node" &&
42 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > searchRadius) {
46 for (var key in feature.tags) {
47 if (uninterestingTags.indexOf(key) < 0) {
56 function featurePrefix(feature) {
57 var tags = feature.tags;
60 if (tags.boundary === "administrative") {
61 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
63 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
65 for (var key in tags) {
66 var value = tags[key];
69 if (prefixes[key][value]) {
70 return prefixes[key][value];
72 var first = value.substr(0, 1).toUpperCase(),
73 rest = value.substr(1).replace(/_/g, " ");
82 prefix = I18n.t("javascripts.query." + feature.type);
88 function featureName(feature) {
89 var tags = feature.tags;
93 } else if (tags["ref"]) {
95 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
96 return tags["addr:housenumber"] + " " + tags["addr:street"];
98 return "#" + feature.id;
102 function featureLink(feature) {
103 if (feature.type === "area") {
104 if (feature.id >= 3600000000) {
105 var id = feature.id - 3600000000;
107 return "/browse/relation/" + id;
108 } else if (feature.id >= 2400000000) {
109 var id = feature.id - 2400000000;
111 return "/browse/way/" + id;
113 return "/browse/node/" + feature.id;
116 return "/browse/" + feature.type + "/" + feature.id;
120 function featureGeometry(feature, nodes) {
123 if (feature.type === "node") {
124 geometry = L.circleMarker([feature.lat, feature.lon]);
125 } else if (feature.type === "way") {
126 geometry = L.polyline(feature.nodes.map(function (node) {
134 function runQuery(latlng, query, $section) {
135 var $ul = $section.find("ul");
140 $section.find(".loader").oneTime(1000, "loading", function () {
145 url: "http://overpass-api.de/api/interpreter",
148 data: "[timeout:5][out:json];" + query,
150 success: function(results) {
153 $section.find(".loader").stopTime("loading").hide();
155 results.elements.forEach(function (element) {
156 if (element.type === "node") {
157 nodes[element.id] = [element.lat, element.lon];
161 for (var i = 0; i < results.elements.length; i++) {
162 var element = results.elements[i];
164 if (interestingFeature(element, latlng)) {
166 .addClass("query-result")
167 .data("geometry", featureGeometry(element, nodes))
170 .text(featurePrefix(element) + " ")
174 .attr("href", featureLink(element))
175 .text(featureName(element))
180 if ($ul.find("li").length == 0) {
182 .text(I18n.t("javascripts.query.nothing_found"))
189 function queryOverpass(lat, lng) {
190 var latlng = L.latLng(lat, lng),
191 around = "around:" + searchRadius + "," + 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")
200 marker.setLatLng(latlng).addTo(map);
202 marker = L.circle(latlng, searchRadius, { clickable: false }).addTo(map);
205 $(document).everyTime(75, "fadeQueryMarker", function (i) {
207 map.removeLayer(marker);
210 opacity: 0.5 - i * 0.05,
211 fillOpacity: 0.2 - i * 0.02
216 runQuery(latlng, nearby, $("#query-nearby"));
217 runQuery(latlng, isin, $("#query-isin"));
220 function clickHandler(e) {
221 var precision = OSM.zoomPrecision(map.getZoom()),
222 lat = e.latlng.lat.toFixed(precision),
223 lng = e.latlng.lng.toFixed(precision);
225 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
228 function enableQueryMode() {
229 queryButton.addClass("active");
230 map.on("click", clickHandler);
231 $(map.getContainer()).addClass("query-active");
234 function disableQueryMode() {
235 if (marker) map.removeLayer(marker);
236 $(map.getContainer()).removeClass("query-active");
237 map.off("click", clickHandler);
238 queryButton.removeClass("active");
243 page.pushstate = page.popstate = function(path) {
244 OSM.loadSidebarContent(path, function () {
249 page.load = function(path) {
250 var params = querystring.parse(path.substring(path.indexOf('?') + 1));
252 queryOverpass(params.lat, params.lon);
255 return map.getState();
258 page.unload = function() {