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 features = "(node(" + around + ");way(" + around + ");relation(" + around + "))",
217 nearby = "((" + features + ";way(bn));node(w));out;",
218 isin = "is_in(" + lat + "," + lng + ")->.a;(relation(pivot.a);way(pivot.a);node(w));out;";
220 $("#sidebar_content .query-intro")
223 if (marker) map.removeLayer(marker);
224 marker = L.circle(latlng, radius, featureStyle).addTo(map);
226 $(document).everyTime(75, "fadeQueryMarker", function (i) {
228 map.removeLayer(marker);
231 opacity: 1 - i * 0.1,
232 fillOpacity: 0.5 - i * 0.05
237 runQuery(latlng, radius, nearby, $("#query-nearby"));
238 runQuery(latlng, radius, isin, $("#query-isin"));
241 function clickHandler(e) {
242 var precision = OSM.zoomPrecision(map.getZoom()),
243 lat = e.latlng.lat.toFixed(precision),
244 lng = e.latlng.lng.toFixed(precision);
246 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
249 function enableQueryMode() {
250 queryButton.addClass("active");
251 map.on("click", clickHandler);
252 $(map.getContainer()).addClass("query-active");
255 function disableQueryMode() {
256 if (marker) map.removeLayer(marker);
257 $(map.getContainer()).removeClass("query-active").removeClass("query-disabled");
258 map.off("click", clickHandler);
259 queryButton.removeClass("active");
264 page.pushstate = page.popstate = function(path) {
265 OSM.loadSidebarContent(path, function () {
266 page.load(path, true);
270 page.load = function(path, noCentre) {
271 var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
272 latlng = L.latLng(params.lat, params.lon);
274 if (!window.location.hash &&
275 (!noCentre || !map.getBounds().contains(latlng))) {
276 OSM.router.withoutMoveListener(function () {
277 map.setView(latlng, 15);
281 queryOverpass(params.lat, params.lon);
285 page.unload = function() {