Kaynağa Gözat

support tiles in GeoJSON

Maurits van der Schee 4 yıl önce
ebeveyn
işleme
6814e293d9

+ 0
- 196
examples/clients/leaflet/GeoJSONGridLayer.js Dosyayı Görüntüle

@@ -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
-})();

+ 12
- 24
examples/clients/leaflet/geojson-tile-layer.js Dosyayı Görüntüle

@@ -18,43 +18,31 @@
18 18
 
19 19
         createTile: function (coords) {
20 20
             var tile = L.DomUtil.create('div', 'leaflet-tile');
21
+            tile.style['box-shadow'] = 'inset 0 0 2px #f00';
21 22
             var url = L.Util.template(this.url, coords);
22 23
             this.ajaxRequest('GET', url, false, this.updateLayers.bind(this));
23 24
             return tile;
24 25
         },
25 26
 
26
-        ajaxRequest: function(method, url, data, callback) {
27
-            var request = new XMLHttpRequest();
28
-            request.open(method, url, true);
29
-            request.onreadystatechange = function() {
30
-                if (request.readyState === 4 && request.status === 200) {
31
-                    callback(JSON.parse(request.responseText));
32
-                }
33
-            };
34
-            if (data) {
35
-                request.setRequestHeader('Content-type', 'application/json');
36
-                request.send(JSON.stringify(data));
37
-            } else {
38
-                request.send();
39
-            }		
40
-            return request;
41
-        },
42
-
43 27
         updateLayers: function(geoData) {
44
-            this.layer.clearLayers();
45
-            this.layer.addData(geoData);
28
+            if (geoData.type == 'FeatureCollection'){
29
+                for (var i=0;i<geoData.features.length;i++) {
30
+                    var id = geoData.features[i].id;
31
+                    if (!this.features[id]) {
32
+                        this.layer.addData(geoData.features[i]);
33
+                        this.features[id] = true;
34
+                    }
35
+                }
36
+            }            
46 37
         },
47 38
 
48 39
         onAdd(map) {
49 40
             L.GridLayer.prototype.onAdd.call(this, map); 
50 41
             map.addLayer(this.layer);
51 42
             this.map = map;
52
-            //map.on('moveend zoomend refresh', this.reloadMap, this);
53
-            //this.reloadMap();
54 43
         },
55 44
 
56 45
         onRemove(map) {
57
-            //map.off('moveend zoomend refresh', this.reloadMap, this);
58 46
             this.map = null;
59 47
             map.removeLayer(this.layer)
60 48
             L.GridLayer.prototype.onRemove.call(this, map);
@@ -79,8 +67,8 @@
79 67
 
80 68
     });
81 69
 
82
-    L.geoJSONTileLayer = function (options) {
83
-        return new L.GeoJSONTileLayer(options);
70
+    L.geoJSONTileLayer = function (url, options) {
71
+        return new L.GeoJSONTileLayer(url, options);
84 72
     };
85 73
 
86 74
 }).call(this);

+ 8
- 7
examples/clients/leaflet/vanilla.html Dosyayı Görüntüle

@@ -12,6 +12,7 @@
12 12
     <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"></script>
13 13
 	<script src="geojson-layer.js"></script>
14 14
 	<script src="geojson-tile-layer.js"></script>
15
+	<!--<script src="geojson-tile-layer.js"></script>-->
15 16
 </head>
16 17
 <body>
17 18
 
@@ -19,15 +20,15 @@
19 20
 <script>
20 21
 	var mymap = L.map('mapid').setView([20, 30], 3);
21 22
 
22
-	//L.tileLayer('https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}', {
23
-	// 	maxZoom: 18,
24
-	//}).addTo(mymap);
23
+	L.tileLayer('https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}', {
24
+		maxZoom: 18,
25
+	}).addTo(mymap);
25 26
 
26
-	//L.geoJSONLayer('http://localhost:8000/api.php/geojson/countries/1,2?bbox={bbox}', {
27
-	// 	maxZoom: 18,
28
-	//}).addTo(mymap);
27
+	L.geoJSONLayer('http://localhost:8000/api.php/geojson/users?bbox={bbox}', {
28
+		maxZoom: 18,
29
+	}).addTo(mymap);
29 30
 
30
-	L.geoJSONTileLayer('http://localhost:8000/api.php/geojson/countries/1,2?tile={z},{y},{x}', {
31
+	L.geoJSONTileLayer('http://localhost:8000/src/geojson/countries?filter=id,lt,3&tile={z},{x},{y}', {
31 32
 		maxZoom: 18,
32 33
 	}).addTo(mymap);
33 34
 </script>

Loading…
İptal
Kaydet