Add layer overpass.

This commit is contained in:
David Molineus
2016-11-09 10:00:02 +01:00
parent 0cd3b761a8
commit 90a08c29ef
10 changed files with 532 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
Copyright (c) 2013 Karsten Hinz <k.hinz(at)tu-bs.de>
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.

View File

@@ -0,0 +1,8 @@
.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;
}

View File

@@ -0,0 +1,333 @@
L.Control.MinZoomIndicator = L.Control.extend({
options: {
position: 'bottomleft',
},
/**
* map: layerId -> zoomlevel
*/
_layers: {},
/** TODO check if nessesary
*/
initialize: function (options) {
L.Util.setOptions(this, options);
this._layers = new Object();
},
/**
* 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 minZoomlevel=-1;
for(var 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;
var container = this._container = L.DomUtil.create('div', className);
map.on('moveend', this._updateBox, this);
this._updateBox(null);
// L.DomEvent.disableClickPropagation(container);
return container;
},
onRemove: function(map) {
L.Control.prototype.onRemove.call(this, map);
map.off({
'moveend': this._updateBox
}, this);
this._map = null;
},
_updateBox: function (event) {
//console.log("map moved -> update Container...");
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: {
debug: false,
minzoom: 15,
endpoint: "http://overpass-api.de/api/",
query: "(node(BBOX)[organic];node(BBOX)[second_hand];);out qt;",
callback: function(data) {
for(var i = 0; i < data.elements.length; i++) {
var e = data.elements[i];
if (e.id in this.instance._ids) return;
this.instance._ids[e.id] = true;
var pos;
if (e.type == "node") {
pos = new L.LatLng(e.lat, e.lon);
} else {
pos = new L.LatLng(e.center.lat, e.center.lon);
}
var popup = this.instance._poiInfo(e.tags,e.id);
var circle = L.circle(pos, 50, {
color: 'green',
fillColor: '#3f0',
fillOpacity: 0.5
})
.bindPopup(popup);
this.instance.addLayer(circle);
}
},
beforeRequest: function() {
if (this.options.debug) {
console.debug('about to query the OverPassAPI');
}
},
afterRequest: function() {
if (this.options.debug) {
console.debug('all queries have finished!');
}
},
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 = {};
// save position of the layer or any options from the constructor
this._ids = {};
this._requested = {};
},
_poiInfo: function(tags,id) {
var link = document.createElement("a");
link.href = "http://www.openstreetmap.org/edit?editor=id&node=" + id;
link.appendChild(document.createTextNode("Edit this entry in iD"));
var table = document.createElement('table');
for (var key in tags){
var row = table.insertRow(0);
row.insertCell(0).appendChild(document.createTextNode(key));
row.insertCell(1).appendChild(document.createTextNode(tags[key]));
}
var div = document.createElement("div")
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) {
//console.log(l+"\t"+b+"\t"+r+"\t"+t);
//this.addBBox(l,b,r,t);
//console.log("calc bboxes");
var requestZoomLevel= 14;
//get left tile index
var lidx = this.long2tile(l,requestZoomLevel);
var ridx = this.long2tile(r,requestZoomLevel);
var tidx = this.lat2tile(t,requestZoomLevel);
var bidx = this.lat2tile(b,requestZoomLevel);
//var result;
var result = new Array();
for (var x=lidx; x<=ridx; x++) {
for (var y=tidx; y<=bidx; y++) {//in tiles tidx<=bidx
var left = Math.round(this.tile2long(x,requestZoomLevel)*1000000)/1000000;
var right = Math.round(this.tile2long(x+1,requestZoomLevel)*1000000)/1000000;
var top = Math.round(this.tile2lat(y,requestZoomLevel)*1000000)/1000000;
var bottom = Math.round(this.tile2lat(y+1,requestZoomLevel)*1000000)/1000000;
//console.log(left+"\t"+bottom+"\t"+right+"\t"+top);
//this.addBBox(left,bottom,right,top);
//console.log("http://osm.org?bbox="+left+","+bottom+","+right+","+top);
result.push( new L.LatLngBounds(new L.LatLng(bottom, left),new L.LatLng(top, right)));
}
}
//console.log(result);
return result;
},
addBBox: function (l,b,r,t) {
var polygon = L.polygon([
[t, l],
[b, l],
[b, r],
[t, r]
]).addTo(this._map);
},
onMoveEnd: function () {
if (this.options.debug) {
console.debug("load Pois");
}
//console.log(this._map.getBounds());
if (this._map.getZoom() >= this.options.minzoom) {
//var bboxList = new Array(this._map.getBounds());
var bboxList = this._view2BBoxes(
this._map.getBounds()._southWest.lng,
this._map.getBounds()._southWest.lat,
this._map.getBounds()._northEast.lng,
this._map.getBounds()._northEast.lat);
// controls the after/before (Request) callbacks
var finishedCount = 0;
var queryCount = bboxList.length;
var beforeRequest = true;
for (var i = 0; i < bboxList.length; i++) {
var bbox = bboxList[i];
var x = bbox._southWest.lng;
var y = bbox._northEast.lat;
if ((x in this._requested) && (y in this._requested[x]) && (this._requested[x][y] == true)) {
queryCount--;
continue;
}
if (!(x in this._requested)) {
this._requested[x] = {};
}
this._requested[x][y] = true;
var queryWithMapCoordinates = this.options.query.replace(/(BBOX)/g, bbox.toOverpassBBoxString());
var url = this.options.endpoint + "interpreter?data=[out:json];" + queryWithMapCoordinates;
if (beforeRequest) {
this.options.beforeRequest.call(this);
beforeRequest = false;
}
var self = this;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
var reference = {instance: self};
self.options.callback.call(reference, JSON.parse(this.response));
if (self.options.debug) {
console.debug('queryCount: ' + queryCount + ' - finishedCount: ' + finishedCount);
}
if (++finishedCount == queryCount) {
self.options.afterRequest.call(self);
}
}
};
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);
}
this.onMoveEnd();
if (this.options.query.indexOf("(BBOX)") != -1) {
map.on('moveend', this.onMoveEnd, this);
}
if (this.options.debug) {
console.debug("add layer");
}
},
onRemove: function (map) {
if (this.options.debug) {
console.debug("remove layer");
}
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 () {
if (this.options.debug) {
console.debug(this._data);
}
return this._data;
}
});
//FIXME no idea why the browser crashes with this code
//L.OverPassLayer = function (options) {
// return new L.OverPassLayer(options);
//};

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,101 @@
Leaflet Layer OverPass
=============================
[![Bower version](https://badge.fury.io/bo/leaflet-layer-overpass.svg)](http://badge.fury.io/bo/leaflet-layer-overpass)
## What is it?
A [Leaflet](http://leafletjs.com/) plugin to create a custom POI overlay - thanks to the [OSM](http://www.openstreetmap.org/) dataset and the [Overpass API](http://overpass-api.de/)
checkout the [demo](http://kartenkarsten.github.io/leaflet-layer-overpass/demo/)
## Installation
You can use bower to install leaflet-layer-overpass.
Simply run
```bash
$ bower install --save leaflet-layer-overpass
```
After that you can include and use the `OverpassLayer.css` and `OverpassLayer.js` files (or `OverPassLayer.min.js` if you want the minified version) from the `dist` folder in your html.
## How to use it?
```javascript
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);
//OverPassAPI overlay
var opl = new L.OverPassLayer({
query: "node(BBOX)['amenity'='post_box'];out;",
});
map.addLayer(opl);
```
In order to get a valid query the [Overpass-turbo IDE](http://overpass-turbo.eu/) might help.
## What are the options?
You can specify an options object as an argument of L.OverPassLayer.
```javascript
options: {
endpoint: "http://overpass.osm.rambler.ru/cgi/",
query: "node(BBOX)['amenity'='post_box'];out;",
debug: false,
callback: function(data) {
for(var i=0;i<data.elements.length;i++) {
var e = data.elements[i];
if (e.id in this.instance._ids) return;
this.instance._ids[e.id] = true;
var pos = new L.LatLng(e.lat, e.lon);
var popup = this.instance._poiInfo(e.tags,e.id);
var color = e.tags.collection_times ? 'green':'red';
var circle = L.circle(pos, 50, {
color: color,
fillColor: '#fa3',
fillOpacity: 0.5
})
.bindPopup(popup);
this.instance.addLayer(circle);
}
},
minZoomIndicatorOptions: {
position: 'topright',
minZoomMessageNoLayer: "no layer assigned",
minZoomMessage: "current Zoom-Level: CURRENTZOOM all data at Level: MINZOOMLEVEL"
}
};
```
## Used by
- [Gdzie](http://gdzie.bl.ee/#!7/51.495/20.995/)
- [pois.elblogdehumitos.com.ar](http://pois.elblogdehumitos.com.ar/)
- [briefkastenkarte.de](http://briefkastenkarte.de/)
## Dependencies
- Leaflet (tried with version 0.6.2, 0.7.3)
## Development
In order to contribute to the project you should first clone the repository. The javascript source files
reside in the `src` folder and are concatenated and minified by gulp. If you want to make changes
make them in the `src` folder and then build the `dist` file with gulp.
For that you first need to install gulp if you do not have installed it yet
```
$ npm install --global gulp
```
Then install all the needed packages for this project:
```
$ npm install
```
And then just run
```
gulp
```
after you made your changes. This will combine (and minify) the files and put them into the `dist` folder.
## Further Ideas
- OverPass result to -> geoJSON to -> Leaflet Layer to support ways and areas as well (see also [PoiMap](https://github.com/simon04/POImap/blob/master/railway.html), [OverPassTurbo](https://github.com/tyrasd/overpass-ide/blob/gh-pages/js/overpass.js))
- improve popup text. use links, format addresses and contact details (compare with [OpenLinkMap](http://www.openlinkmap.org/))
- improve caching - allow to store data for some days in browser

View File

@@ -0,0 +1,31 @@
{
"name": "leaflet-layer-overpass",
"version": "1.0.2",
"homepage": "https://github.com/kartenkarsten/leaflet-layer-overpass",
"authors": [
"Karsten Hinz <k.hinz@tu-bs.de>",
"Knut Hühne <knut@k-nut.eu>"
],
"description": "simply add an overpass layer to a leafleat map",
"main": "OverPassLayer.js",
"keywords": [
"leaflet",
"overpass",
"openstreetmap",
"osm",
"features",
"layer"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"demo",
"test",
"tests"
],
"dependencies": {
"leaflet": "~0.7.3"
}
}

View File

@@ -0,0 +1,14 @@
var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
gulp.task('default', function() {
return gulp.src('./src/*.js')
.pipe(concat('OverPassLayer.js'))
.pipe(gulp.dest('./dist/'))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest('./dist/'));
});

View File

@@ -0,0 +1,21 @@
{
"name": "leaflet-layer-overpass",
"version": "1.0.1",
"main": "gulpfile.js",
"devDependencies": {
"gulp-concat": "^2.5.0",
"gulp-rename": "^1.2.0",
"gulp": "^3.8.11",
"gulp-uglify": "^1.1.0"
},
"dependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/kartenkarsten/leaflet-layer-overpass/"
},
"author": "Karsten Hinz",
"license": "MIT"
}

View File

@@ -29,6 +29,10 @@ var paths = [
dest: 'assets/leaflet-fullscreen',
css: 'Control.FullScreen.css',
js: 'Control.FullScreen.js'
},
{
dest: 'assets/leaflet-layer-overpass',
css: 'OverPassLayer.css'
}
];

View File

@@ -72,6 +72,16 @@ $GLOBALS['LEAFLET_LIBRARIES']['leaflet-fullscreen'] = array
'javascript' => 'assets/leaflet/libs/leaflet-fullscreen/Control.FullScreen.min.js'
);
$GLOBALS['LEAFLET_LIBRARIES']['leaflet-layer-overpass'] = array
(
'name' => 'Leaflet Layer OverPass',
'version' => '1.0.2',
'license' => '<a href="https://github.com/kartenkarsten/leaflet-layer-overpass/blob/master/LICENSE" target="_blank">License</a>',
'homepage' => 'https://github.com/kartenkarsten/leaflet-layer-overpass',
'css' => 'assets/leaflet/libs/leaflet-layer-overpass/OverPassLayer.css',
'javascript' => 'assets/leaflet/libs/leaflet-layer-overpass/OverPassLayer.min.js'
);
$GLOBALS['LEAFLET_LIBRARIES']['leaflet-control-geocoder'] = array
(
'name' => 'Leaflet Control Geocoder',