Skip to content

Instantly share code, notes, and snippets.

@nrenner
Created August 20, 2015 15:06
Show Gist options
  • Save nrenner/61b5f3c3a139f7443e87 to your computer and use it in GitHub Desktop.
Save nrenner/61b5f3c3a139f7443e87 to your computer and use it in GitHub Desktop.
OverPassLayer debug page
<!DOCTYPE html>
<html>
<head>
<title>OverPass Layer Debug</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<link rel="stylesheet" href="OverPassLayer.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet-src.js"></script>
<script src="OverPassLayer.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
(function() {
'use strict';
var attr_osm = 'Map data &copy; <a href="http://openstreetmap.org/">OpenStreetMap</a> contributors',
attr_overpass = 'POI via <a href="http://www.overpass-api.de/">Overpass API</a>';
var osm = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {opacity: 0.7, attribution: [attr_osm, attr_overpass].join(', ')});
var map = new L.Map('map').addLayer(osm).setView(new L.LatLng(52.265, 10.524), 14);
L.Path.CLIP_PADDING = 1;
// noop "request"
L.OverPassLayer.prototype.sendRequest = function(url, done) {
var self = this,
reference = { 'instance': this };
setTimeout(function() {
self.options.onSuccess.call(reference, { "elements": [] });
done();
}, 500);
};
// current request bboxes in blue
var requestBoxes = L.featureGroup().addTo(map);
var toOverpassBBoxStringOrig = L.LatLngBounds.prototype.toOverpassBBoxString;
L.LatLngBounds.prototype.toOverpassBBoxString = function (){
var s = toOverpassBBoxStringOrig.apply(this, arguments);
var box = L.rectangle(this, {
color: 'blue',
weight: 1,
opacity: 0.5,
fillOpacity: 0.1,
clickable: false
});
requestBoxes.addLayer(box);
return s;
}
// total bboxes in black
var responseBoxes = L.featureGroup().addTo(map);
var totalCount = 0;
function afterRequest() {
var requestLayers = requestBoxes.getLayers(),
count = requestLayers.length;
totalCount += count;
console.debug('z' + map.getZoom() + ': ' + count + ' requests, total: ' + totalCount);
requestBoxes.clearLayers();
requestLayers.forEach(function(layer) {
layer.setStyle({
color: 'black'
});
responseBoxes.addLayer(layer);
})
};
var opl = new L.OverPassLayer({
debug: true,
afterRequest: afterRequest
});
map.addLayer(opl);
var opl12 = new L.OverPassLayer({
minzoom: 12,
minZoom: 12,
afterRequest: afterRequest
});
var oplViewBox = new L.OverPassLayer({
requestPerTile: false,
afterRequest: afterRequest
});
var opl16 = new L.OverPassLayer({
minzoom: 16,
minZoom: 16,
afterRequest: afterRequest
});
var switcher = L.control.layers({
"minzoom 15 (default)": opl,
"minzoom 12 (Briefkastenkarte)": opl12,
"requestPerTile: false (viewport)": oplViewBox,
"minzoom 16 (= 2 offset)": opl16
}, {}).addTo(map);
map.on('baselayerchange', function(evt) {
responseBoxes.clearLayers();
totalCount = 0;
console.debug('\n--- ' + evt.name + ':');
});
}());
</script>
</body>
</html>
.leaflet-control-minZoomIndicator {
font-size: 2em;
background: #ffffff;
background-color: rgba(255,255,255,0.7);
border-radius: 10px;
padding: 1px 15px;
opacity: 0.5;
}
// https://github.com/GuillaumeAmat/leaflet-overpass-layer
/*
Copyright (c) 2013 Guillaume AMAT <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Please note that this software includes other libraries or parts of libraries which have their own license file or license annotation in the source code and may be released under different licences.
*/
/*
fork of
https://github.com/kartenkarsten/leaflet-layer-overpass
Copyright (c) 2013 Karsten Hinz <k.hinz(at)tu-bs.de>
*/
L.Control.MinZoomIndicator = L.Control.extend({
options: {
position: 'bottomleft',
},
_layers: {},
initialize: function (options) {
L.Util.setOptions(this, options);
this._layers = {};
},
/**
* adds a layer with minzoom information to this._layers
*/
_addLayer: function(layer) {
var minzoom = 15;
if (layer.options.minzoom) {
minzoom = layer.options.minzoom;
}
this._layers[layer._leaflet_id] = minzoom;
this._updateBox(null);
},
/**
* removes a layer from this._layers
*/
_removeLayer: function(layer) {
this._layers[layer._leaflet_id] = null;
this._updateBox(null);
},
_getMinZoomLevel: function() {
var key,
minZoomlevel =- 1;
for(key in this._layers) {
if ((this._layers[key] !== null) && (this._layers[key] > minZoomlevel)) {
minZoomlevel = this._layers[key];
}
}
return minZoomlevel;
},
onAdd: function (map) {
this._map = map;
map.zoomIndicator = this;
var className = this.className,
container = this._container = L.DomUtil.create('div', className);
map.on('moveend', this._updateBox, this);
this._updateBox(null);
return container;
},
onRemove: function(map) {
L.Control.prototype.onRemove.call(this, map);
map.off({
'moveend': this._updateBox
}, this);
this._map = null;
},
_updateBox: function (event) {
if (event !== null) {
L.DomEvent.preventDefault(event);
}
var minzoomlevel = this._getMinZoomLevel();
if (minzoomlevel == -1) {
this._container.innerHTML = this.options.minZoomMessageNoLayer;
} else {
this._container.innerHTML = this.options.minZoomMessage
.replace(/CURRENTZOOM/, this._map.getZoom())
.replace(/MINZOOMLEVEL/, minzoomlevel);
}
if (this._map.getZoom() >= minzoomlevel) {
this._container.style.display = 'none';
} else {
this._container.style.display = 'block';
}
},
className : 'leaflet-control-minZoomIndicator'
});
L.LatLngBounds.prototype.toOverpassBBoxString = function (){
var a = this._southWest,
b = this._northEast;
return [a.lat, a.lng, b.lat, b.lng].join(',');
};
L.OverPassLayer = L.FeatureGroup.extend({
options: {
'minZoom': 15,
'requestPerTile': true,
'endPoint': 'http://overpass-api.de/api/',
'query': '(node({{bbox}})[organic];node({{bbox}})[second_hand];);out qt;',
'timeout': 30 * 1000, // Milliseconds
'retryOnTimeout': false,
'noInitialRequest': false,
beforeRequest: function() {
},
afterRequest: function() {
},
onSuccess: function(data) {
for(var i = 0; i < data.elements.length; i++) {
var pos, popup, circle,
e = data.elements[i];
if (e.id in this.instance._ids) return;
this.instance._ids[e.id] = true;
if (e.type == 'node') {
pos = new L.LatLng(e.lat, e.lon);
} else {
pos = new L.LatLng(e.center.lat, e.center.lon);
}
popup = this.instance._poiInfo(e.tags, e.id);
circle = L.circle(pos, 50, {
'color': 'green',
'fillColor': '#3f0',
'fillOpacity': 0.5,
})
.bindPopup(popup);
this.instance.addLayer(circle);
}
},
onError: function() {
},
onTimeout: function() {
},
minZoomIndicatorOptions: {
'position': 'bottomleft',
'minZoomMessageNoLayer': 'no layer assigned',
'minZoomMessage': 'current Zoom-Level: CURRENTZOOM all data at Level: MINZOOMLEVEL',
},
},
initialize: function (options) {
L.Util.setOptions(this, options);
this._layers = {};
this._ids = {};
this._requested = {};
},
_poiInfo: function(tags, id) {
var row,
link = document.createElement('a'),
table = document.createElement('table'),
div = document.createElement('div');
link.href = 'http://www.openstreetmap.org/edit?editor=id&node=' + id;
link.appendChild(document.createTextNode("Edit this entry in iD"));
for (var key in tags){
row = table.insertRow(0);
row.insertCell(0).appendChild(document.createTextNode(key));
row.insertCell(1).appendChild(document.createTextNode(tags[key]));
}
div.appendChild(link);
div.appendChild(table);
return div;
},
/**
* splits the current view in uniform bboxes to allow caching
*/
long2tile: function (lon, zoom) {
return ( Math.floor((lon + 180) / 360 * Math.pow(2, zoom)) );
},
lat2tile: function (lat, zoom) {
return ( Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI/180)) / Math.PI) / 2 * Math.pow(2, zoom)) );
},
tile2long: function (x, z) {
return ( x / Math.pow(2, z) * 360 - 180 );
},
tile2lat: function (y, z) {
var n = Math.PI - 2 * Math.PI * y / Math.pow(2, z);
return ( 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))) );
},
_view2BBoxes: function(l, b, r, t) {
var top, right, bottom, left,
requestZoomLevel= 14,
lidx = this.long2tile(l, requestZoomLevel),
ridx = this.long2tile(r, requestZoomLevel),
tidx = this.lat2tile(t, requestZoomLevel),
bidx = this.lat2tile(b, requestZoomLevel),
result = [];
for (var x = lidx; x <= ridx; x++) {
for (var y = tidx; y <= bidx; y++) {
left = Math.round(this.tile2long(x, requestZoomLevel) * 1000000) / 1000000;
right = Math.round(this.tile2long(x + 1, requestZoomLevel) * 1000000) / 1000000;
top = Math.round(this.tile2lat(y, requestZoomLevel) * 1000000) / 1000000;
bottom = Math.round(this.tile2lat(y + 1, requestZoomLevel) * 1000000) / 1000000;
result.push(
new L.LatLngBounds(new L.LatLng(bottom, left),
new L.LatLng(top, right))
);
}
}
return result;
},
onMoveEnd: function () {
if (this._map.getZoom() >= this.options.minZoom) {
var x, y, bbox, bboxList, request, url, queryWithMapCoordinates,
self = this,
countdown = 0,
beforeRequest = true,
done = function () {
if (--countdown === 0) {
self.options.afterRequest.call(self);
}
};
if (this.options.requestPerTile) {
bboxList = this._view2BBoxes(
this._map.getBounds()._southWest.lng,
this._map.getBounds()._southWest.lat,
this._map.getBounds()._northEast.lng,
this._map.getBounds()._northEast.lat
);
} else {
bboxList = new Array(this._map.getBounds());
}
countdown = bboxList.length;
for (var i = 0; i < bboxList.length; i++) {
bbox = bboxList[i];
x = bbox._southWest.lng;
y = bbox._northEast.lat;
if ((x in this._requested) && (y in this._requested[x]) && (this._requested[x][y] === true)) {
countdown--;
continue;
}
if (!(x in this._requested)) {
this._requested[x] = {};
}
this._requested[x][y] = true;
queryWithMapCoordinates = this.options.query.replace(/(\{\{bbox\}\})/g, bbox.toOverpassBBoxString());
url = this.options.endPoint + 'interpreter?data=[out:json];'+ queryWithMapCoordinates;
if (beforeRequest) {
var beforeRequestResult = this.options.beforeRequest.call(this);
if ( beforeRequestResult === false ) {
this.options.afterRequest.call(this);
return;
}
beforeRequest = false;
}
this.sendRequest(url, done);
}
}
},
sendRequest: function(url, done) {
var self = this,
reference = { 'instance': this };
request = new XMLHttpRequest();
request.open('GET', url, true);
request.timeout = this.options.timeout;
request.ontimeout = function () {
self.options.onTimeout.call(reference, this);
if ( self.options.retryOnTimeout ) {
self.sendRequest( url, done );
}
else {
done();
}
};
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
self.options.onSuccess.call(reference, JSON.parse(this.response));
}
else {
self.options.onError.call(reference, this);
}
done();
};
request.send();
},
onAdd: function (map) {
this._map = map;
if (map.zoomIndicator) {
this._zoomControl = map.zoomIndicator;
this._zoomControl._addLayer(this);
} else {
this._zoomControl = new L.Control.MinZoomIndicator(this.options.minZoomIndicatorOptions);
map.addControl(this._zoomControl);
this._zoomControl._addLayer(this);
}
if ( !this.options.noInitialRequest ) {
this.onMoveEnd();
}
if (this.options.query.indexOf('({{bbox}})') !== -1) {
map.on('moveend', this.onMoveEnd, this);
}
},
onRemove: function (map) {
L.LayerGroup.prototype.onRemove.call(this, map);
this._ids = {};
this._requested = {};
this._zoomControl._removeLayer(this);
map.off({
'moveend': this.onMoveEnd
}, this);
this._map = null;
},
getData: function () {
return this._data;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment