/
Leaflet.VectorGrid.js
279 lines (226 loc) · 8.01 KB
/
Leaflet.VectorGrid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import {} from './Leaflet.Renderer.SVG.Tile.js';
import { PointSymbolizer } from './Symbolizer.Point.js';
import { LineSymbolizer } from './Symbolizer.Line.js';
import { FillSymbolizer } from './Symbolizer.Fill.js';
/* 🍂class VectorGrid
* 🍂inherits GridLayer
*
* A `VectorGrid` is a generic, abstract class for displaying tiled vector data.
* it provides facilities for symbolizing and rendering the data in the vector
* tiles, but lacks the functionality to fetch the vector tiles from wherever
* they are.
*
* Extends Leaflet's `L.GridLayer`.
*/
L.VectorGrid = L.GridLayer.extend({
options: {
// 🍂option rendererFactory = L.svg.tile
// A factory method which will be used to instantiate the per-tile renderers.
rendererFactory: L.svg.tile,
// 🍂option vectorTileLayerStyles: Object = {}
// A data structure holding initial symbolizer definitions for the vector features.
vectorTileLayerStyles: {},
// 🍂option interactive: Boolean = false
// Whether this `VectorGrid` fires `Interactive Layer` events.
interactive: false,
// 🍂option getFeatureId: Function = undefined
// A function that, given a vector feature, returns an unique identifier for it, e.g.
// `function(feat) { return feat.properties.uniqueIdField; }`.
// Must be defined for `setFeatureStyle` to work.
// 🍂option filter: Function = undefined
// A Function that will be used to decide whether to include a feature or not
// depending on feature properties and zoom, e.g.
// `function(properties, zoom) { return true; }`.
// The default is to include all features. Similar to L.GeoJSON filter option.
},
initialize: function(options) {
L.setOptions(this, options);
L.GridLayer.prototype.initialize.apply(this, arguments);
if (this.options.getFeatureId) {
this._vectorTiles = {};
this._overriddenStyles = {};
this.on('tileunload', function(e) {
var key = this._tileCoordsToKey(e.coords),
tile = this._vectorTiles[key];
if (tile && this._map) {
tile.removeFrom(this._map);
}
delete this._vectorTiles[key];
}, this);
}
this._dataLayerNames = {};
},
createTile: function(coords, done) {
var storeFeatures = this.options.getFeatureId;
var tileSize = this.getTileSize();
var renderer = this.options.rendererFactory(coords, tileSize, this.options);
var tileBounds = this._tileCoordsToBounds(coords);
var vectorTilePromise = this._getVectorTilePromise(coords, tileBounds);
if (storeFeatures) {
this._vectorTiles[this._tileCoordsToKey(coords)] = renderer;
renderer._features = {};
}
vectorTilePromise.then( function renderTile(vectorTile) {
if (vectorTile.layers && vectorTile.layers.length !== 0) {
for (var layerName in vectorTile.layers) {
this._dataLayerNames[layerName] = true;
var layer = vectorTile.layers[layerName];
var pxPerExtent = this.getTileSize().divideBy(layer.extent);
var layerStyle = this.options.vectorTileLayerStyles[ layerName ] ||
L.Path.prototype.options;
for (var i = 0; i < layer.features.length; i++) {
var feat = layer.features[i];
var id;
if (this.options.filter instanceof Function &&
!this.options.filter(feat.properties, coords.z)) {
continue;
}
var styleOptions = layerStyle;
if (storeFeatures) {
id = this.options.getFeatureId(feat);
var styleOverride = this._overriddenStyles[id];
if (styleOverride) {
if (styleOverride[layerName]) {
styleOptions = styleOverride[layerName];
} else {
styleOptions = styleOverride;
}
}
}
if (styleOptions instanceof Function) {
styleOptions = styleOptions(feat.properties, coords.z);
}
if (!(styleOptions instanceof Array)) {
styleOptions = [styleOptions];
}
if (!styleOptions.length) {
continue;
}
var featureLayer = this._createLayer(feat, pxPerExtent);
for (var j = 0; j < styleOptions.length; j++) {
var style = L.extend({}, L.Path.prototype.options, styleOptions[j]);
featureLayer.render(renderer, style);
renderer._addPath(featureLayer);
}
if (this.options.interactive) {
featureLayer.makeInteractive();
}
if (storeFeatures) {
// multiple features may share the same id, add them
// to an array of features
if (!renderer._features[id]) {
renderer._features[id] = [];
}
renderer._features[id].push({
layerName: layerName,
feature: featureLayer
});
}
}
}
}
if (this._map != null) {
renderer.addTo(this._map);
}
L.Util.requestAnimFrame(done.bind(coords, null, null));
}.bind(this));
return renderer.getContainer();
},
// 🍂method setFeatureStyle(id: Number, layerStyle: L.Path Options): this
// Given the unique ID for a vector features (as per the `getFeatureId` option),
// re-symbolizes that feature across all tiles it appears in.
setFeatureStyle: function(id, layerStyle) {
this._overriddenStyles[id] = layerStyle;
for (var tileKey in this._vectorTiles) {
var tile = this._vectorTiles[tileKey];
var features = tile._features[id];
if (features) {
for (var i=0; i<features.length; i++) {
var feature = features[i];
var styleOptions = layerStyle;
if (layerStyle[feature.layerName]) {
styleOptions = layerStyle[feature.layerName];
}
this._updateStyles(feature.feature, tile, styleOptions);
}
}
}
return this;
},
// 🍂method setFeatureStyle(id: Number): this
// Reverts the effects of a previous `setFeatureStyle` call.
resetFeatureStyle: function(id) {
delete this._overriddenStyles[id];
for (var tileKey in this._vectorTiles) {
var tile = this._vectorTiles[tileKey];
var features = tile._features[id];
if (features) {
for (var i=0; i<features.length; i++) {
var feature = features[i];
var styleOptions = this.options.vectorTileLayerStyles[feature.layerName] ||
L.Path.prototype.options;
this._updateStyles(feature.feature, tile, styleOptions);
}
}
}
return this;
},
// 🍂method setFilter(filterFn: Function): this
// Sets filter function to filter displayed features.
setFilter: function (filterFn) {
this.options.filter = filterFn;
this.redraw();
return this;
},
// 🍂method getDataLayerNames(): Array
// Returns an array of strings, with all the known names of data layers in
// the vector tiles displayed. Useful for introspection.
getDataLayerNames: function() {
return Object.keys(this._dataLayerNames);
},
_updateStyles: function(feat, renderer, styleOptions) {
styleOptions = (styleOptions instanceof Function) ?
styleOptions(feat.properties, renderer.getCoord().z) :
styleOptions;
if (!(styleOptions instanceof Array)) {
styleOptions = [styleOptions];
}
for (var j = 0; j < styleOptions.length; j++) {
var style = L.extend({}, L.Path.prototype.options, styleOptions[j]);
feat.updateStyle(renderer, style);
}
},
_createLayer: function(feat, pxPerExtent, layerStyle) {
var layer;
switch (feat.type) {
case 1:
layer = new PointSymbolizer(feat, pxPerExtent);
//prevent leaflet from treating these canvas points as real markers
layer.getLatLng = null;
break;
case 2:
layer = new LineSymbolizer(feat, pxPerExtent);
break;
case 3:
layer = new FillSymbolizer(feat, pxPerExtent);
break;
}
if (this.options.interactive) {
layer.addEventParent(this);
}
return layer;
},
});
/*
* 🍂section Extension methods
*
* Classes inheriting from `VectorGrid` **must** define the `_getVectorTilePromise` private method.
*
* 🍂method getVectorTilePromise(coords: Object): Promise
* Given a `coords` object in the form of `{x: Number, y: Number, z: Number}`,
* this function must return a `Promise` for a vector tile.
*
*/
L.vectorGrid = function (options) {
return new L.VectorGrid(options);
};