From 90a08c29efe055f5cf89fa31e70daeb6382ee6cd Mon Sep 17 00:00:00 2001 From: David Molineus Date: Wed, 9 Nov 2016 10:00:02 +0100 Subject: [PATCH] Add layer overpass. --- assets/leaflet-layer-overpass/LICENSE | 9 + .../leaflet-layer-overpass/OverPassLayer.css | 8 + .../leaflet-layer-overpass/OverPassLayer.js | 333 ++++++++++++++++++ .../OverPassLayer.min.js | 1 + assets/leaflet-layer-overpass/README.md | 101 ++++++ assets/leaflet-layer-overpass/bower.json | 31 ++ assets/leaflet-layer-overpass/gulpfile.js | 14 + assets/leaflet-layer-overpass/package.json | 21 ++ gulpfile.js | 4 + module/config/config.php | 10 + 10 files changed, 532 insertions(+) create mode 100644 assets/leaflet-layer-overpass/LICENSE create mode 100644 assets/leaflet-layer-overpass/OverPassLayer.css create mode 100644 assets/leaflet-layer-overpass/OverPassLayer.js create mode 100644 assets/leaflet-layer-overpass/OverPassLayer.min.js create mode 100644 assets/leaflet-layer-overpass/README.md create mode 100644 assets/leaflet-layer-overpass/bower.json create mode 100644 assets/leaflet-layer-overpass/gulpfile.js create mode 100644 assets/leaflet-layer-overpass/package.json diff --git a/assets/leaflet-layer-overpass/LICENSE b/assets/leaflet-layer-overpass/LICENSE new file mode 100644 index 0000000..93b5069 --- /dev/null +++ b/assets/leaflet-layer-overpass/LICENSE @@ -0,0 +1,9 @@ +Copyright (c) 2013 Karsten Hinz + +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. diff --git a/assets/leaflet-layer-overpass/OverPassLayer.css b/assets/leaflet-layer-overpass/OverPassLayer.css new file mode 100644 index 0000000..9a644e8 --- /dev/null +++ b/assets/leaflet-layer-overpass/OverPassLayer.css @@ -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; +} diff --git a/assets/leaflet-layer-overpass/OverPassLayer.js b/assets/leaflet-layer-overpass/OverPassLayer.js new file mode 100644 index 0000000..3f8f0e8 --- /dev/null +++ b/assets/leaflet-layer-overpass/OverPassLayer.js @@ -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); +//}; diff --git a/assets/leaflet-layer-overpass/OverPassLayer.min.js b/assets/leaflet-layer-overpass/OverPassLayer.min.js new file mode 100644 index 0000000..6fadba9 --- /dev/null +++ b/assets/leaflet-layer-overpass/OverPassLayer.min.js @@ -0,0 +1 @@ +L.Control.MinZoomIndicator=L.Control.extend({options:{position:"bottomleft"},_layers:{},initialize:function(t){L.Util.setOptions(this,t),this._layers=new Object},_addLayer:function(t){var e=15;t.options.minzoom&&(e=t.options.minzoom),this._layers[t._leaflet_id]=e,this._updateBox(null)},_removeLayer:function(t){this._layers[t._leaflet_id]=null,this._updateBox(null)},_getMinZoomLevel:function(){var t=-1;for(var e in this._layers)null!=this._layers[e]&&this._layers[e]>t&&(t=this._layers[e]);return t},onAdd:function(t){this._map=t,t.zoomIndicator=this;var e=this.className,o=this._container=L.DomUtil.create("div",e);return t.on("moveend",this._updateBox,this),this._updateBox(null),o},onRemove:function(t){L.Control.prototype.onRemove.call(this,t),t.off({moveend:this._updateBox},this),this._map=null},_updateBox:function(t){null!=t&&L.DomEvent.preventDefault(t);var e=this._getMinZoomLevel();this._container.innerHTML=-1==e?this.options.minZoomMessageNoLayer:this.options.minZoomMessage.replace(/CURRENTZOOM/,this._map.getZoom()).replace(/MINZOOMLEVEL/,e),this._container.style.display=this._map.getZoom()>=e?"none":"block"},className:"leaflet-control-minZoomIndicator"}),L.LatLngBounds.prototype.toOverpassBBoxString=function(){var t=this._southWest,e=this._northEast;return[t.lat,t.lng,e.lat,e.lng].join(",")},L.OverPassLayer=L.FeatureGroup.extend({options:{debug:!1,minzoom:15,endpoint:"http://overpass-api.de/api/",query:"(node(BBOX)[organic];node(BBOX)[second_hand];);out qt;",callback:function(t){for(var e=0;e=d;d++)for(var u=r;l>=u;u++){var p=Math.round(1e6*this.tile2long(d,i))/1e6,c=Math.round(1e6*this.tile2long(d+1,i))/1e6,m=Math.round(1e6*this.tile2lat(u,i))/1e6,_=Math.round(1e6*this.tile2lat(u+1,i))/1e6;h.push(new L.LatLngBounds(new L.LatLng(_,p),new L.LatLng(m,c)))}return h},addBBox:function(t,e,o,n){L.polygon([[n,t],[e,t],[e,o],[n,o]]).addTo(this._map)},onMoveEnd:function(){if(this.options.debug&&console.debug("load Pois"),this._map.getZoom()>=this.options.minzoom)for(var t=this._view2BBoxes(this._map.getBounds()._southWest.lng,this._map.getBounds()._southWest.lat,this._map.getBounds()._northEast.lng,this._map.getBounds()._northEast.lat),e=0,o=t.length,n=!0,i=0;i=200&&this.status<400){var t={instance:d};d.options.callback.call(t,JSON.parse(this.response)),d.options.debug&&console.debug("queryCount: "+o+" - finishedCount: "+e),++e==o&&d.options.afterRequest.call(d)}},u.send()}}},onAdd:function(t){this._map=t,t.zoomIndicator?(this._zoomControl=t.zoomIndicator,this._zoomControl._addLayer(this)):(this._zoomControl=new L.Control.MinZoomIndicator(this.options.minZoomIndicatorOptions),t.addControl(this._zoomControl),this._zoomControl._addLayer(this)),this.onMoveEnd(),-1!=this.options.query.indexOf("(BBOX)")&&t.on("moveend",this.onMoveEnd,this),this.options.debug&&console.debug("add layer")},onRemove:function(t){this.options.debug&&console.debug("remove layer"),L.LayerGroup.prototype.onRemove.call(this,t),this._ids={},this._requested={},this._zoomControl._removeLayer(this),t.off({moveend:this.onMoveEnd},this),this._map=null},getData:function(){return this.options.debug&&console.debug(this._data),this._data}}); \ No newline at end of file diff --git a/assets/leaflet-layer-overpass/README.md b/assets/leaflet-layer-overpass/README.md new file mode 100644 index 0000000..6435f2e --- /dev/null +++ b/assets/leaflet-layer-overpass/README.md @@ -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 © OpenStreetMap contributors', +attr_overpass = 'POI via Overpass API'; +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 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 + diff --git a/assets/leaflet-layer-overpass/bower.json b/assets/leaflet-layer-overpass/bower.json new file mode 100644 index 0000000..fbceb00 --- /dev/null +++ b/assets/leaflet-layer-overpass/bower.json @@ -0,0 +1,31 @@ +{ + "name": "leaflet-layer-overpass", + "version": "1.0.2", + "homepage": "https://github.com/kartenkarsten/leaflet-layer-overpass", + "authors": [ + "Karsten Hinz ", + "Knut Hühne " + ], + "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" + } +} diff --git a/assets/leaflet-layer-overpass/gulpfile.js b/assets/leaflet-layer-overpass/gulpfile.js new file mode 100644 index 0000000..5a19c45 --- /dev/null +++ b/assets/leaflet-layer-overpass/gulpfile.js @@ -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/')); +}); + diff --git a/assets/leaflet-layer-overpass/package.json b/assets/leaflet-layer-overpass/package.json new file mode 100644 index 0000000..19acaa0 --- /dev/null +++ b/assets/leaflet-layer-overpass/package.json @@ -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" +} diff --git a/gulpfile.js b/gulpfile.js index 537a5cd..b5ad94d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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' } ]; diff --git a/module/config/config.php b/module/config/config.php index 08d0ec9..1309a98 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -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' => 'License', + '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',