forked from glenrobertson/leaflet-tilelayer-geojson
-
Notifications
You must be signed in to change notification settings - Fork 5
/
TileLayer.GeoJSON.js
176 lines (146 loc) · 5.27 KB
/
TileLayer.GeoJSON.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
L.TileLayer.Vector = L.TileLayer.extend({
options: {
tileRequestFactory: L.tileRequest,
ajax: L.Request.get,
// use L.tileCacheNone to turn caching off
tileCacheFactory: L.tileCache,
// factory function to create the vector tile layers (defaults to L.GeoJSON)
layerFactory: L.geoJson,
// factory function to create a web worker for parsing/preparing tile data
//workerFactory: L.communistWorker
workerFactory: L.noWorker
},
initialize: function (url, options, vectorOptions) {
L.TileLayer.prototype.initialize.call(this, url, options);
this.vectorOptions = vectorOptions || {};
this._tileRequest = this.options.tileRequestFactory(this, this.options.ajax);
this._tileCache = this.options.tileCacheFactory();
// reference to a standalone function that can be stringified for a web worker
this._parseData = this.options.parseData || L.TileLayer.Vector.parseData;
this._worker = this.options.workerFactory(this._parseData);
this._addQueue = new L.TileQueue(L.bind(this._addTileDataInternal, this));
},
onAdd: function (map) {
L.TileLayer.prototype.onAdd.call(this, map);
this.on('tileunload', this._unloadTile);
// root vector layer, contains tile vector layers as children
this.vectorLayer = this._createVectorLayer();
map.addLayer(this.vectorLayer);
this._worker.onAdd(map);
this._tileCache.onAdd(map);
},
onRemove: function (map) {
// unload tiles (L.TileLayer only calls _reset in onAdd)
this._reset();
map.removeLayer(this.vectorLayer);
L.TileLayer.prototype.onRemove.call(this, map);
this.off('tileunload', this._unloadTile);
this._worker.onRemove(map);
this._tileCache.onRemove(map);
this.vectorLayer = null;
},
_addTile: function(coords, container) {
var cached = null;
this._wrapCoords(coords);
var key = this._tileCoordsToKey(coords);
var urlZoom = this._getZoomForUrl();
var tile = cached = this._tileCache.get(key, urlZoom);
if (!tile) {
tile = { key: key, urlZoom: urlZoom, datum: null, loading: true };
} else {
tile.loading = true;
}
this._tiles[key] = tile;
this.fire('tileloadstart', {tile: tile});
if (cached) {
this._addTileData(tile);
} else {
tile.url = this.getTileUrl(coords);
this._loadTile(tile);
}
},
_loadTile: function (tile) {
this._tileRequest.process(tile, L.bind(function(err, tile) {
if (!err) {
this._addTileData(tile);
} else {
this._tileLoaded();
}
},this));
},
// TODO _tileLoaded replaced by _tileReady + _visibleTilesReady,
// but cannot use because tile assumed to be component (L.DomUtil.addClass)?
_tileLoaded: function () {
this._tilesToLoad--;
if (this._tilesToLoad === 0) {
this.fire('load');
}
},
_createVectorLayer: function() {
return this.options.layerFactory(null, this.vectorOptions);
},
_createTileLayer: function() {
return this._createVectorLayer();
},
_addTileData: function(tile) {
if (!tile.parsed) {
this._worker.process(tile, L.bind(function(err, tile) {
if (!err) {
this._addQueue.add(tile);
} else {
// TODO refactor, copied from TileRequest, needed when plain request in worker
tile.loading = false;
this.fire('tileerror', {tile: tile});
this._tileLoaded();
}
},this));
} else {
// from cache
this._addQueue.add(tile);
}
},
_addTileDataInternal: function(tile) {
var tileLayer = this._createTileLayer();
if (!tile.parsed) {
// when no worker for parsing
tile.parsed = this._parseData(tile.datum);
tile.datum = null;
}
tileLayer.addData(tile.parsed);
tile.layer = tileLayer;
this.vectorLayer.addLayer(tileLayer);
tile.loading = false;
this.fire('tileload', {tile: tile});
this._tileLoaded();
},
_unloadTile: function(evt) {
var tile = evt.tile,
tileLayer = tile.layer;
this._tileRequest.abort(tile);
if (tile.loading) {
this._addQueue.remove(tile);
// not from cache or not loaded and parsed yet
if (!tile.parsed) {
this._worker.abort(tile);
}
this.fire('tileabort', {tile: tile});
this._tileLoaded();
}
if (tileLayer && this.vectorLayer.hasLayer(tileLayer)) {
this.vectorLayer.removeLayer(tileLayer);
}
if (tile.parsed) {
this._tileCache.put(tile);
}
},
_reset: function() {
L.TileLayer.prototype._reset.apply(this, arguments);
this._addQueue.clear();
this._worker.clear();
}
});
L.extend(L.TileLayer.Vector, {
parseData: function(data) {
return JSON.parse(data);
}
});