|
@@ -1,196 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Leaflet.GeoJSONGridLayer
|
3
|
|
- */
|
4
|
|
-
|
5
|
|
-
|
6
|
|
-/*
|
7
|
|
- function long2tile(lon,zoom) { return (Math.floor((lon+180)/360*Math.pow(2,zoom))); }
|
8
|
|
- function lat2tile(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))); }
|
9
|
|
-
|
10
|
|
-Inverse process:
|
11
|
|
-
|
12
|
|
- function tile2long(x,z) {
|
13
|
|
- return (x/Math.pow(2,z)*360-180);
|
14
|
|
- }
|
15
|
|
- function tile2lat(y,z) {
|
16
|
|
- var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
|
17
|
|
- return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
|
18
|
|
- }
|
19
|
|
-
|
20
|
|
-Example for calculating number of tiles within given extent and zoom-level:
|
21
|
|
-
|
22
|
|
-var zoom = 9;
|
23
|
|
-var top_tile = lat2tile(north_edge, zoom); // eg.lat2tile(34.422, 9);
|
24
|
|
-var left_tile = lon2tile(west_edge, zoom);
|
25
|
|
-var bottom_tile = lat2tile(south_edge, zoom);
|
26
|
|
-var right_tile = lon2tile(east_edge, zoom);
|
27
|
|
-var width = Math.abs(left_tile - right_tile) + 1;
|
28
|
|
-var height = Math.abs(top_tile - bottom_tile) + 1;
|
29
|
|
-
|
30
|
|
-// total tiles
|
31
|
|
-var total_tiles = width * height; // -> eg. 377
|
32
|
|
-*/
|
33
|
|
-
|
34
|
|
-(function () {
|
35
|
|
-
|
36
|
|
- var console = window.console || {
|
37
|
|
- error: function () {},
|
38
|
|
- warn: function () {}
|
39
|
|
- };
|
40
|
|
-
|
41
|
|
- function defineLeafletGeoJSONGridLayer(L) {
|
42
|
|
- L.GeoJSONGridLayer = L.GridLayer.extend({
|
43
|
|
- initialize: function (url, options) {
|
44
|
|
- L.GridLayer.prototype.initialize.call(this, options);
|
45
|
|
-
|
46
|
|
- this._url = url;
|
47
|
|
- this._geojsons = {};
|
48
|
|
- this._features = {};
|
49
|
|
- this.geoJsonClass = (this.options.geoJsonClass ? this.options.geoJsonClass : L.GeoJSON);
|
50
|
|
- },
|
51
|
|
-
|
52
|
|
- onAdd: function (map) {
|
53
|
|
- var layers = this._geojsons;
|
54
|
|
- Object.keys(layers).forEach(function (key) {
|
55
|
|
- map.addLayer(layers[key]);
|
56
|
|
- });
|
57
|
|
-
|
58
|
|
- L.GridLayer.prototype.onAdd.call(this, map);
|
59
|
|
- this.zoomanimHandler = this._handleZoom.bind(this);
|
60
|
|
- map.on('zoomanim', this.zoomanimHandler);
|
61
|
|
- },
|
62
|
|
-
|
63
|
|
- onRemove: function (map) {
|
64
|
|
- var layers = this._geojsons;
|
65
|
|
- Object.keys(layers).forEach(function (key) {
|
66
|
|
- map.removeLayer(layers[key]);
|
67
|
|
- });
|
68
|
|
-
|
69
|
|
- L.GridLayer.prototype.onRemove.call(this, map);
|
70
|
|
- map.off('zoomanim', this.zoomanimHandler);
|
71
|
|
- },
|
72
|
|
-
|
73
|
|
- _handleZoom: function (e) {
|
74
|
|
- this.checkZoomConditions(e.zoom);
|
75
|
|
- },
|
76
|
|
-
|
77
|
|
- createTile: function (coords, done) {
|
78
|
|
- var tile = L.DomUtil.create('div', 'leaflet-tile');
|
79
|
|
- var size = this.getTileSize();
|
80
|
|
- tile.width = size.x;
|
81
|
|
- tile.height = size.y;
|
82
|
|
-
|
83
|
|
- this.fetchTile(coords, function (error) {
|
84
|
|
- done(error, tile);
|
85
|
|
- });
|
86
|
|
- return tile;
|
87
|
|
- },
|
88
|
|
-
|
89
|
|
- fetchTile: function (coords, done) {
|
90
|
|
- var tileUrl = L.Util.template(this._url, coords);
|
91
|
|
- var tileLayer = this;
|
92
|
|
-
|
93
|
|
- var request = new XMLHttpRequest();
|
94
|
|
- request.open('GET', tileUrl, true);
|
95
|
|
-
|
96
|
|
- request.onload = function () {
|
97
|
|
- if (request.status >= 200 && request.status < 400) {
|
98
|
|
- var data = JSON.parse(request.responseText);
|
99
|
|
- tileLayer.addData(data);
|
100
|
|
- done(null);
|
101
|
|
- } else {
|
102
|
|
- // We reached our target server, but it returned an error
|
103
|
|
- done(request.statusText);
|
104
|
|
- }
|
105
|
|
- };
|
106
|
|
-
|
107
|
|
- request.onerror = function () {
|
108
|
|
- done(request.statusText);
|
109
|
|
- };
|
110
|
|
-
|
111
|
|
- request.send();
|
112
|
|
- },
|
113
|
|
-
|
114
|
|
- getLayers: function () {
|
115
|
|
- var geojsons = this._geojsons,
|
116
|
|
- layers = [];
|
117
|
|
- Object.keys(geojsons).forEach(function (key) {
|
118
|
|
- layers.push(geojsons[key]);
|
119
|
|
- });
|
120
|
|
- return layers;
|
121
|
|
- },
|
122
|
|
-
|
123
|
|
- hasLayerWithId: function (sublayer, id) {
|
124
|
|
- if (!this._geojsons[sublayer] || !this._features[sublayer]) return false;
|
125
|
|
- return this._features[sublayer].hasOwnProperty(id);
|
126
|
|
- },
|
127
|
|
-
|
128
|
|
- addData: function (data) {
|
129
|
|
- if (data.type === 'FeatureCollection') {
|
130
|
|
- this.addSubLayerData('default', data);
|
131
|
|
- }
|
132
|
|
- else {
|
133
|
|
- var tileLayer = this;
|
134
|
|
- Object.keys(data).forEach(function (key) {
|
135
|
|
- tileLayer.addSubLayerData(key, data[key]);
|
136
|
|
- });
|
137
|
|
- }
|
138
|
|
- },
|
139
|
|
-
|
140
|
|
- addSubLayerData: function (sublayer, data) {
|
141
|
|
- if (!this._geojsons[sublayer]) {
|
142
|
|
- this._geojsons[sublayer] = new this.geoJsonClass(null, this.options.layers[sublayer]).addTo(this._map);
|
143
|
|
- this.checkZoomConditions(this._map.getZoom());
|
144
|
|
- }
|
145
|
|
- var toAdd = data.features.filter(function (feature) {
|
146
|
|
- return !this.hasLayerWithId(sublayer, feature.id ? feature.id : feature.properties.id);
|
147
|
|
- }, this);
|
148
|
|
-
|
149
|
|
- if (!this._features[sublayer]) {
|
150
|
|
- this._features[sublayer] = {};
|
151
|
|
- }
|
152
|
|
- toAdd.forEach(function (feature) {
|
153
|
|
- var id = feature.id ? feature.id : feature.properties.id;
|
154
|
|
- this._features[sublayer][id] = feature;
|
155
|
|
- }, this);
|
156
|
|
-
|
157
|
|
- this._geojsons[sublayer].addData({
|
158
|
|
- type: 'FeatureCollection',
|
159
|
|
- features: toAdd
|
160
|
|
- });
|
161
|
|
- },
|
162
|
|
-
|
163
|
|
- checkZoomConditions: function (zoom) {
|
164
|
|
- var layers = this._geojsons,
|
165
|
|
- map = this._map;
|
166
|
|
- Object.keys(layers).forEach(function (key) {
|
167
|
|
- var layer = layers[key],
|
168
|
|
- options = layer.options;
|
169
|
|
- if ((options.maxZoom && zoom > options.maxZoom) ||
|
170
|
|
- (options.minZoom && zoom < options.minZoom)) {
|
171
|
|
- map.removeLayer(layer);
|
172
|
|
- }
|
173
|
|
- else {
|
174
|
|
- map.addLayer(layer);
|
175
|
|
- }
|
176
|
|
- });
|
177
|
|
- }
|
178
|
|
- });
|
179
|
|
-
|
180
|
|
- L.geoJsonGridLayer = function(url, options) {
|
181
|
|
- return new L.GeoJSONGridLayer(url, options);
|
182
|
|
- };
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
- if (typeof define === 'function' && define.amd) {
|
186
|
|
- // Try to add leaflet.loading to Leaflet using AMD
|
187
|
|
- define(['leaflet'], function (L) {
|
188
|
|
- defineLeafletGeoJSONGridLayer(L);
|
189
|
|
- });
|
190
|
|
- }
|
191
|
|
- else {
|
192
|
|
- // Else use the global L
|
193
|
|
- defineLeafletGeoJSONGridLayer(L);
|
194
|
|
- }
|
195
|
|
-
|
196
|
|
-})();
|