@maplibre/geojson-vt 6.2.0 → 6.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/dist/clip.d.ts.map +1 -1
- package/dist/cluster-tile-index.d.ts +12 -9
- package/dist/cluster-tile-index.d.ts.map +1 -1
- package/dist/convert.d.ts +2 -9
- package/dist/convert.d.ts.map +1 -1
- package/dist/deconvert.d.ts +4 -11
- package/dist/deconvert.d.ts.map +1 -1
- package/dist/definitions.d.ts +38 -10
- package/dist/definitions.d.ts.map +1 -1
- package/dist/difference.d.ts +2 -1
- package/dist/difference.d.ts.map +1 -1
- package/dist/geojson-vt-dev.js +287 -162
- package/dist/geojson-vt.js +1 -1
- package/dist/geojson-vt.mjs +285 -161
- package/dist/geojson-vt.mjs.map +1 -1
- package/dist/geojsonvt.d.ts +9 -4
- package/dist/geojsonvt.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/tile-index.d.ts +4 -3
- package/dist/tile-index.d.ts.map +1 -1
- package/dist/world-coordinate-helper.d.ts +13 -0
- package/dist/world-coordinate-helper.d.ts.map +1 -0
- package/package.json +6 -6
- package/src/clip.ts +0 -1
- package/src/cluster-tile-index.test.ts +28 -8
- package/src/cluster-tile-index.ts +53 -27
- package/src/convert.ts +45 -47
- package/src/deconvert.test.ts +4 -4
- package/src/deconvert.ts +15 -31
- package/src/definitions.ts +39 -10
- package/src/difference.ts +49 -19
- package/src/geojson-to-tile.ts +2 -2
- package/src/geojsonvt.test.ts +183 -20
- package/src/geojsonvt.ts +31 -10
- package/src/index.ts +2 -1
- package/src/tile-index.ts +8 -6
- package/src/world-coordinate-helper.ts +63 -0
- package/src/wrap.ts +1 -1
package/src/geojsonvt.test.ts
CHANGED
|
@@ -38,25 +38,25 @@ test('publicly exposed cluster methods: return undefined when clustering is disa
|
|
|
38
38
|
expect(index.getClusterLeaves(123, 10, 0)).toBeNull();
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
-
test('
|
|
41
|
+
test('a unit square without wrap cuts a line at the buffer around the world instead of wrapping or clamping it', () => {
|
|
42
42
|
const fromThreeWorldsNorthToEast = {type: 'LineString' as const, coordinates: [[0.25, -3], [0.75, 0.5], [1.25, 0.5]]};
|
|
43
43
|
|
|
44
|
-
const index = new GeoJSONVT(fromThreeWorldsNorthToEast, {
|
|
44
|
+
const index = new GeoJSONVT(fromThreeWorldsNorthToEast, {coordinates: 'unitSquare', wrap: false});
|
|
45
45
|
|
|
46
46
|
expect(index.getTile(0, 0, 0).features.map(feature => feature.geometry)).toEqual([[[[2770, -64], [3072, 2048], [4160, 2048]]]]);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
test('
|
|
49
|
+
test('a unit square without wrap cuts the top tile at the world again on update, so a feature can move in from outside', () => {
|
|
50
50
|
const movingIn = {type: 'Feature' as const, id: 'moving in', geometry: {type: 'Point' as const, coordinates: [1.25, 0.5]}, properties: {}};
|
|
51
51
|
const stayingOut = {type: 'Feature' as const, id: 'staying out', geometry: {type: 'Point' as const, coordinates: [1.5, 0.5]}, properties: {}};
|
|
52
|
-
const index = new GeoJSONVT({type: 'FeatureCollection', features: [movingIn, stayingOut]}, {
|
|
52
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [movingIn, stayingOut]}, {coordinates: 'unitSquare', wrap: false, updateable: true});
|
|
53
53
|
|
|
54
54
|
index.updateData({update: [{id: 'moving in', newGeometry: {type: 'Point', coordinates: [0.5, 0.5]}}]});
|
|
55
55
|
|
|
56
56
|
expect(index.getTile(0, 0, 0).features.map(feature => feature.id)).toEqual(['moving in']);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
test('
|
|
59
|
+
test('a unit square without wrap returns the data in the unit square', () => {
|
|
60
60
|
const features: GeoJSON.Feature[] = [
|
|
61
61
|
{type: 'Feature', id: 'point', geometry: {type: 'Point', coordinates: [0.25, 0.75]}, properties: {}},
|
|
62
62
|
{type: 'Feature', id: 'multi point', geometry: {type: 'MultiPoint', coordinates: [[0.25, 0.75], [0.5, 0.5]]}, properties: {}},
|
|
@@ -65,14 +65,14 @@ test('worldCoordinates returns the data in world coordinates', () => {
|
|
|
65
65
|
{type: 'Feature', id: 'polygon', geometry: {type: 'Polygon', coordinates: [[[0.25, 0.25], [0.75, 0.25], [0.75, 0.75], [0.25, 0.25]]]}, properties: {}},
|
|
66
66
|
{type: 'Feature', id: 'multi polygon', geometry: {type: 'MultiPolygon', coordinates: [[[[0.25, 0.25], [0.75, 0.25], [0.75, 0.75], [0.25, 0.25]]]]}, properties: {}}
|
|
67
67
|
];
|
|
68
|
-
const index = new GeoJSONVT({type: 'FeatureCollection', features}, {
|
|
68
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features}, {coordinates: 'unitSquare', wrap: false, updateable: true});
|
|
69
69
|
|
|
70
70
|
expect(index.getData()).toEqual({type: 'FeatureCollection', features});
|
|
71
71
|
});
|
|
72
72
|
|
|
73
|
-
test('
|
|
73
|
+
test('a unit square without wrap keeps a MultiLineString it splits for lineMetrics in the unit square', () => {
|
|
74
74
|
const multiLine = {type: 'MultiLineString' as const, coordinates: [[[0.25, 0.75], [0.5, 0.5]], [[0.5, 0.25], [0.75, 0.25]]]};
|
|
75
|
-
const index = new GeoJSONVT(multiLine, {
|
|
75
|
+
const index = new GeoJSONVT(multiLine, {coordinates: 'unitSquare', wrap: false, lineMetrics: true, updateable: true});
|
|
76
76
|
|
|
77
77
|
expect(index.getData()).toEqual({type: 'FeatureCollection', features: [
|
|
78
78
|
{type: 'Feature', geometry: {type: 'LineString', coordinates: [[0.25, 0.75], [0.5, 0.5]]}},
|
|
@@ -80,9 +80,9 @@ test('worldCoordinates keeps a MultiLineString it splits for lineMetrics in worl
|
|
|
80
80
|
]});
|
|
81
81
|
});
|
|
82
82
|
|
|
83
|
-
test('
|
|
83
|
+
test('a unit square without wrap hands the update filter features in the unit square', () => {
|
|
84
84
|
const point = {type: 'Feature' as const, id: 'p', geometry: {type: 'Point' as const, coordinates: [0.25, 0.75]}, properties: {}};
|
|
85
|
-
const index = new GeoJSONVT({type: 'FeatureCollection', features: [point]}, {
|
|
85
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [point]}, {coordinates: 'unitSquare', wrap: false, updateable: true});
|
|
86
86
|
const filtered: GeoJSON.Geometry[] = [];
|
|
87
87
|
|
|
88
88
|
index.updateData({add: [{...point, id: 'q'}]}, feature => {
|
|
@@ -93,7 +93,7 @@ test('worldCoordinates hands the update filter features in world coordinates', (
|
|
|
93
93
|
expect(filtered).toContainEqual({type: 'Point', coordinates: [0.25, 0.75]});
|
|
94
94
|
});
|
|
95
95
|
|
|
96
|
-
test('
|
|
96
|
+
test('a unit square without wrap leaves points outside the world out of the clusters', () => {
|
|
97
97
|
const data = {
|
|
98
98
|
type: 'FeatureCollection' as const,
|
|
99
99
|
features: [
|
|
@@ -103,12 +103,12 @@ test('worldCoordinates leaves points outside the world out of the clusters', ()
|
|
|
103
103
|
]
|
|
104
104
|
};
|
|
105
105
|
|
|
106
|
-
const index = new GeoJSONVT(data, {
|
|
106
|
+
const index = new GeoJSONVT(data, {coordinates: 'unitSquare', wrap: false, cluster: true});
|
|
107
107
|
|
|
108
108
|
expect(index.getTile(0, 0, 0).features.map(feature => feature.geometry)).toEqual([[[507, 256]]]);
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
-
test('
|
|
111
|
+
test('a unit square without wrap keeps points at either edge out of the clustered tile at the other edge', () => {
|
|
112
112
|
const data = {
|
|
113
113
|
type: 'FeatureCollection' as const,
|
|
114
114
|
features: [
|
|
@@ -117,12 +117,12 @@ test('worldCoordinates keeps points at either edge out of the clustered tile at
|
|
|
117
117
|
]
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
-
const index = new GeoJSONVT(data, {
|
|
120
|
+
const index = new GeoJSONVT(data, {coordinates: 'unitSquare', wrap: false, cluster: true});
|
|
121
121
|
|
|
122
122
|
expect(index.getTile(0, 0, 0).features).toHaveLength(2);
|
|
123
123
|
});
|
|
124
124
|
|
|
125
|
-
test('
|
|
125
|
+
test('a unit square without wrap returns cluster children and leaves in the unit square', () => {
|
|
126
126
|
const points = {
|
|
127
127
|
type: 'FeatureCollection' as const,
|
|
128
128
|
features: Array.from({length: 20}, (_, i) => ({
|
|
@@ -131,7 +131,7 @@ test('worldCoordinates returns cluster children and leaves in world coordinates'
|
|
|
131
131
|
properties: {name: `Point ${i}`}
|
|
132
132
|
}))
|
|
133
133
|
};
|
|
134
|
-
const index = new GeoJSONVT(points, {
|
|
134
|
+
const index = new GeoJSONVT(points, {coordinates: 'unitSquare', wrap: false, cluster: true, clusterOptions: {radius: 100}});
|
|
135
135
|
const cluster = index.getTile(0, 0, 0).features.find(f => (f.tags as {cluster?: boolean})?.cluster);
|
|
136
136
|
const clusterId = (cluster.tags as {cluster_id: number}).cluster_id;
|
|
137
137
|
|
|
@@ -142,10 +142,41 @@ test('worldCoordinates returns cluster children and leaves in world coordinates'
|
|
|
142
142
|
expect(leafPositions).toEqual(expect.arrayContaining(points.features.map(point => point.geometry.coordinates)));
|
|
143
143
|
});
|
|
144
144
|
|
|
145
|
-
test('
|
|
145
|
+
test('a unit square without wrap stays without wrap for clusters after updateData', () => {
|
|
146
|
+
const point = (id: string, x: number) => ({type: 'Feature' as const, id, properties: {}, geometry: {type: 'Point' as const, coordinates: [x, 0.5]}});
|
|
147
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [point('a', 0.01)]}, {coordinates: 'unitSquare', wrap: false, cluster: true, updateable: true});
|
|
148
|
+
|
|
149
|
+
index.updateData({add: [point('b', 0.99)]});
|
|
150
|
+
|
|
151
|
+
expect(index.getTile(1, -1, 0)).toBeNull();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('wraps positions in the unit square around the antimeridian by default', () => {
|
|
155
|
+
const nearTheEastEdge = {type: 'Feature' as const, properties: {}, geometry: {type: 'Point' as const, coordinates: [0.999, 0.25]}};
|
|
156
|
+
const index = new GeoJSONVT(nearTheEastEdge, {coordinates: 'unitSquare'});
|
|
157
|
+
|
|
158
|
+
expect(index.getTile(1, 0, 0).features.map(feature => feature.geometry)).toEqual([[[-8, 2048]]]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('cuts the top tile above and below for positions in a unit square that wraps', () => {
|
|
162
|
+
const fromThreeBelowToThreeAbove = {type: 'Feature' as const, properties: {}, geometry: {type: 'LineString' as const, coordinates: [[0.25, -3], [0.75, 3]]}};
|
|
163
|
+
const index = new GeoJSONVT(fromThreeBelowToThreeAbove, {coordinates: 'unitSquare'});
|
|
164
|
+
|
|
165
|
+
expect(index.getTile(0, 0, 0).features[0].geometry).toEqual([[[2043, -64], [2395, 4160]]]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('copies no lng/lat features across the antimeridian without wrap', () => {
|
|
169
|
+
const nearTheAntimeridian = {type: 'Feature' as const, properties: {}, geometry: {type: 'Point' as const, coordinates: [179.9, -10]}};
|
|
170
|
+
const index = new GeoJSONVT(nearTheAntimeridian, {wrap: false});
|
|
171
|
+
|
|
172
|
+
expect(index.getTile(1, 0, 1)?.features ?? []).toEqual([]);
|
|
173
|
+
expect(index.getTile(1, 1, 1).features).toHaveLength(1);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('a unit square without wrap returns no tile for a column on either side of the world', () => {
|
|
146
177
|
const inTheTwoTopTiles = {type: 'MultiPoint' as const, coordinates: [[0.25, 0.25], [0.75, 0.25]]};
|
|
147
178
|
|
|
148
|
-
const index = new GeoJSONVT(inTheTwoTopTiles, {
|
|
179
|
+
const index = new GeoJSONVT(inTheTwoTopTiles, {coordinates: 'unitSquare', wrap: false});
|
|
149
180
|
|
|
150
181
|
expect([index.getTile(1, -1, 0), index.getTile(1, 2, 0)]).toEqual([null, null]);
|
|
151
182
|
});
|
|
@@ -210,6 +241,138 @@ test('leaves out a MultiPolygon that has only empty polygons', () => {
|
|
|
210
241
|
expect(index.getData()).toEqual({type: 'FeatureCollection', features: []});
|
|
211
242
|
});
|
|
212
243
|
|
|
244
|
+
test('updateData draws a feature again when an update gives back the coordinates an earlier one took away', () => {
|
|
245
|
+
const line = {type: 'Feature' as const, id: 'line', properties: {name: 'line'}, geometry: {type: 'LineString' as const, coordinates: [[0, 0], [10, 10]]}};
|
|
246
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [line]}, {updateable: true});
|
|
247
|
+
|
|
248
|
+
index.updateData({update: [{id: 'line', newGeometry: {type: 'LineString', coordinates: []}}]});
|
|
249
|
+
index.updateData({update: [{id: 'line', newGeometry: line.geometry}]});
|
|
250
|
+
|
|
251
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.tags)).toEqual([{name: 'line'}]);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('updateData draws a feature loaded without a geometry once an update gives it one', () => {
|
|
255
|
+
const later: GeoJSON.Feature = {type: 'Feature', id: 'later', properties: {name: 'later'}, geometry: null};
|
|
256
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [later]}, {updateable: true});
|
|
257
|
+
|
|
258
|
+
index.updateData({update: [{id: 'later', newGeometry: {type: 'Point', coordinates: [10, 10]}}]});
|
|
259
|
+
|
|
260
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.tags)).toEqual([{name: 'later'}]);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('updateData draws a feature added without coordinates once an update gives it some', () => {
|
|
264
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: []}, {updateable: true});
|
|
265
|
+
|
|
266
|
+
index.updateData({add: [{type: 'Feature', id: 'added', properties: {name: 'added'}, geometry: {type: 'Polygon', coordinates: []}}]});
|
|
267
|
+
index.updateData({update: [{id: 'added', newGeometry: {type: 'Polygon', coordinates: [[[0, 0], [10, 0], [10, 10], [0, 0]]]}}]});
|
|
268
|
+
|
|
269
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.tags)).toEqual([{name: 'added'}]);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('updateData leaves a feature without a geometry removed when an update gives it one', () => {
|
|
273
|
+
const kept = {type: 'Feature' as const, id: 'kept', properties: {}, geometry: {type: 'Point' as const, coordinates: [0, 0]}};
|
|
274
|
+
const removed: GeoJSON.Feature = {type: 'Feature', id: 'removed', properties: {}, geometry: null};
|
|
275
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [kept, removed]}, {updateable: true});
|
|
276
|
+
|
|
277
|
+
index.updateData({remove: ['removed']});
|
|
278
|
+
index.updateData({update: [{id: 'removed', newGeometry: {type: 'Point', coordinates: [10, 10]}}]});
|
|
279
|
+
|
|
280
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.id)).toEqual(['kept']);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test('updateData leaves a feature without a geometry removed by removeAll when an update gives it one', () => {
|
|
284
|
+
const kept = {type: 'Feature' as const, id: 'kept', properties: {}, geometry: {type: 'Point' as const, coordinates: [0, 0]}};
|
|
285
|
+
const removed: GeoJSON.Feature = {type: 'Feature', id: 'removed', properties: {}, geometry: null};
|
|
286
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [removed]}, {updateable: true});
|
|
287
|
+
|
|
288
|
+
index.updateData({removeAll: true, add: [kept]});
|
|
289
|
+
index.updateData({update: [{id: 'removed', newGeometry: {type: 'Point', coordinates: [10, 10]}}]});
|
|
290
|
+
|
|
291
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.id)).toEqual(['kept']);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('updateData gives each feature added with generateId a new id that is not reused and can be removed', () => {
|
|
295
|
+
const point = (x: number): GeoJSON.Feature => ({type: 'Feature', properties: {x}, geometry: {type: 'Point', coordinates: [x, 0]}});
|
|
296
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [point(0), point(1)]}, {updateable: true, generateId: true});
|
|
297
|
+
|
|
298
|
+
index.updateData({add: [point(2), point(3)]});
|
|
299
|
+
index.updateData({remove: [0]});
|
|
300
|
+
index.updateData({add: [point(4)]});
|
|
301
|
+
index.updateData({remove: [2]});
|
|
302
|
+
|
|
303
|
+
expect((index.getData() as GeoJSON.FeatureCollection).features.map(feature => [feature.id, feature.properties.x])).toEqual([[1, 1], [3, 3], [4, 4]]);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test('updateData keeps the property changes made to a feature while it has no geometry', () => {
|
|
307
|
+
const renamed: GeoJSON.Feature = {type: 'Feature', id: 'renamed', properties: {name: 'before'}, geometry: null};
|
|
308
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [renamed]}, {updateable: true});
|
|
309
|
+
|
|
310
|
+
index.updateData({update: [{id: 'renamed', addOrUpdateProperties: [{key: 'name', value: 'after'}]}]});
|
|
311
|
+
index.updateData({update: [{id: 'renamed', newGeometry: {type: 'Point', coordinates: [10, 10]}}]});
|
|
312
|
+
|
|
313
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.tags)).toEqual([{name: 'after'}]);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('updateData does not bring back a feature the update filter took out after an update gave it a geometry', () => {
|
|
317
|
+
const kept = {type: 'Feature' as const, id: 'kept', properties: {}, geometry: {type: 'Point' as const, coordinates: [0, 0]}};
|
|
318
|
+
const hidden: GeoJSON.Feature = {type: 'Feature', id: 'hidden', properties: {hidden: true}, geometry: null};
|
|
319
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [kept, hidden]}, {updateable: true});
|
|
320
|
+
|
|
321
|
+
index.updateData({update: [{id: 'hidden', newGeometry: {type: 'Point', coordinates: [10, 10]}}]}, feature => !feature.properties.hidden);
|
|
322
|
+
index.updateData({update: [{id: 'hidden', newGeometry: {type: 'Point', coordinates: [20, 20]}}]});
|
|
323
|
+
|
|
324
|
+
expect(index.getTile(0, 0, 0).features.map(feature => feature.id)).toEqual(['kept']);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test('updateData does not bring back a feature the update filter took out while it had no coordinates', () => {
|
|
328
|
+
const point = {type: 'Feature' as const, id: 'point', properties: {keep: true}, geometry: {type: 'Point' as const, coordinates: [0, 0]}};
|
|
329
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [point]}, {updateable: true});
|
|
330
|
+
const keepIfMarked = (feature: GeoJSON.Feature) => feature.properties?.keep === true;
|
|
331
|
+
|
|
332
|
+
index.updateData({update: [{id: 'point', newGeometry: {type: 'Point', coordinates: []}, addOrUpdateProperties: [{key: 'keep', value: false}]}]}, keepIfMarked);
|
|
333
|
+
index.updateData({update: [{id: 'point', newGeometry: {type: 'Point', coordinates: [10, 10]}, addOrUpdateProperties: [{key: 'keep', value: true}]}]}, keepIfMarked);
|
|
334
|
+
|
|
335
|
+
expect(index.getData()).toEqual({type: 'FeatureCollection', features: []});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test('updateData draws a bare geometry loaded without coordinates once an update gives it some', () => {
|
|
339
|
+
const index = new GeoJSONVT({type: 'LineString', coordinates: []}, {updateable: true, generateId: true});
|
|
340
|
+
|
|
341
|
+
index.updateData({update: [{id: 0, newGeometry: {type: 'LineString', coordinates: [[0, 0], [10, 10]]}}]});
|
|
342
|
+
|
|
343
|
+
expect(index.getTile(0, 0, 0)?.features.map(feature => feature.id)).toEqual([0]);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('updateData keeps a feature loaded without a geometry away from the update filter', () => {
|
|
347
|
+
const point = {type: 'Feature' as const, id: 'point', properties: {}, geometry: {type: 'Point' as const, coordinates: [0, 0]}};
|
|
348
|
+
const later: GeoJSON.Feature = {type: 'Feature', id: 'later', properties: {}, geometry: null};
|
|
349
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [point, later]}, {updateable: true});
|
|
350
|
+
const pointsOnly = (feature: GeoJSON.Feature) => feature.geometry.type === 'Point';
|
|
351
|
+
|
|
352
|
+
index.updateData({update: [{id: 'point', addOrUpdateProperties: [{key: 'name', value: 'p'}]}]}, pointsOnly);
|
|
353
|
+
index.updateData({update: [{id: 'later', newGeometry: {type: 'Point', coordinates: [10, 10]}}]}, pointsOnly);
|
|
354
|
+
|
|
355
|
+
expect(index.getTile(0, 0, 0)?.features.map(feature => feature.id).sort()).toEqual(['later', 'point']);
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test('updateData hands the filter a feature without coordinates with its promoted id', () => {
|
|
359
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [{type: 'Feature', id: 'raw', properties: {key: 'promoted'}, geometry: {type: 'Point', coordinates: []}}]}, {updateable: true, promoteId: 'key'});
|
|
360
|
+
|
|
361
|
+
index.updateData({}, feature => feature.id === 'promoted');
|
|
362
|
+
index.updateData({update: [{id: 'promoted', newGeometry: {type: 'Point', coordinates: [10, 10]}}]});
|
|
363
|
+
|
|
364
|
+
expect(index.getTile(0, 0, 0)?.features.map(feature => feature.id)).toEqual(['promoted']);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
test('updateData hands the filter a feature without coordinates with its generated id', () => {
|
|
368
|
+
const index = new GeoJSONVT({type: 'FeatureCollection', features: [{type: 'Feature', properties: {}, geometry: {type: 'Point', coordinates: []}}]}, {updateable: true, generateId: true});
|
|
369
|
+
|
|
370
|
+
index.updateData({}, feature => feature.id === 0);
|
|
371
|
+
index.updateData({update: [{id: 0, newGeometry: {type: 'Point', coordinates: [10, 10]}}]});
|
|
372
|
+
|
|
373
|
+
expect(index.getTile(0, 0, 0)?.features.map(feature => feature.id)).toEqual([0]);
|
|
374
|
+
});
|
|
375
|
+
|
|
213
376
|
test('keeps the corner of a polygon whose vertex lies on the left edge of a tile', () => {
|
|
214
377
|
const polygon = {
|
|
215
378
|
type: 'Polygon' as const,
|
|
@@ -261,14 +424,14 @@ test('clusters each position of a MultiPoint feature when clustering is on', ()
|
|
|
261
424
|
]);
|
|
262
425
|
});
|
|
263
426
|
|
|
264
|
-
test('
|
|
427
|
+
test('a unit square without wrap leaves the positions of a MultiPoint outside the world out of the clusters', () => {
|
|
265
428
|
const multiPoint = {
|
|
266
429
|
type: 'Feature' as const,
|
|
267
430
|
properties: {},
|
|
268
431
|
geometry: {type: 'MultiPoint' as const, coordinates: [[0.99, 0.5], [1.03, 0.5], [0.5, -0.03]]}
|
|
269
432
|
};
|
|
270
433
|
|
|
271
|
-
const index = new GeoJSONVT(multiPoint, {
|
|
434
|
+
const index = new GeoJSONVT(multiPoint, {coordinates: 'unitSquare', wrap: false, cluster: true});
|
|
272
435
|
|
|
273
436
|
expect(index.getTile(0, 0, 0).features.map(feature => feature.geometry)).toEqual([[[507, 256]]]);
|
|
274
437
|
});
|
package/src/geojsonvt.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {convertToInternal} from './convert';
|
|
2
2
|
import {convertToGeoJSON, featureToGeoJSON} from './deconvert';
|
|
3
|
+
import {getWorldCoordinateHelper, type WorldCoordinateHelper} from './world-coordinate-helper';
|
|
3
4
|
import {wrap} from './wrap';
|
|
4
5
|
import {applySourceDiff, type GeoJSONVTSourceDiff} from './difference';
|
|
5
6
|
import {ClusterTileIndex, defaultClusterOptions} from './cluster-tile-index';
|
|
6
7
|
import {TileIndex} from './tile-index';
|
|
7
|
-
import type {ClusterOrPointFeature, GeoJSONVTTileIndex, GeoJSONVTInternalFeature, GeoJSONVTOptions, GeoJSONVTTile
|
|
8
|
+
import type {ClusterOrPointFeature, GeoJSONVTTileIndex, GeoJSONVTInternalFeature, GeoJSONVTOptions, GeoJSONVTTile} from './definitions';
|
|
8
9
|
|
|
9
10
|
export const defaultOptions: GeoJSONVTOptions = {
|
|
10
11
|
maxZoom: 14,
|
|
@@ -20,7 +21,8 @@ export const defaultOptions: GeoJSONVTOptions = {
|
|
|
20
21
|
cluster: false,
|
|
21
22
|
clusterOptions: defaultClusterOptions,
|
|
22
23
|
debug: 0,
|
|
23
|
-
|
|
24
|
+
coordinates: 'lngLat',
|
|
25
|
+
wrap: true
|
|
24
26
|
};
|
|
25
27
|
|
|
26
28
|
/**
|
|
@@ -28,12 +30,16 @@ export const defaultOptions: GeoJSONVTOptions = {
|
|
|
28
30
|
*/
|
|
29
31
|
export class GeoJSONVT {
|
|
30
32
|
private options: GeoJSONVTOptions;
|
|
33
|
+
private worldCoordinateHelper: WorldCoordinateHelper;
|
|
31
34
|
|
|
32
35
|
private source?: GeoJSONVTInternalFeature[];
|
|
36
|
+
private unlocated?: Map<string | number, GeoJSON.Feature>;
|
|
33
37
|
private tileIndex: GeoJSONVTTileIndex;
|
|
38
|
+
private nextId: number;
|
|
34
39
|
|
|
35
40
|
constructor(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions) {
|
|
36
41
|
options = this.options = Object.assign({}, defaultOptions, options);
|
|
42
|
+
this.worldCoordinateHelper = getWorldCoordinateHelper(options.coordinates);
|
|
37
43
|
|
|
38
44
|
const debug = options.debug;
|
|
39
45
|
|
|
@@ -42,8 +48,14 @@ export class GeoJSONVT {
|
|
|
42
48
|
if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error('maxZoom should be in the 0-24 range');
|
|
43
49
|
if (options.promoteId && options.generateId) throw new Error('promoteId and generateId cannot be used together.');
|
|
44
50
|
|
|
51
|
+
// for updateable indexes, keep the properties of the features without coordinates, so that an update can give them some
|
|
52
|
+
if (options.updateable) this.unlocated = new Map();
|
|
53
|
+
|
|
54
|
+
// with generateId, features added by an update get ids after the ones generated here
|
|
55
|
+
this.nextId = data.type === 'FeatureCollection' ? data.features.length : 1;
|
|
56
|
+
|
|
45
57
|
// projects and adds simplification info
|
|
46
|
-
let features = convertToInternal(data, options);
|
|
58
|
+
let features = convertToInternal(data, options, this.unlocated);
|
|
47
59
|
|
|
48
60
|
if (debug) {
|
|
49
61
|
console.timeEnd('preprocess data');
|
|
@@ -63,7 +75,7 @@ export class GeoJSONVT {
|
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
private initializeIndex(features: GeoJSONVTInternalFeature[], options: GeoJSONVTOptions) {
|
|
66
|
-
this.tileIndex = options.cluster ? new ClusterTileIndex(options.clusterOptions, options.
|
|
78
|
+
this.tileIndex = options.cluster ? new ClusterTileIndex({...options.clusterOptions, coordinates: options.coordinates, wrap: options.wrap}) : new TileIndex(options);
|
|
67
79
|
if (!features.length) return;
|
|
68
80
|
this.tileIndex.initialize(features);
|
|
69
81
|
}
|
|
@@ -94,8 +106,11 @@ export class GeoJSONVT {
|
|
|
94
106
|
|
|
95
107
|
if (!options.updateable) throw new Error('to update tile geojson `updateable` option must be set to true');
|
|
96
108
|
|
|
109
|
+
// give added features new generated ids, so none of them share or reuse an id
|
|
110
|
+
if (options.generateId && diff.add) diff = {...diff, add: diff.add.map(feature => ({...feature, id: this.nextId++}))};
|
|
111
|
+
|
|
97
112
|
// apply diff and collect affected features and updated source that will be used to invalidate tiles
|
|
98
|
-
let {affected, source} = applySourceDiff(this.source, diff, options);
|
|
113
|
+
let {affected, source} = applySourceDiff(this.source, diff, options, this.unlocated);
|
|
99
114
|
|
|
100
115
|
if (filter) {
|
|
101
116
|
({affected, source} = this.filterUpdate(source, affected, filter));
|
|
@@ -111,19 +126,25 @@ export class GeoJSONVT {
|
|
|
111
126
|
}
|
|
112
127
|
|
|
113
128
|
/**
|
|
114
|
-
* Filter an update using a predicate function
|
|
129
|
+
* Filter an update using a predicate function, the features an update emptied included, so that one the filter
|
|
130
|
+
* rejects can't come back with a later update. A feature without a geometry at all isn't passed to the predicate.
|
|
131
|
+
* Returns the affected and updated source features.
|
|
115
132
|
*/
|
|
116
133
|
private filterUpdate(source: GeoJSONVTInternalFeature[], affected: GeoJSONVTInternalFeature[], predicate: (feature: GeoJSON.Feature) => boolean) {
|
|
117
134
|
const removeIds = new Set();
|
|
118
135
|
|
|
119
136
|
for (const feature of source) {
|
|
120
137
|
if (feature.id == undefined) continue;
|
|
121
|
-
if (predicate(featureToGeoJSON(feature, this.
|
|
138
|
+
if (predicate(featureToGeoJSON(feature, this.worldCoordinateHelper))) continue;
|
|
122
139
|
affected.push(feature);
|
|
123
140
|
removeIds.add(feature.id);
|
|
124
141
|
}
|
|
125
142
|
source = source.filter(feature => !removeIds.has(feature.id));
|
|
126
143
|
|
|
144
|
+
for (const [id, feature] of this.unlocated ?? []) {
|
|
145
|
+
if (feature.geometry && !predicate(feature)) this.unlocated.delete(id);
|
|
146
|
+
}
|
|
147
|
+
|
|
127
148
|
return {affected, source};
|
|
128
149
|
}
|
|
129
150
|
|
|
@@ -132,15 +153,15 @@ export class GeoJSONVT {
|
|
|
132
153
|
*/
|
|
133
154
|
getData(): GeoJSON.GeoJSON {
|
|
134
155
|
if (!this.options.updateable) throw new Error('to retrieve data the `updateable` option must be set to true');
|
|
135
|
-
return convertToGeoJSON(this.source, this.
|
|
156
|
+
return convertToGeoJSON(this.source, this.worldCoordinateHelper);
|
|
136
157
|
}
|
|
137
158
|
|
|
138
159
|
/**
|
|
139
160
|
* Update supercluster options and regenerate the index.
|
|
140
161
|
* @param cluster - whether to enable clustering
|
|
141
|
-
* @param clusterOptions - {@link SuperclusterOptions}
|
|
162
|
+
* @param clusterOptions - {@link SuperclusterOptions} without `coordinates` and `wrap`, which come from the index options
|
|
142
163
|
*/
|
|
143
|
-
updateClusterOptions(cluster: boolean, clusterOptions:
|
|
164
|
+
updateClusterOptions(cluster: boolean, clusterOptions: GeoJSONVTOptions['clusterOptions']) {
|
|
144
165
|
const wasCluster = this.options.cluster;
|
|
145
166
|
this.options.cluster = cluster;
|
|
146
167
|
this.options.clusterOptions = clusterOptions;
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
import type {GeoJSONVTFeatureDiff, GeoJSONVTSourceDiff} from './difference';
|
|
3
|
-
import type {GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, GeoJSONToTileOptions, PartialGeoJSONVTFeature, SliceArray, SliceFixedArray, ClusterProperties, ClusterFeature, ClusterOrPointFeature, GeoJSONVTTile, GeoJSONVTFeature, GeoJSONVTFeaturePoint, GeoJSONVTFeatureNonPoint, GeoJSONVTInternalTileFeaturePoint, GeoJSONVTInternalTileFeatureNonPoint, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTTileIndex, SuperclusterOptions} from './definitions';
|
|
3
|
+
import type {GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, GeoJSONToTileOptions, PartialGeoJSONVTFeature, SliceArray, SliceFixedArray, ClusterProperties, ClusterFeature, ClusterOrPointFeature, GeoJSONVTTile, GeoJSONVTFeature, GeoJSONVTFeaturePoint, GeoJSONVTFeatureNonPoint, GeoJSONVTInternalTileFeaturePoint, GeoJSONVTInternalTileFeatureNonPoint, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTTileIndex, SuperclusterOptions, GeoJSONVTCoordinatesType} from './definitions';
|
|
4
4
|
import type {KDBushWithData} from './cluster-tile-index';
|
|
5
5
|
import {GeoJSONVT} from './geojsonvt';
|
|
6
6
|
import {ClusterTileIndex} from './cluster-tile-index';
|
|
@@ -39,6 +39,7 @@ export type {
|
|
|
39
39
|
GeoJSONVTInternalPolygonFeature,
|
|
40
40
|
GeoJSONVTInternalMultiPolygonFeature,
|
|
41
41
|
SuperclusterOptions,
|
|
42
|
+
GeoJSONVTCoordinatesType,
|
|
42
43
|
ClusterProperties,
|
|
43
44
|
ClusterFeature,
|
|
44
45
|
ClusterOrPointFeature,
|
package/src/tile-index.ts
CHANGED
|
@@ -84,7 +84,7 @@ export class TileIndex implements GeoJSONVTTileIndex {
|
|
|
84
84
|
const {extent, debug} = this.options;
|
|
85
85
|
|
|
86
86
|
const z2 = 1 << z;
|
|
87
|
-
if (this.options.
|
|
87
|
+
if (!this.options.wrap && (x < 0 || x >= z2)) return null; // a row outside the world has no tile either
|
|
88
88
|
x = (x + z2) & (z2 - 1); // wrap tile x coordinate
|
|
89
89
|
|
|
90
90
|
const id = toID(z, x, y);
|
|
@@ -122,14 +122,16 @@ export class TileIndex implements GeoJSONVTTileIndex {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
|
-
* The features the top tile starts from
|
|
126
|
-
* and only what lies inside the world square and its buffer
|
|
127
|
-
*
|
|
125
|
+
* The features the top tile starts from. A world that wraps keeps them all left and right, where wrapping already
|
|
126
|
+
* put them inside, and one that doesn't only what lies inside the world square and its buffer. Positions in the
|
|
127
|
+
* unit square are also cut above and below in a world that wraps, where lng/lat is clamped already. The index keeps
|
|
128
|
+
* the rest, so an update can still move a feature in from outside.
|
|
128
129
|
*/
|
|
129
130
|
private clipToWorld(features: GeoJSONVTInternalFeature[]): GeoJSONVTInternalFeature[] {
|
|
130
|
-
|
|
131
|
+
const {wrap, coordinates} = this.options;
|
|
132
|
+
if (wrap && coordinates !== 'unitSquare') return features;
|
|
131
133
|
const buffer = this.options.buffer / this.options.extent;
|
|
132
|
-
const inX = clip(features, 1, -buffer, 1 + buffer, AxisType.X, -Infinity, Infinity, this.options) || [];
|
|
134
|
+
const inX = wrap ? features : clip(features, 1, -buffer, 1 + buffer, AxisType.X, -Infinity, Infinity, this.options) || [];
|
|
133
135
|
return clip(inX, 1, -buffer, 1 + buffer, AxisType.Y, -Infinity, Infinity, this.options) || [];
|
|
134
136
|
}
|
|
135
137
|
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {GeoJSONVTCoordinatesType} from './definitions';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How the positions of the input relate to the world square the tiles cover, in which x and y run from 0 to 1.
|
|
5
|
+
*/
|
|
6
|
+
export interface WorldCoordinateHelper {
|
|
7
|
+
projectX(x: number): number;
|
|
8
|
+
projectY(y: number): number;
|
|
9
|
+
unprojectX(x: number): number;
|
|
10
|
+
unprojectY(y: number): number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Longitude and latitude, which spherical mercator puts in the world square.
|
|
15
|
+
*/
|
|
16
|
+
class MercatorWorldCoordinateHelper implements WorldCoordinateHelper {
|
|
17
|
+
projectX(lng: number): number {
|
|
18
|
+
return lng / 360 + 0.5;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
projectY(lat: number): number {
|
|
22
|
+
const sin = Math.sin(lat * Math.PI / 180);
|
|
23
|
+
const y = 0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
|
|
24
|
+
return y < 0 ? 0 : y > 1 ? 1 : y;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
unprojectX(x: number): number {
|
|
28
|
+
return (x - 0.5) * 360;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
unprojectY(y: number): number {
|
|
32
|
+
const y2 = (180 - y * 360) * Math.PI / 180;
|
|
33
|
+
return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Positions in the unit square already, see `coordinates` in {@link GeoJSONVTOptions}.
|
|
39
|
+
*/
|
|
40
|
+
class IdentityWorldCoordinateHelper implements WorldCoordinateHelper {
|
|
41
|
+
projectX(x: number): number {
|
|
42
|
+
return x;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
projectY(y: number): number {
|
|
46
|
+
return y;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
unprojectX(x: number): number {
|
|
50
|
+
return x;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
unprojectY(y: number): number {
|
|
54
|
+
return y;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const mercatorWorldCoordinateHelper: WorldCoordinateHelper = new MercatorWorldCoordinateHelper();
|
|
59
|
+
const identityWorldCoordinateHelper: WorldCoordinateHelper = new IdentityWorldCoordinateHelper();
|
|
60
|
+
|
|
61
|
+
export function getWorldCoordinateHelper(coordinates?: GeoJSONVTCoordinatesType): WorldCoordinateHelper {
|
|
62
|
+
return coordinates === 'unitSquare' ? identityWorldCoordinateHelper : mercatorWorldCoordinateHelper;
|
|
63
|
+
}
|
package/src/wrap.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, SliceArray, SliceFixed
|
|
|
4
4
|
import {createFeature, optimizeLineMemory} from './feature';
|
|
5
5
|
|
|
6
6
|
export function wrap(features: GeoJSONVTInternalFeature[], options: GeoJSONVTOptions): GeoJSONVTInternalFeature[] {
|
|
7
|
-
if (options.
|
|
7
|
+
if (!options.wrap) return features;
|
|
8
8
|
|
|
9
9
|
const buffer = options.buffer / options.extent;
|
|
10
10
|
let merged = features;
|