@loaders.gl/mvt 3.2.12 → 3.3.0-alpha.10
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/dist/dist.min.js +782 -10
- package/dist/es5/bundle.js +0 -1
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/helpers/binary-util-functions.js +2 -12
- package/dist/es5/helpers/binary-util-functions.js.map +1 -1
- package/dist/es5/helpers/mapbox-util-functions.js +2 -10
- package/dist/es5/helpers/mapbox-util-functions.js.map +1 -1
- package/dist/es5/index.js +7 -1
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +14 -35
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +3 -14
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile.js +3 -10
- package/dist/es5/lib/binary-vector-tile/vector-tile.js.map +1 -1
- package/dist/es5/lib/geojson-tiler/LICENSE +19 -0
- package/dist/es5/lib/geojson-tiler/clip.js +247 -0
- package/dist/es5/lib/geojson-tiler/clip.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/convert.js +163 -0
- package/dist/es5/lib/geojson-tiler/convert.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/feature.js +63 -0
- package/dist/es5/lib/geojson-tiler/feature.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/geojson-tiler.js +203 -0
- package/dist/es5/lib/geojson-tiler/geojson-tiler.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/simplify.js +54 -0
- package/dist/es5/lib/geojson-tiler/simplify.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/tile.js +127 -0
- package/dist/es5/lib/geojson-tiler/tile.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/transform.js +52 -0
- package/dist/es5/lib/geojson-tiler/transform.js.map +1 -0
- package/dist/es5/lib/geojson-tiler/wrap.js +127 -0
- package/dist/es5/lib/geojson-tiler/wrap.js.map +1 -0
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js +8 -35
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js +4 -14
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/es5/lib/mapbox-vector-tile/vector-tile.js +3 -10
- package/dist/es5/lib/mapbox-vector-tile/vector-tile.js.map +1 -1
- package/dist/es5/lib/parse-mvt.js +3 -37
- package/dist/es5/lib/parse-mvt.js.map +1 -1
- package/dist/es5/lib/types.js.map +1 -1
- package/dist/es5/mvt-loader.js +9 -18
- package/dist/es5/mvt-loader.js.map +1 -1
- package/dist/es5/workers/mvt-worker.js +0 -2
- package/dist/es5/workers/mvt-worker.js.map +1 -1
- package/dist/esm/bundle.js +1 -1
- package/dist/esm/bundle.js.map +1 -1
- package/dist/esm/helpers/binary-util-functions.js +5 -10
- package/dist/esm/helpers/binary-util-functions.js.map +1 -1
- package/dist/esm/helpers/mapbox-util-functions.js +5 -8
- package/dist/esm/helpers/mapbox-util-functions.js.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +6 -25
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +2 -14
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile.js +1 -3
- package/dist/esm/lib/binary-vector-tile/vector-tile.js.map +1 -1
- package/dist/esm/lib/geojson-tiler/LICENSE +19 -0
- package/dist/esm/lib/geojson-tiler/clip.js +184 -0
- package/dist/esm/lib/geojson-tiler/clip.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/convert.js +120 -0
- package/dist/esm/lib/geojson-tiler/convert.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/feature.js +37 -0
- package/dist/esm/lib/geojson-tiler/feature.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/geojson-tiler.js +189 -0
- package/dist/esm/lib/geojson-tiler/geojson-tiler.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/simplify.js +49 -0
- package/dist/esm/lib/geojson-tiler/simplify.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/tile.js +108 -0
- package/dist/esm/lib/geojson-tiler/tile.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/transform.js +35 -0
- package/dist/esm/lib/geojson-tiler/transform.js.map +1 -0
- package/dist/esm/lib/geojson-tiler/wrap.js +75 -0
- package/dist/esm/lib/geojson-tiler/wrap.js.map +1 -0
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js +2 -36
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js +2 -14
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/esm/lib/mapbox-vector-tile/vector-tile.js +1 -3
- package/dist/esm/lib/mapbox-vector-tile/vector-tile.js.map +1 -1
- package/dist/esm/lib/parse-mvt.js +0 -26
- package/dist/esm/lib/parse-mvt.js.map +1 -1
- package/dist/esm/lib/types.js.map +1 -1
- package/dist/esm/mvt-loader.js +9 -3
- package/dist/esm/mvt-loader.js.map +1 -1
- package/dist/esm/workers/mvt-worker.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/lib/geojson-tiler/clip.d.ts +16 -0
- package/dist/lib/geojson-tiler/clip.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/clip.js +209 -0
- package/dist/lib/geojson-tiler/convert.d.ts +10 -0
- package/dist/lib/geojson-tiler/convert.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/convert.js +134 -0
- package/dist/lib/geojson-tiler/feature.d.ts +3 -0
- package/dist/lib/geojson-tiler/feature.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/feature.js +46 -0
- package/dist/lib/geojson-tiler/geojson-tiler.d.ts +44 -0
- package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/geojson-tiler.js +210 -0
- package/dist/lib/geojson-tiler/simplify.d.ts +10 -0
- package/dist/lib/geojson-tiler/simplify.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/simplify.js +68 -0
- package/dist/lib/geojson-tiler/tile.d.ts +38 -0
- package/dist/lib/geojson-tiler/tile.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/tile.js +125 -0
- package/dist/lib/geojson-tiler/transform.d.ts +7 -0
- package/dist/lib/geojson-tiler/transform.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/transform.js +43 -0
- package/dist/lib/geojson-tiler/wrap.d.ts +17 -0
- package/dist/lib/geojson-tiler/wrap.d.ts.map +1 -0
- package/dist/lib/geojson-tiler/wrap.js +86 -0
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +3 -3
- package/dist/lib/types.d.ts +6 -6
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/mvt-worker.js +10 -11
- package/package.json +5 -5
- package/src/index.ts +7 -0
- package/src/lib/geojson-tiler/LICENSE +19 -0
- package/src/lib/geojson-tiler/clip.ts +248 -0
- package/src/lib/geojson-tiler/convert.ts +158 -0
- package/src/lib/geojson-tiler/feature.ts +45 -0
- package/src/lib/geojson-tiler/geojson-tiler.ts +281 -0
- package/src/lib/geojson-tiler/simplify.ts +77 -0
- package/src/lib/geojson-tiler/tile.ts +185 -0
- package/src/lib/geojson-tiler/transform.ts +55 -0
- package/src/lib/geojson-tiler/wrap.ts +108 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
// Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
|
|
3
|
+
|
|
4
|
+
import type {GeoJSONTileFeature} from './tile';
|
|
5
|
+
import {createFeature} from './feature';
|
|
6
|
+
|
|
7
|
+
/* eslint-disable no-continue */
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Clip features between two vertical or horizontal axis-parallel lines:
|
|
11
|
+
* | |
|
|
12
|
+
* ___|___ | /
|
|
13
|
+
* / | \____|____/
|
|
14
|
+
* | |
|
|
15
|
+
*
|
|
16
|
+
* @param k1 and k2 are the line coordinates
|
|
17
|
+
* @param axis: 0 for x, 1 for y
|
|
18
|
+
* @param minAll and maxAll: minimum and maximum coordinate value for all features
|
|
19
|
+
*/
|
|
20
|
+
// eslint-disable-next-line max-params, complexity, max-statements
|
|
21
|
+
export function clip(
|
|
22
|
+
features: GeoJSONTileFeature[],
|
|
23
|
+
scale: number,
|
|
24
|
+
k1: number,
|
|
25
|
+
k2: number,
|
|
26
|
+
axis,
|
|
27
|
+
minAll: number,
|
|
28
|
+
maxAll: number,
|
|
29
|
+
options: {lineMetrics: boolean}
|
|
30
|
+
): GeoJSONTileFeature[] | null {
|
|
31
|
+
k1 /= scale;
|
|
32
|
+
k2 /= scale;
|
|
33
|
+
|
|
34
|
+
if (minAll >= k1 && maxAll < k2) {
|
|
35
|
+
return features;
|
|
36
|
+
}
|
|
37
|
+
// trivial accept
|
|
38
|
+
else if (maxAll < k1 || minAll >= k2) {
|
|
39
|
+
return null; // trivial reject
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const clipped: GeoJSONTileFeature[] = [];
|
|
43
|
+
|
|
44
|
+
for (const feature of features) {
|
|
45
|
+
const geometry = feature.geometry;
|
|
46
|
+
let type = feature.type;
|
|
47
|
+
|
|
48
|
+
const min = axis === 0 ? feature.minX : feature.minY;
|
|
49
|
+
const max = axis === 0 ? feature.maxX : feature.maxY;
|
|
50
|
+
|
|
51
|
+
if (min >= k1 && max < k2) {
|
|
52
|
+
// trivial accept
|
|
53
|
+
clipped.push(feature);
|
|
54
|
+
continue;
|
|
55
|
+
} else if (max < k1 || min >= k2) {
|
|
56
|
+
// trivial reject
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let newGeometry: number[][][] | number[][] = [];
|
|
61
|
+
|
|
62
|
+
if (type === 'Point' || type === 'MultiPoint') {
|
|
63
|
+
clipPoints(geometry, newGeometry, k1, k2, axis);
|
|
64
|
+
} else if (type === 'LineString') {
|
|
65
|
+
clipLine(geometry, newGeometry, k1, k2, axis, false, options.lineMetrics);
|
|
66
|
+
} else if (type === 'MultiLineString') {
|
|
67
|
+
clipLines(geometry, newGeometry, k1, k2, axis, false);
|
|
68
|
+
} else if (type === 'Polygon') {
|
|
69
|
+
clipLines(geometry, newGeometry, k1, k2, axis, true);
|
|
70
|
+
} else if (type === 'MultiPolygon') {
|
|
71
|
+
for (const polygon of geometry) {
|
|
72
|
+
const newPolygon = [];
|
|
73
|
+
clipLines(polygon, newPolygon, k1, k2, axis, true);
|
|
74
|
+
if (newPolygon.length) {
|
|
75
|
+
newGeometry.push(newPolygon);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (newGeometry.length) {
|
|
81
|
+
if (options.lineMetrics && type === 'LineString') {
|
|
82
|
+
for (const line of newGeometry) {
|
|
83
|
+
clipped.push(createFeature(feature.id, type, line, feature.tags));
|
|
84
|
+
}
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (type === 'LineString' || type === 'MultiLineString') {
|
|
89
|
+
if (newGeometry.length === 1) {
|
|
90
|
+
type = 'LineString';
|
|
91
|
+
// @ts-expect-error TODO - use proper GeoJSON geometry types
|
|
92
|
+
newGeometry = newGeometry[0];
|
|
93
|
+
} else {
|
|
94
|
+
type = 'MultiLineString';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (type === 'Point' || type === 'MultiPoint') {
|
|
98
|
+
type = newGeometry.length === 3 ? 'Point' : 'MultiPoint';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
clipped.push(createFeature(feature.id, type, newGeometry, feature.tags));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return clipped.length ? clipped : null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function clipPoints(geom, newGeom, k1: number, k2: number, axis): void {
|
|
109
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
110
|
+
const a = geom[i + axis];
|
|
111
|
+
|
|
112
|
+
if (a >= k1 && a <= k2) {
|
|
113
|
+
addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// eslint-disable-next-line max-params, complexity, max-statements
|
|
119
|
+
function clipLine(
|
|
120
|
+
geom,
|
|
121
|
+
newGeom,
|
|
122
|
+
k1: number,
|
|
123
|
+
k2: number,
|
|
124
|
+
axis,
|
|
125
|
+
isPolygon: boolean,
|
|
126
|
+
trackMetrics: boolean
|
|
127
|
+
): void {
|
|
128
|
+
let slice = newSlice(geom);
|
|
129
|
+
const intersect = axis === 0 ? intersectX : intersectY;
|
|
130
|
+
let len = geom.start;
|
|
131
|
+
let segLen;
|
|
132
|
+
let t;
|
|
133
|
+
|
|
134
|
+
for (let i = 0; i < geom.length - 3; i += 3) {
|
|
135
|
+
const ax = geom[i];
|
|
136
|
+
const ay = geom[i + 1];
|
|
137
|
+
const az = geom[i + 2];
|
|
138
|
+
const bx = geom[i + 3];
|
|
139
|
+
const by = geom[i + 4];
|
|
140
|
+
const a = axis === 0 ? ax : ay;
|
|
141
|
+
const b = axis === 0 ? bx : by;
|
|
142
|
+
let exited = false;
|
|
143
|
+
|
|
144
|
+
if (trackMetrics) {
|
|
145
|
+
segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (a < k1) {
|
|
149
|
+
// ---|--> | (line enters the clip region from the left)
|
|
150
|
+
if (b > k1) {
|
|
151
|
+
t = intersect(slice, ax, ay, bx, by, k1);
|
|
152
|
+
if (trackMetrics) {
|
|
153
|
+
slice.start = len + segLen * t;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} else if (a > k2) {
|
|
157
|
+
// | <--|--- (line enters the clip region from the right)
|
|
158
|
+
if (b < k2) {
|
|
159
|
+
t = intersect(slice, ax, ay, bx, by, k2);
|
|
160
|
+
if (trackMetrics) {
|
|
161
|
+
slice.start = len + segLen * t;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
addPoint(slice, ax, ay, az);
|
|
166
|
+
}
|
|
167
|
+
if (b < k1 && a >= k1) {
|
|
168
|
+
// <--|--- | or <--|-----|--- (line exits the clip region on the left)
|
|
169
|
+
t = intersect(slice, ax, ay, bx, by, k1);
|
|
170
|
+
exited = true;
|
|
171
|
+
}
|
|
172
|
+
if (b > k2 && a <= k2) {
|
|
173
|
+
// | ---|--> or ---|-----|--> (line exits the clip region on the right)
|
|
174
|
+
t = intersect(slice, ax, ay, bx, by, k2);
|
|
175
|
+
exited = true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!isPolygon && exited) {
|
|
179
|
+
if (trackMetrics) {
|
|
180
|
+
slice.end = len + segLen * t;
|
|
181
|
+
}
|
|
182
|
+
newGeom.push(slice);
|
|
183
|
+
slice = newSlice(geom);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (trackMetrics) {
|
|
187
|
+
len += segLen;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// add the last point
|
|
192
|
+
let last = geom.length - 3;
|
|
193
|
+
const ax = geom[last];
|
|
194
|
+
const ay = geom[last + 1];
|
|
195
|
+
const az = geom[last + 2];
|
|
196
|
+
const a = axis === 0 ? ax : ay;
|
|
197
|
+
if (a >= k1 && a <= k2) addPoint(slice, ax, ay, az);
|
|
198
|
+
|
|
199
|
+
// close the polygon if its endpoints are not the same after clipping
|
|
200
|
+
last = slice.length - 3;
|
|
201
|
+
if (isPolygon && last >= 3 && (slice[last] !== slice[0] || slice[last + 1] !== slice[1])) {
|
|
202
|
+
addPoint(slice, slice[0], slice[1], slice[2]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// add the final slice
|
|
206
|
+
if (slice.length) {
|
|
207
|
+
newGeom.push(slice);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
class Slice extends Array<number> {
|
|
212
|
+
size?: number;
|
|
213
|
+
start?: number;
|
|
214
|
+
end?: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function newSlice(line: {size: number; start: number; end: number}): Slice {
|
|
218
|
+
const slice: Slice = [];
|
|
219
|
+
slice.size = line.size;
|
|
220
|
+
slice.start = line.start;
|
|
221
|
+
slice.end = line.end;
|
|
222
|
+
return slice;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// eslint-disable-next-line max-params
|
|
226
|
+
function clipLines(geom, newGeom, k1: number, k2: number, axis, isPolygon: boolean): void {
|
|
227
|
+
for (const line of geom) {
|
|
228
|
+
clipLine(line, newGeom, k1, k2, axis, isPolygon, false);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function addPoint(out: number[], x: number, y: number, z: number): void {
|
|
233
|
+
out.push(x, y, z);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// eslint-disable-next-line max-params
|
|
237
|
+
function intersectX(out, ax: number, ay: number, bx: number, by: number, x: number): number {
|
|
238
|
+
const t = (x - ax) / (bx - ax);
|
|
239
|
+
addPoint(out, x, ay + (by - ay) * t, 1);
|
|
240
|
+
return t;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// eslint-disable-next-line max-params
|
|
244
|
+
function intersectY(out, ax: number, ay: number, bx: number, by: number, y): number {
|
|
245
|
+
const t = (y - ay) / (by - ay);
|
|
246
|
+
addPoint(out, ax + (bx - ax) * t, y, 1);
|
|
247
|
+
return t;
|
|
248
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
// Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
|
|
3
|
+
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
// @ts-nocheck
|
|
6
|
+
|
|
7
|
+
import type {Feature} from '@loaders.gl/schema';
|
|
8
|
+
import type {GeoJSONTileFeature} from './tile';
|
|
9
|
+
|
|
10
|
+
import {simplify} from './simplify';
|
|
11
|
+
import {createFeature} from './feature';
|
|
12
|
+
|
|
13
|
+
// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
|
|
14
|
+
|
|
15
|
+
export function convert(data: Feature, options): GeoJSONTileFeature[] {
|
|
16
|
+
const features = [];
|
|
17
|
+
if (data.type === 'FeatureCollection') {
|
|
18
|
+
for (let i = 0; i < data.features.length; i++) {
|
|
19
|
+
convertFeature(features, data.features[i], options, i);
|
|
20
|
+
}
|
|
21
|
+
} else if (data.type === 'Feature') {
|
|
22
|
+
convertFeature(features, data, options);
|
|
23
|
+
} else {
|
|
24
|
+
// single geometry or a geometry collection
|
|
25
|
+
convertFeature(features, {geometry: data}, options);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return features;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ConvertFeatureOptions = {
|
|
32
|
+
maxZoom?: number;
|
|
33
|
+
tolerance: number;
|
|
34
|
+
extent: number;
|
|
35
|
+
lineMetrics: boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function convertFeature(
|
|
39
|
+
features: GeoJSONTileFeature[],
|
|
40
|
+
geojson: Feature,
|
|
41
|
+
options: ConvertFeatureOptions,
|
|
42
|
+
index: number
|
|
43
|
+
): void {
|
|
44
|
+
if (!geojson.geometry) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const coords = geojson.geometry.coordinates;
|
|
49
|
+
const type = geojson.geometry.type;
|
|
50
|
+
const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
|
|
51
|
+
let geometry = [];
|
|
52
|
+
let id = geojson.id;
|
|
53
|
+
if (options.promoteId) {
|
|
54
|
+
id = geojson.properties[options.promoteId];
|
|
55
|
+
} else if (options.generateId) {
|
|
56
|
+
id = index || 0;
|
|
57
|
+
}
|
|
58
|
+
if (type === 'Point') {
|
|
59
|
+
convertPoint(coords, geometry);
|
|
60
|
+
} else if (type === 'MultiPoint') {
|
|
61
|
+
for (const p of coords) {
|
|
62
|
+
convertPoint(p, geometry);
|
|
63
|
+
}
|
|
64
|
+
} else if (type === 'LineString') {
|
|
65
|
+
convertLine(coords, geometry, tolerance, false);
|
|
66
|
+
} else if (type === 'MultiLineString') {
|
|
67
|
+
if (options.lineMetrics) {
|
|
68
|
+
// explode into linestrings to be able to track metrics
|
|
69
|
+
for (const line of coords) {
|
|
70
|
+
geometry = [];
|
|
71
|
+
convertLine(line, geometry, tolerance, false);
|
|
72
|
+
features.push(createFeature(id, 'LineString', geometry, geojson.properties));
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
} else {
|
|
76
|
+
convertLines(coords, geometry, tolerance, false);
|
|
77
|
+
}
|
|
78
|
+
} else if (type === 'Polygon') {
|
|
79
|
+
convertLines(coords, geometry, tolerance, true);
|
|
80
|
+
} else if (type === 'MultiPolygon') {
|
|
81
|
+
for (const polygon of coords) {
|
|
82
|
+
const newPolygon = [];
|
|
83
|
+
convertLines(polygon, newPolygon, tolerance, true);
|
|
84
|
+
geometry.push(newPolygon);
|
|
85
|
+
}
|
|
86
|
+
} else if (type === 'GeometryCollection') {
|
|
87
|
+
for (const singleGeometry of geojson.geometry.geometries) {
|
|
88
|
+
convertFeature(
|
|
89
|
+
features,
|
|
90
|
+
{
|
|
91
|
+
id,
|
|
92
|
+
geometry: singleGeometry,
|
|
93
|
+
properties: geojson.properties
|
|
94
|
+
},
|
|
95
|
+
options,
|
|
96
|
+
index
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
} else {
|
|
101
|
+
throw new Error('Input data is not a valid GeoJSON object.');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
features.push(createFeature(id, type, geometry, geojson.properties));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function convertPoint(coords, out): void {
|
|
108
|
+
out.push(projectX(coords[0]), projectY(coords[1]), 0);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function convertLine(ring: number[], out, tolerance: number, isPolygon: boolean): void {
|
|
112
|
+
let x0, y0;
|
|
113
|
+
let size = 0;
|
|
114
|
+
|
|
115
|
+
for (let j = 0; j < ring.length; j++) {
|
|
116
|
+
const x = projectX(ring[j][0]);
|
|
117
|
+
const y = projectY(ring[j][1]);
|
|
118
|
+
|
|
119
|
+
out.push(x, y, 0);
|
|
120
|
+
|
|
121
|
+
if (j > 0) {
|
|
122
|
+
if (isPolygon) {
|
|
123
|
+
size += (x0 * y - x * y0) / 2; // area
|
|
124
|
+
} else {
|
|
125
|
+
size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)); // length
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
x0 = x;
|
|
129
|
+
y0 = y;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const last = out.length - 3;
|
|
133
|
+
out[2] = 1;
|
|
134
|
+
simplify(out, 0, last, tolerance);
|
|
135
|
+
out[last + 2] = 1;
|
|
136
|
+
|
|
137
|
+
out.size = Math.abs(size);
|
|
138
|
+
out.start = 0;
|
|
139
|
+
out.end = out.size;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function convertLines(rings: number[][], out, tolerance: number, isPolygon: boolean): void {
|
|
143
|
+
for (let i = 0; i < rings.length; i++) {
|
|
144
|
+
const geom = [];
|
|
145
|
+
convertLine(rings[i], geom, tolerance, isPolygon);
|
|
146
|
+
out.push(geom);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function projectX(x: number): number {
|
|
151
|
+
return x / 360 + 0.5;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function projectY(y: number): number {
|
|
155
|
+
const sin = Math.sin((y * Math.PI) / 180);
|
|
156
|
+
const y2 = 0.5 - (0.25 * Math.log((1 + sin) / (1 - sin))) / Math.PI;
|
|
157
|
+
return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
|
|
158
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
// Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
|
|
3
|
+
|
|
4
|
+
import {GeoJSONTileFeature} from './tile';
|
|
5
|
+
|
|
6
|
+
export function createFeature(id, type, geom, tags): GeoJSONTileFeature {
|
|
7
|
+
const feature: GeoJSONTileFeature = {
|
|
8
|
+
// eslint-disable-next-line
|
|
9
|
+
id: id == null ? null : id,
|
|
10
|
+
type,
|
|
11
|
+
geometry: geom,
|
|
12
|
+
tags,
|
|
13
|
+
minX: Infinity,
|
|
14
|
+
minY: Infinity,
|
|
15
|
+
maxX: -Infinity,
|
|
16
|
+
maxY: -Infinity
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
|
|
20
|
+
calcLineBBox(feature, geom);
|
|
21
|
+
} else if (type === 'Polygon') {
|
|
22
|
+
// the outer ring (ie [0]) contains all inner rings
|
|
23
|
+
calcLineBBox(feature, geom[0]);
|
|
24
|
+
} else if (type === 'MultiLineString') {
|
|
25
|
+
for (const line of geom) {
|
|
26
|
+
calcLineBBox(feature, line);
|
|
27
|
+
}
|
|
28
|
+
} else if (type === 'MultiPolygon') {
|
|
29
|
+
for (const polygon of geom) {
|
|
30
|
+
// the outer ring (ie [0]) contains all inner rings
|
|
31
|
+
calcLineBBox(feature, polygon[0]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return feature;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function calcLineBBox(feature, geom) {
|
|
39
|
+
for (let i = 0; i < geom.length; i += 3) {
|
|
40
|
+
feature.minX = Math.min(feature.minX, geom[i]);
|
|
41
|
+
feature.minY = Math.min(feature.minY, geom[i + 1]);
|
|
42
|
+
feature.maxX = Math.max(feature.maxX, geom[i]);
|
|
43
|
+
feature.maxY = Math.max(feature.maxY, geom[i + 1]);
|
|
44
|
+
}
|
|
45
|
+
}
|