This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tile.js
224 lines (201 loc) · 4.67 KB
/
tile.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
import rbush from 'rbush';
import bbox from '@turf/bbox';
/**
* A tile object
*
* @class Tile
* @private
*/
export default class Tile {
/**
* Constructor
*
* param {number} x - x coordinate of tile
* param {number} y - y coordinate of tile
* param {number} z - z coordinate of tile
*/
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.coords = { x, y, z };
this._features = {};
this.loaded = false;
this._index = rbush();
this.featureGroup = L.featureGroup();
}
/**
* We call this method when all features have been added to the tile
*
* @returns {Tile} this
*/
init() {
this.indexFeatures();
return this;
}
/**
* This method adds all features in this tile to an rbush index
*/
indexFeatures() {
const bboxes = [];
for (const id in this._features) {
if (!this._features.hasOwnProperty(id)) {
continue;
}
const feature = this._features[id];
const geom = feature.geojson.geometry;
const c = geom.coordinates;
let minX;
let minY;
let maxX;
let maxY;
if (geom.type === 'Point') {
minX = c[0];
maxX = c[0];
minY = c[1];
maxY = c[1];
} else {
[minX, minY, maxX, maxY] = bbox(geom);
}
const item = {
minX,
minY,
maxX,
maxY,
id: feature.id
};
feature.indexEntry = item;
bboxes.push(item);
}
// bulk insert into spatial index
this._index.load(bboxes);
}
/**
* @param {string} id
* @returns {boolean} true if this tile contains a feature with the given id
*/
contains(id) {
return id in this._features;
}
/**
* @param {Feature} feature
* @returns {Tile} this
*/
addFeature(feature) {
this._features[feature.id] = feature;
this.featureGroup.addLayer(feature.layer);
return this;
}
/**
* @param {string} id
* @returns {Tile} this
*/
removeFeature(id) {
if (!this.contains(id)) {
return this;
}
const feature = this.getFeature(id);
this.featureGroup.removeLayer(feature.layer);
this._index.remove(feature.indexEntry);
delete this._features[id];
return this;
}
/**
* @param {string} id
* @returns {Feature}
*/
getFeature(id) {
return this._features[id];
}
/**
* This method adds this tile to the given layer by calling `addLayer` with the
* tile's underlying FeatureGroup
*
* @param {L.Map|L.FeatureGroup|L.LayerGroup}
* @returns {Tile} this
*/
addTo(layer) {
layer.addLayer(this.featureGroup);
return this;
}
/**
* This method removes this tile from the given layer by calling `removeLayer` with
* the tile's underlying FeatureGroup
*
* @param {L.Map|L.FeatureGroup|L.LayerGroup}
* @returns {Tile} this
*/
removeFrom(layer) {
layer.removeLayer(this.featureGroup);
}
/**
* @param {number} minX
* @param {number} minY
* @param {number} maxX
* @param {number} maxY
* @returns {Array<String>} an array of feature ids of features that intersect
* the bounding box
*/
search(minX, minY, maxX, maxY) {
return this._index.search({ minX, minY, maxX, maxY }).map(r => r.id);
}
/**
* Marks the tile as loaded
*
* @returns {Tile} this
*/
markAsLoaded() {
this.loaded = true;
return this;
}
/**
* @param {string} property
* @param {string} value
* @param {boolean} on
* @param {boolead} toggled
* @returns {Tile} this
*/
toggleByProperty(property, value, on, toggled) {
let feature;
let geoj;
for (const id in this._features) {
if (!this._features.hasOwnProperty(id)) {
continue;
}
feature = this.getFeature(id);
geoj = feature.geojson;
if (property in geoj.properties && geoj.properties[property] === value) {
if (toggled) {
if (on) {
this._index.insert(feature.indexEntry);
this.featureGroup.addLayer(feature.layer);
} else {
this._index.remove(feature.indexEntry);
this.featureGroup.removeLayer(feature.layer);
}
}
}
}
return this;
}
/**
* @param {string} property
* @param {string} value
* @param {Object} style
* @returns {Tile} this
*/
restyleByProperty(property, value, style) {
let feature;
for (const id in this._features) {
if (!this._features.hasOwnProperty(id)) {
continue;
}
feature = this.getFeature(id);
if (property in feature.geojson.properties
&& feature.geojson.properties[property] === value) {
feature.layer.setStyle(style);
}
}
return this;
}
}