MVTFeature.js
10.6 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
function MVTFeature(mvtLayer, vtf, ctx, id, style) {
if (!vtf) return null;
// Apply all of the properties of vtf to this object.
for (var key in vtf) {
this[key] = vtf[key];
}
this.mvtLayer = mvtLayer;
this.mvtSource = mvtLayer.mvtSource;
this.map = mvtLayer.mvtSource.map;
this.id = id;
this.layerLink = this.mvtSource.layerLink;
this.toggleEnabled = true;
this.selected = false;
// how much we divide the coordinate from the vector tile
this.divisor = vtf.extent / ctx.tileSize;
this.extent = vtf.extent;
this.tileSize = ctx.tileSize;
//An object to store the paths and contexts for this feature
this.tiles = {};
this.style = style;
//Add to the collection
this.addTileFeature(vtf, ctx);
var self = this;
this.map.on('zoomend', function() {
self.staticLabel = null;
});
if (style && style.dynamicLabel && typeof style.dynamicLabel === 'function') {
this.dynamicLabel = this.mvtSource.dynamicLabel.createFeature(this);
}
ajax(self);
}
function ajax(self) {
var style = self.style;
if (style && style.ajaxSource && typeof style.ajaxSource === 'function') {
var ajaxEndpoint = style.ajaxSource(self);
if (ajaxEndpoint) {
Util.getJSON(ajaxEndpoint, function(error, response, body) {
if (error) {
throw ['ajaxSource AJAX Error', error];
} else {
ajaxCallback(self, response);
return true;
}
});
}
}
return false;
}
function ajaxCallback(self, response) {
self.ajaxData = response;
/**
* You can attach a callback function to a feature in your app
* that will get called whenever new ajaxData comes in. This
* can be used to update UI that looks at data from within a feature.
*
* setStyle may possibly have a style with a different ajaxData source,
* and you would potentially get new contextual data for your feature.
*
* TODO: This needs to be documented.
*/
if (typeof self.ajaxDataReceived === 'function') {
self.ajaxDataReceived(self, response);
}
self._setStyle(self.mvtLayer.style);
redrawTiles(self);
}
MVTFeature.prototype._setStyle = function(styleFn) {
this.style = styleFn(this, this.ajaxData);
// The label gets removed, and the (re)draw,
// that is initiated by the MVTLayer creates a new label.
this.removeLabel();
};
MVTFeature.prototype.setStyle = function(styleFn) {
this.ajaxData = null;
this.style = styleFn(this, null);
var hasAjaxSource = ajax(this);
if (!hasAjaxSource) {
// The label gets removed, and the (re)draw,
// that is initiated by the MVTLayer creates a new label.
this.removeLabel();
}
};
MVTFeature.prototype.draw = function(canvasID) {
//Get the info from the tiles list
var tileInfo = this.tiles[canvasID];
var vtf = tileInfo.vtf;
var ctx = tileInfo.ctx;
//Get the actual canvas from the parent layer's _tiles object.
var xy = canvasID.split(":").slice(1, 3).join(":");
ctx.canvas = this.mvtLayer._tiles[xy];
// This could be used to directly compute the style function from the layer on every draw.
// This is much less efficient...
// this.style = this.mvtLayer.style(this);
if (this.selected) {
var style = this.style.selected || this.style;
} else {
var style = this.style;
}
var coordinates = vtf.coordinates;
switch (vtf.type) {
case 1: //Point
this._drawPoint(ctx, coordinates, style);
if (!this.staticLabel && typeof this.style.staticLabel === 'function') {
if (this.style.ajaxSource && !this.ajaxData) {
break;
}
this._drawStaticLabel(ctx, coordinates, style);
}
break;
case 2: //LineString
this._drawLineString(ctx, coordinates, style);
break;
case 3: //Polygon
this._drawPolygon(ctx, coordinates, style);
break;
default:
throw new Error('Unmanaged type: ' + vtf.type);
}
};
MVTFeature.prototype.getPathsForTile = function(canvasID) {
//Get the info from the parts list
return this.tiles[canvasID].paths;
};
MVTFeature.prototype.addTileFeature = function(vtf, ctx) {
//Store the important items in the tiles list
//We only want to store info for tiles for the current map zoom. If it is tile info for another zoom level, ignore it
//Also, if there are existing tiles in the list for other zoom levels, expunge them.
var zoom = this.map.getZoom();
if(ctx.zoom != zoom) return;
this.clearTileFeatures(zoom); //TODO: This iterates thru all tiles every time a new tile is added. Figure out a better way to do this.
this.tiles[ctx.id] = {
ctx: ctx,
vtf: vtf,
paths: []
};
};
/**
* Clear the inner list of tile features if they don't match the given zoom.
*
* @param zoom
*/
MVTFeature.prototype.clearTileFeatures = function(zoom) {
//If stored tiles exist for other zoom levels, expunge them from the list.
for (var key in this.tiles) {
if(key.split(":")[0] != zoom) delete this.tiles[key];
}
};
/**
* Redraws all of the tiles associated with a feature. Useful for
* style change and toggling.
*
* @param self
*/
function redrawTiles(self) {
//Redraw the whole tile, not just this vtf
var tiles = self.tiles;
var mvtLayer = self.mvtLayer;
for (var id in tiles) {
var tileZoom = parseInt(id.split(':')[0]);
var mapZoom = self.map.getZoom();
if (tileZoom === mapZoom) {
//Redraw the tile
mvtLayer.redrawTile(id);
}
}
}
MVTFeature.prototype.toggle = function() {
if (this.selected) {
this.deselect();
} else {
this.select();
}
};
MVTFeature.prototype.select = function() {
this.selected = true;
this.mvtSource.featureSelected(this);
redrawTiles(this);
var linkedFeature = this.linkedFeature();
if (linkedFeature && linkedFeature.staticLabel && !linkedFeature.staticLabel.selected) {
linkedFeature.staticLabel.select();
}
};
MVTFeature.prototype.deselect = function() {
this.selected = false;
this.mvtSource.featureDeselected(this);
redrawTiles(this);
var linkedFeature = this.linkedFeature();
if (linkedFeature && linkedFeature.staticLabel && linkedFeature.staticLabel.selected) {
linkedFeature.staticLabel.deselect();
}
};
MVTFeature.prototype.on = function(eventType, callback) {
this._eventHandlers[eventType] = callback;
};
MVTFeature.prototype._drawPoint = function(ctx, coordsArray, style) {
if (!style) return;
if (!ctx || !ctx.canvas) return;
var tile = this.tiles[ctx.id];
//Get radius
var radius = 1;
if (typeof style.radius === 'function') {
radius = style.radius(ctx.zoom); //Allows for scale dependent rednering
}
else{
radius = style.radius;
}
var p = this._tilePoint(coordsArray[0][0]);
var c = ctx.canvas;
var ctx2d;
try{
ctx2d = c.getContext('2d');
}
catch(e){
console.log("_drawPoint error: " + e);
return;
}
ctx2d.beginPath();
ctx2d.fillStyle = style.color;
ctx2d.arc(p.x, p.y, radius, 0, Math.PI * 2);
ctx2d.closePath();
ctx2d.fill();
if(style.lineWidth && style.strokeStyle){
ctx2d.lineWidth = style.lineWidth;
ctx2d.strokeStyle = style.strokeStyle;
ctx2d.stroke();
}
ctx2d.restore();
tile.paths.push([p]);
};
MVTFeature.prototype._drawLineString = function(ctx, coordsArray, style) {
if (!style) return;
if (!ctx || !ctx.canvas) return;
var ctx2d = ctx.canvas.getContext('2d');
ctx2d.strokeStyle = style.color;
ctx2d.lineWidth = style.size;
ctx2d.beginPath();
var projCoords = [];
var tile = this.tiles[ctx.id];
for (var gidx in coordsArray) {
var coords = coordsArray[gidx];
for (i = 0; i < coords.length; i++) {
var method = (i === 0 ? 'move' : 'line') + 'To';
var proj = this._tilePoint(coords[i]);
projCoords.push(proj);
ctx2d[method](proj.x, proj.y);
}
}
ctx2d.stroke();
ctx2d.restore();
tile.paths.push(projCoords);
};
MVTFeature.prototype._drawPolygon = function(ctx, coordsArray, style) {
if (!style) return;
if (!ctx || !ctx.canvas) return;
var ctx2d = ctx.canvas.getContext('2d');
var outline = style.outline;
// color may be defined via function to make choropleth work right
if (typeof style.color === 'function') {
ctx2d.fillStyle = style.color(ctx2d);
} else {
ctx2d.fillStyle = style.color;
}
if (outline) {
ctx2d.strokeStyle = outline.color;
ctx2d.lineWidth = outline.size;
}
ctx2d.beginPath();
var projCoords = [];
var tile = this.tiles[ctx.id];
var featureLabel = this.dynamicLabel;
if (featureLabel) {
featureLabel.addTilePolys(ctx, coordsArray);
}
for (var gidx = 0, len = coordsArray.length; gidx < len; gidx++) {
var coords = coordsArray[gidx];
for (var i = 0; i < coords.length; i++) {
var coord = coords[i];
var method = (i === 0 ? 'move' : 'line') + 'To';
var proj = this._tilePoint(coords[i]);
projCoords.push(proj);
ctx2d[method](proj.x, proj.y);
}
}
ctx2d.closePath();
ctx2d.fill();
if (outline) {
ctx2d.stroke();
}
tile.paths.push(projCoords);
};
MVTFeature.prototype._drawStaticLabel = function(ctx, coordsArray, style) {
if (!style) return;
if (!ctx) return;
// If the corresponding layer is not on the map,
// we dont want to put on a label.
if (!this.mvtLayer._map) return;
var vecPt = this._tilePoint(coordsArray[0][0]);
// We're making a standard Leaflet Marker for this label.
var p = this._project(vecPt, ctx.tile.x, ctx.tile.y, this.extent, this.tileSize); //vectile pt to merc pt
var mercPt = L.point(p.x, p.y); // make into leaflet obj
var latLng = this.map.unproject(mercPt); // merc pt to latlng
this.staticLabel = new StaticLabel(this, ctx, latLng, style);
this.mvtLayer.featureWithLabelAdded(this);
};
MVTFeature.prototype.removeLabel = function() {
if (!this.staticLabel) return;
this.staticLabel.remove();
this.staticLabel = null;
};
/**
* Projects a vector tile point to the Spherical Mercator pixel space for a given zoom level.
*
* @param vecPt
* @param tileX
* @param tileY
* @param extent
* @param tileSize
*/
MVTFeature.prototype._project = function(vecPt, tileX, tileY, extent, tileSize) {
var xOffset = tileX * tileSize;
var yOffset = tileY * tileSize;
return {
x: Math.floor(vecPt.x + xOffset),
y: Math.floor(vecPt.y + yOffset)
};
};
/**
* Takes a coordinate from a vector tile and turns it into a Leaflet Point.
*
* @param ctx
* @param coords
* @returns {eGeomType.Point}
* @private
*/
MVTFeature.prototype._tilePoint = function(coords) {
return new L.Point(coords.x / this.divisor, coords.y / this.divisor);
};
MVTFeature.prototype.linkedFeature = function() {
var linkedLayer = this.mvtLayer.linkedLayer();
if(linkedLayer){
var linkedFeature = linkedLayer.features[this.id];
return linkedFeature;
}else{
return null;
}
};