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'],
16 queryButton.on("click", function (e) {
20 if (queryButton.hasClass("disabled")) return;
22 if (queryButton.hasClass("active")) {
25 OSM.router.route("/");
29 }).on("disabled", function (e) {
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 (e) {
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("click", ".query-results li.query-result", function (e) {
55 if (!$(e.target).is('a')) {
56 $(this).find("a").simulate("click", e);
60 function interestingFeature(feature, origin, radius) {
62 if (feature.type === "node" &&
63 OSM.distance(origin, L.latLng(feature.lat, feature.lon)) > radius) {
67 for (var key in feature.tags) {
68 if (uninterestingTags.indexOf(key) < 0) {
77 function featurePrefix(feature) {
78 var tags = feature.tags;
81 if (tags.boundary === "administrative") {
82 prefix = I18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level)
84 var prefixes = I18n.t("geocoder.search_osm_nominatim.prefix");
86 for (var key in tags) {
87 var value = tags[key];
90 if (prefixes[key][value]) {
91 return prefixes[key][value];
93 var first = value.substr(0, 1).toUpperCase(),
94 rest = value.substr(1).replace(/_/g, " ");
103 prefix = I18n.t("javascripts.query." + feature.type);
109 function featureName(feature) {
110 var tags = feature.tags;
114 } else if (tags["ref"]) {
116 } else if (tags["addr:housename"]) {
117 return tags["addr:housename"];
118 } else if (tags["addr:housenumber"] && tags["addr:street"]) {
119 return tags["addr:housenumber"] + " " + tags["addr:street"];
121 return "#" + feature.id;
125 function featureGeometry(feature, features) {
128 if (feature.type === "node") {
129 geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
130 } else if (feature.type === "way") {
131 geometry = L.polyline(feature.nodes.map(function (node) {
132 return features["node" + node].getLatLng();
134 } else if (feature.type === "relation") {
135 geometry = L.featureGroup();
137 feature.members.forEach(function (member) {
138 if (features[member.type + member.ref]) {
139 geometry.addLayer(features[member.type + member.ref]);
145 features[feature.type + feature.id] = geometry;
151 function runQuery(latlng, radius, query, $section) {
152 var $ul = $section.find("ul");
157 $section.find(".loader").oneTime(1000, "loading", function () {
161 if ($section.data("ajax")) {
162 $section.data("ajax").abort();
165 $section.data("ajax", $.ajax({
166 url: OSM.OVERPASS_URL,
169 data: "[timeout:5][out:json];" + query,
171 success: function(results) {
174 $section.find(".loader").stopTime("loading").hide();
176 for (var i = 0; i < results.elements.length; i++) {
177 var element = results.elements[i],
178 geometry = featureGeometry(element, features);
180 if (interestingFeature(element, latlng, radius)) {
182 .addClass("query-result")
183 .data("geometry", geometry)
186 .text(featurePrefix(element) + " ")
190 .attr("href", "/" + element.type + "/" + element.id)
191 .text(featureName(element))
196 if ($ul.find("li").length == 0) {
198 .text(I18n.t("javascripts.query.nothing_found"))
202 error: function(xhr, status, error) {
203 $section.find(".loader").stopTime("loading").hide();
206 .text(I18n.t("javascripts.query." + status, { server: OSM.OVERPASS_URL, error: error }))
212 function queryOverpass(lat, lng) {
213 var latlng = L.latLng(lat, lng),
214 radius = 10 * Math.pow(1.5, 19 - map.getZoom()),
215 around = "around:" + radius + "," + lat + "," + lng,
216 nodes = "node(" + around + ")",
217 ways = "way(" + around + ");node(w)",
218 relations = "relation(" + around + ")",
219 nearby = "(" + nodes + ";" + ways + ";" + relations + ");out;",
220 isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a);node(w));out;";
222 $("#sidebar_content .query-intro")
225 if (marker) map.removeLayer(marker);
226 marker = L.circle(latlng, radius, featureStyle).addTo(map);
228 $(document).everyTime(75, "fadeQueryMarker", function (i) {
230 map.removeLayer(marker);
233 opacity: 1 - i * 0.1,
234 fillOpacity: 0.5 - i * 0.05
239 runQuery(latlng, radius, nearby, $("#query-nearby"));
240 runQuery(latlng, radius, isin, $("#query-isin"));
243 function clickHandler(e) {
244 var precision = OSM.zoomPrecision(map.getZoom()),
245 lat = e.latlng.lat.toFixed(precision),
246 lng = e.latlng.lng.toFixed(precision);
248 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
251 function enableQueryMode() {
252 queryButton.addClass("active");
253 map.on("click", clickHandler);
254 $(map.getContainer()).addClass("query-active");
257 function disableQueryMode() {
258 if (marker) map.removeLayer(marker);
259 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
260 map.off("click", clickHandler);
261 queryButton.removeClass("active");
266 page.pushstate = page.popstate = function(path) {
267 OSM.loadSidebarContent(path, function () {
268 page.load(path, true);
272 page.load = function(path, noCentre) {
273 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
274 latlng = L.latLng(params.lat, params.lon);
276 if (!window.location.hash &&
277 (!noCentre || !map.getBounds().contains(latlng))) {
278 OSM.router.withoutMoveListener(function () {
279 map.setView(latlng, 15);
283 queryOverpass(params.lat, params.lon);
287 page.unload = function() {