@maplibre/geojson-vt 5.0.0 → 5.0.2
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/geojson-vt-dev.js +511 -534
- package/dist/geojson-vt.js +1 -1
- package/dist/geojson-vt.mjs +1155 -0
- package/dist/geojson-vt.mjs.map +1 -0
- package/package.json +20 -8
- package/src/clip.test.ts +79 -0
- package/src/clip.ts +232 -0
- package/src/convert.ts +178 -0
- package/src/definitions.ts +86 -0
- package/src/difference.test.ts +270 -0
- package/src/{difference.js → difference.ts} +87 -49
- package/src/feature.ts +64 -0
- package/src/{index.js → index.ts} +107 -52
- package/src/simplify.test.ts +73 -0
- package/src/{simplify.js → simplify.ts} +22 -9
- package/src/tile.ts +166 -0
- package/src/transform.ts +55 -0
- package/src/wrap.ts +81 -0
- package/src/clip.js +0 -200
- package/src/convert.js +0 -139
- package/src/feature.js +0 -43
- package/src/tile.js +0 -123
- package/src/transform.js +0 -41
- package/src/wrap.js +0 -68
package/src/wrap.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
|
|
2
|
+
import {clip} from './clip';
|
|
3
|
+
import type { GeoJSONVTFeature, GeoJSONVTOptions, StartEndSizeArray } from './definitions';
|
|
4
|
+
import {createFeature} from './feature';
|
|
5
|
+
|
|
6
|
+
export function wrap(features: GeoJSONVTFeature[], options: GeoJSONVTOptions): GeoJSONVTFeature[] {
|
|
7
|
+
const buffer = options.buffer / options.extent;
|
|
8
|
+
let merged = features;
|
|
9
|
+
|
|
10
|
+
const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
|
|
11
|
+
const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
|
|
12
|
+
|
|
13
|
+
if (!left && !right) return merged;
|
|
14
|
+
|
|
15
|
+
merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
|
|
16
|
+
|
|
17
|
+
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
|
|
18
|
+
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
|
|
19
|
+
|
|
20
|
+
return merged;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function shiftFeatureCoords(features: GeoJSONVTFeature[], offset: number): GeoJSONVTFeature[] {
|
|
24
|
+
const newFeatures = [];
|
|
25
|
+
|
|
26
|
+
for (const feature of features) {
|
|
27
|
+
switch (feature.type) {
|
|
28
|
+
case 'Point':
|
|
29
|
+
case 'MultiPoint':
|
|
30
|
+
case 'LineString': {
|
|
31
|
+
const newGeometry = shiftCoords(feature.geometry, offset);
|
|
32
|
+
|
|
33
|
+
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
case 'MultiLineString':
|
|
38
|
+
case 'Polygon': {
|
|
39
|
+
const newGeometry = [];
|
|
40
|
+
for (const line of feature.geometry) {
|
|
41
|
+
newGeometry.push(shiftCoords(line, offset));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case 'MultiPolygon': {
|
|
49
|
+
const newGeometry = [];
|
|
50
|
+
for (const polygon of feature.geometry) {
|
|
51
|
+
const newPolygon = [];
|
|
52
|
+
for (const line of polygon) {
|
|
53
|
+
newPolygon.push(shiftCoords(line, offset));
|
|
54
|
+
}
|
|
55
|
+
newGeometry.push(newPolygon);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return newFeatures;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function shiftCoords(points: StartEndSizeArray, offset: number): number[] | StartEndSizeArray {
|
|
68
|
+
const newPoints: StartEndSizeArray = [];
|
|
69
|
+
newPoints.size = points.size;
|
|
70
|
+
|
|
71
|
+
if (points.start !== undefined) {
|
|
72
|
+
newPoints.start = points.start;
|
|
73
|
+
newPoints.end = points.end;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (let i = 0; i < points.length; i += 3) {
|
|
77
|
+
newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return newPoints;
|
|
81
|
+
}
|
package/src/clip.js
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import createFeature from './feature.js';
|
|
3
|
-
|
|
4
|
-
/* clip features between two vertical or horizontal axis-parallel lines:
|
|
5
|
-
* | |
|
|
6
|
-
* ___|___ | /
|
|
7
|
-
* / | \____|____/
|
|
8
|
-
* | |
|
|
9
|
-
*
|
|
10
|
-
* k1 and k2 are the line coordinates
|
|
11
|
-
* axis: 0 for x, 1 for y
|
|
12
|
-
* minAll and maxAll: minimum and maximum coordinate value for all features
|
|
13
|
-
*/
|
|
14
|
-
export default function clip(features, scale, k1, k2, axis, minAll, maxAll, options) {
|
|
15
|
-
k1 /= scale;
|
|
16
|
-
k2 /= scale;
|
|
17
|
-
|
|
18
|
-
if (minAll >= k1 && maxAll < k2) return features; // trivial accept
|
|
19
|
-
else if (maxAll < k1 || minAll >= k2) return null; // trivial reject
|
|
20
|
-
|
|
21
|
-
const clipped = [];
|
|
22
|
-
|
|
23
|
-
for (const feature of features) {
|
|
24
|
-
const geometry = feature.geometry;
|
|
25
|
-
let type = feature.type;
|
|
26
|
-
|
|
27
|
-
const min = axis === 0 ? feature.minX : feature.minY;
|
|
28
|
-
const max = axis === 0 ? feature.maxX : feature.maxY;
|
|
29
|
-
|
|
30
|
-
if (min >= k1 && max < k2) { // trivial accept
|
|
31
|
-
clipped.push(feature);
|
|
32
|
-
continue;
|
|
33
|
-
} else if (max < k1 || min >= k2) { // trivial reject
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let newGeometry = [];
|
|
38
|
-
|
|
39
|
-
if (type === 'Point' || type === 'MultiPoint') {
|
|
40
|
-
clipPoints(geometry, newGeometry, k1, k2, axis);
|
|
41
|
-
|
|
42
|
-
} else if (type === 'LineString') {
|
|
43
|
-
clipLine(geometry, newGeometry, k1, k2, axis, false, options.lineMetrics);
|
|
44
|
-
|
|
45
|
-
} else if (type === 'MultiLineString') {
|
|
46
|
-
clipLines(geometry, newGeometry, k1, k2, axis, false);
|
|
47
|
-
|
|
48
|
-
} else if (type === 'Polygon') {
|
|
49
|
-
clipLines(geometry, newGeometry, k1, k2, axis, true);
|
|
50
|
-
|
|
51
|
-
} else if (type === 'MultiPolygon') {
|
|
52
|
-
for (const polygon of geometry) {
|
|
53
|
-
const newPolygon = [];
|
|
54
|
-
clipLines(polygon, newPolygon, k1, k2, axis, true);
|
|
55
|
-
if (newPolygon.length) {
|
|
56
|
-
newGeometry.push(newPolygon);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (newGeometry.length) {
|
|
62
|
-
if (options.lineMetrics && type === 'LineString') {
|
|
63
|
-
for (const line of newGeometry) {
|
|
64
|
-
clipped.push(createFeature(feature.id, type, line, feature.tags));
|
|
65
|
-
}
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (type === 'LineString' || type === 'MultiLineString') {
|
|
70
|
-
if (newGeometry.length === 1) {
|
|
71
|
-
type = 'LineString';
|
|
72
|
-
newGeometry = newGeometry[0];
|
|
73
|
-
} else {
|
|
74
|
-
type = 'MultiLineString';
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (type === 'Point' || type === 'MultiPoint') {
|
|
78
|
-
type = newGeometry.length === 3 ? 'Point' : 'MultiPoint';
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
clipped.push(createFeature(feature.id, type, newGeometry, feature.tags));
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return clipped.length ? clipped : null;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function clipPoints(geom, newGeom, k1, k2, axis) {
|
|
89
|
-
for (let i = 0; i < geom.length; i += 3) {
|
|
90
|
-
const a = geom[i + axis];
|
|
91
|
-
|
|
92
|
-
if (a >= k1 && a <= k2) {
|
|
93
|
-
addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function clipLine(geom, newGeom, k1, k2, axis, isPolygon, trackMetrics) {
|
|
99
|
-
|
|
100
|
-
let slice = newSlice(geom);
|
|
101
|
-
const intersect = axis === 0 ? intersectX : intersectY;
|
|
102
|
-
let len = geom.start;
|
|
103
|
-
let segLen, t;
|
|
104
|
-
|
|
105
|
-
for (let i = 0; i < geom.length - 3; i += 3) {
|
|
106
|
-
const ax = geom[i];
|
|
107
|
-
const ay = geom[i + 1];
|
|
108
|
-
const az = geom[i + 2];
|
|
109
|
-
const bx = geom[i + 3];
|
|
110
|
-
const by = geom[i + 4];
|
|
111
|
-
const a = axis === 0 ? ax : ay;
|
|
112
|
-
const b = axis === 0 ? bx : by;
|
|
113
|
-
let exited = false;
|
|
114
|
-
|
|
115
|
-
if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
|
|
116
|
-
|
|
117
|
-
if (a < k1) {
|
|
118
|
-
// ---|--> | (line enters the clip region from the left)
|
|
119
|
-
if (b > k1) {
|
|
120
|
-
t = intersect(slice, ax, ay, bx, by, k1);
|
|
121
|
-
if (trackMetrics) slice.start = len + segLen * t;
|
|
122
|
-
}
|
|
123
|
-
} else if (a > k2) {
|
|
124
|
-
// | <--|--- (line enters the clip region from the right)
|
|
125
|
-
if (b < k2) {
|
|
126
|
-
t = intersect(slice, ax, ay, bx, by, k2);
|
|
127
|
-
if (trackMetrics) slice.start = len + segLen * t;
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
addPoint(slice, ax, ay, az);
|
|
131
|
-
}
|
|
132
|
-
if (b < k1 && a >= k1) {
|
|
133
|
-
// <--|--- | or <--|-----|--- (line exits the clip region on the left)
|
|
134
|
-
t = intersect(slice, ax, ay, bx, by, k1);
|
|
135
|
-
exited = true;
|
|
136
|
-
}
|
|
137
|
-
if (b > k2 && a <= k2) {
|
|
138
|
-
// | ---|--> or ---|-----|--> (line exits the clip region on the right)
|
|
139
|
-
t = intersect(slice, ax, ay, bx, by, k2);
|
|
140
|
-
exited = true;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (!isPolygon && exited) {
|
|
144
|
-
if (trackMetrics) slice.end = len + segLen * t;
|
|
145
|
-
newGeom.push(slice);
|
|
146
|
-
slice = newSlice(geom);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (trackMetrics) len += segLen;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// add the last point
|
|
153
|
-
let last = geom.length - 3;
|
|
154
|
-
const ax = geom[last];
|
|
155
|
-
const ay = geom[last + 1];
|
|
156
|
-
const az = geom[last + 2];
|
|
157
|
-
const a = axis === 0 ? ax : ay;
|
|
158
|
-
if (a >= k1 && a <= k2) addPoint(slice, ax, ay, az);
|
|
159
|
-
|
|
160
|
-
// close the polygon if its endpoints are not the same after clipping
|
|
161
|
-
last = slice.length - 3;
|
|
162
|
-
if (isPolygon && last >= 3 && (slice[last] !== slice[0] || slice[last + 1] !== slice[1])) {
|
|
163
|
-
addPoint(slice, slice[0], slice[1], slice[2]);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// add the final slice
|
|
167
|
-
if (slice.length) {
|
|
168
|
-
newGeom.push(slice);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function newSlice(line) {
|
|
173
|
-
const slice = [];
|
|
174
|
-
slice.size = line.size;
|
|
175
|
-
slice.start = line.start;
|
|
176
|
-
slice.end = line.end;
|
|
177
|
-
return slice;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function clipLines(geom, newGeom, k1, k2, axis, isPolygon) {
|
|
181
|
-
for (const line of geom) {
|
|
182
|
-
clipLine(line, newGeom, k1, k2, axis, isPolygon, false);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function addPoint(out, x, y, z) {
|
|
187
|
-
out.push(x, y, z);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function intersectX(out, ax, ay, bx, by, x) {
|
|
191
|
-
const t = (x - ax) / (bx - ax);
|
|
192
|
-
addPoint(out, x, ay + (by - ay) * t, 1);
|
|
193
|
-
return t;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function intersectY(out, ax, ay, bx, by, y) {
|
|
197
|
-
const t = (y - ay) / (by - ay);
|
|
198
|
-
addPoint(out, ax + (bx - ax) * t, y, 1);
|
|
199
|
-
return t;
|
|
200
|
-
}
|
package/src/convert.js
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import simplify from './simplify.js';
|
|
3
|
-
import createFeature from './feature.js';
|
|
4
|
-
|
|
5
|
-
// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
|
|
6
|
-
|
|
7
|
-
export default function convert(data, options) {
|
|
8
|
-
const features = [];
|
|
9
|
-
if (data.type === 'FeatureCollection') {
|
|
10
|
-
for (let i = 0; i < data.features.length; i++) {
|
|
11
|
-
convertFeature(features, data.features[i], options, i);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
} else if (data.type === 'Feature') {
|
|
15
|
-
convertFeature(features, data, options);
|
|
16
|
-
|
|
17
|
-
} else {
|
|
18
|
-
// single geometry or a geometry collection
|
|
19
|
-
convertFeature(features, {geometry: data}, options);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return features;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function convertFeature(features, geojson, options, index) {
|
|
26
|
-
if (!geojson.geometry) return;
|
|
27
|
-
|
|
28
|
-
const coords = geojson.geometry.coordinates;
|
|
29
|
-
if (coords && coords.length === 0) return;
|
|
30
|
-
|
|
31
|
-
const type = geojson.geometry.type;
|
|
32
|
-
const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
|
|
33
|
-
let geometry = [];
|
|
34
|
-
let id = geojson.id;
|
|
35
|
-
if (options.promoteId) {
|
|
36
|
-
id = geojson.properties[options.promoteId];
|
|
37
|
-
} else if (options.generateId) {
|
|
38
|
-
id = index || 0;
|
|
39
|
-
}
|
|
40
|
-
if (type === 'Point') {
|
|
41
|
-
convertPoint(coords, geometry);
|
|
42
|
-
|
|
43
|
-
} else if (type === 'MultiPoint') {
|
|
44
|
-
for (const p of coords) {
|
|
45
|
-
convertPoint(p, geometry);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
} else if (type === 'LineString') {
|
|
49
|
-
convertLine(coords, geometry, tolerance, false);
|
|
50
|
-
|
|
51
|
-
} else if (type === 'MultiLineString') {
|
|
52
|
-
if (options.lineMetrics) {
|
|
53
|
-
// explode into linestrings to be able to track metrics
|
|
54
|
-
for (const line of coords) {
|
|
55
|
-
geometry = [];
|
|
56
|
-
convertLine(line, geometry, tolerance, false);
|
|
57
|
-
features.push(createFeature(id, 'LineString', geometry, geojson.properties));
|
|
58
|
-
}
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
convertLines(coords, geometry, tolerance, false);
|
|
62
|
-
|
|
63
|
-
} else if (type === 'Polygon') {
|
|
64
|
-
convertLines(coords, geometry, tolerance, true);
|
|
65
|
-
|
|
66
|
-
} else if (type === 'MultiPolygon') {
|
|
67
|
-
for (const polygon of coords) {
|
|
68
|
-
const newPolygon = [];
|
|
69
|
-
convertLines(polygon, newPolygon, tolerance, true);
|
|
70
|
-
geometry.push(newPolygon);
|
|
71
|
-
}
|
|
72
|
-
} else if (type === 'GeometryCollection') {
|
|
73
|
-
for (const singleGeometry of geojson.geometry.geometries) {
|
|
74
|
-
convertFeature(features, {
|
|
75
|
-
id,
|
|
76
|
-
geometry: singleGeometry,
|
|
77
|
-
properties: geojson.properties
|
|
78
|
-
}, options, index);
|
|
79
|
-
}
|
|
80
|
-
return;
|
|
81
|
-
} else {
|
|
82
|
-
throw new Error('Input data is not a valid GeoJSON object.');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
features.push(createFeature(id, type, geometry, geojson.properties));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function convertPoint(coords, out) {
|
|
89
|
-
out.push(projectX(coords[0]), projectY(coords[1]), 0);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function convertLine(ring, out, tolerance, isPolygon) {
|
|
93
|
-
let x0, y0;
|
|
94
|
-
let size = 0;
|
|
95
|
-
|
|
96
|
-
for (let j = 0; j < ring.length; j++) {
|
|
97
|
-
const x = projectX(ring[j][0]);
|
|
98
|
-
const y = projectY(ring[j][1]);
|
|
99
|
-
|
|
100
|
-
out.push(x, y, 0);
|
|
101
|
-
|
|
102
|
-
if (j > 0) {
|
|
103
|
-
if (isPolygon) {
|
|
104
|
-
size += (x0 * y - x * y0) / 2; // area
|
|
105
|
-
} else {
|
|
106
|
-
size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)); // length
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
x0 = x;
|
|
110
|
-
y0 = y;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const last = out.length - 3;
|
|
114
|
-
out[2] = 1;
|
|
115
|
-
if (tolerance > 0) simplify(out, 0, last, tolerance);
|
|
116
|
-
out[last + 2] = 1;
|
|
117
|
-
|
|
118
|
-
out.size = Math.abs(size);
|
|
119
|
-
out.start = 0;
|
|
120
|
-
out.end = out.size;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function convertLines(rings, out, tolerance, isPolygon) {
|
|
124
|
-
for (let i = 0; i < rings.length; i++) {
|
|
125
|
-
const geom = [];
|
|
126
|
-
convertLine(rings[i], geom, tolerance, isPolygon);
|
|
127
|
-
out.push(geom);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function projectX(x) {
|
|
132
|
-
return x / 360 + 0.5;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function projectY(y) {
|
|
136
|
-
const sin = Math.sin(y * Math.PI / 180);
|
|
137
|
-
const y2 = 0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
|
|
138
|
-
return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
|
|
139
|
-
}
|
package/src/feature.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export default function createFeature(id, type, geom, tags) {
|
|
3
|
-
const feature = {
|
|
4
|
-
id: id == null ? null : id,
|
|
5
|
-
type,
|
|
6
|
-
geometry: geom,
|
|
7
|
-
tags,
|
|
8
|
-
minX: Infinity,
|
|
9
|
-
minY: Infinity,
|
|
10
|
-
maxX: -Infinity,
|
|
11
|
-
maxY: -Infinity
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
|
|
15
|
-
calcLineBBox(feature, geom);
|
|
16
|
-
|
|
17
|
-
} else if (type === 'Polygon') {
|
|
18
|
-
// the outer ring (ie [0]) contains all inner rings
|
|
19
|
-
calcLineBBox(feature, geom[0]);
|
|
20
|
-
|
|
21
|
-
} else if (type === 'MultiLineString') {
|
|
22
|
-
for (const line of geom) {
|
|
23
|
-
calcLineBBox(feature, line);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
} else if (type === 'MultiPolygon') {
|
|
27
|
-
for (const polygon of geom) {
|
|
28
|
-
// the outer ring (ie [0]) contains all inner rings
|
|
29
|
-
calcLineBBox(feature, polygon[0]);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return feature;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function calcLineBBox(feature, geom) {
|
|
37
|
-
for (let i = 0; i < geom.length; i += 3) {
|
|
38
|
-
feature.minX = Math.min(feature.minX, geom[i]);
|
|
39
|
-
feature.minY = Math.min(feature.minY, geom[i + 1]);
|
|
40
|
-
feature.maxX = Math.max(feature.maxX, geom[i]);
|
|
41
|
-
feature.maxY = Math.max(feature.maxY, geom[i + 1]);
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/tile.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export default function createTile(features, z, tx, ty, options) {
|
|
3
|
-
const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
|
|
4
|
-
const tile = {
|
|
5
|
-
features: [],
|
|
6
|
-
numPoints: 0,
|
|
7
|
-
numSimplified: 0,
|
|
8
|
-
numFeatures: features.length,
|
|
9
|
-
source: null,
|
|
10
|
-
x: tx,
|
|
11
|
-
y: ty,
|
|
12
|
-
z,
|
|
13
|
-
transformed: false,
|
|
14
|
-
minX: 2,
|
|
15
|
-
minY: 1,
|
|
16
|
-
maxX: -1,
|
|
17
|
-
maxY: 0
|
|
18
|
-
};
|
|
19
|
-
for (const feature of features) {
|
|
20
|
-
addFeature(tile, feature, tolerance, options);
|
|
21
|
-
}
|
|
22
|
-
return tile;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function addFeature(tile, feature, tolerance, options) {
|
|
26
|
-
const geom = feature.geometry;
|
|
27
|
-
const type = feature.type;
|
|
28
|
-
const simplified = [];
|
|
29
|
-
|
|
30
|
-
tile.minX = Math.min(tile.minX, feature.minX);
|
|
31
|
-
tile.minY = Math.min(tile.minY, feature.minY);
|
|
32
|
-
tile.maxX = Math.max(tile.maxX, feature.maxX);
|
|
33
|
-
tile.maxY = Math.max(tile.maxY, feature.maxY);
|
|
34
|
-
|
|
35
|
-
if (type === 'Point' || type === 'MultiPoint') {
|
|
36
|
-
for (let i = 0; i < geom.length; i += 3) {
|
|
37
|
-
simplified.push(geom[i], geom[i + 1]);
|
|
38
|
-
tile.numPoints++;
|
|
39
|
-
tile.numSimplified++;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
} else if (type === 'LineString') {
|
|
43
|
-
addLine(simplified, geom, tile, tolerance, false, false);
|
|
44
|
-
|
|
45
|
-
} else if (type === 'MultiLineString' || type === 'Polygon') {
|
|
46
|
-
for (let i = 0; i < geom.length; i++) {
|
|
47
|
-
addLine(simplified, geom[i], tile, tolerance, type === 'Polygon', i === 0);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
} else if (type === 'MultiPolygon') {
|
|
51
|
-
|
|
52
|
-
for (let k = 0; k < geom.length; k++) {
|
|
53
|
-
const polygon = geom[k];
|
|
54
|
-
for (let i = 0; i < polygon.length; i++) {
|
|
55
|
-
addLine(simplified, polygon[i], tile, tolerance, true, i === 0);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (simplified.length) {
|
|
61
|
-
let tags = feature.tags || null;
|
|
62
|
-
|
|
63
|
-
if (type === 'LineString' && options.lineMetrics) {
|
|
64
|
-
tags = {};
|
|
65
|
-
for (const key in feature.tags) tags[key] = feature.tags[key];
|
|
66
|
-
/* eslint-disable dot-notation */
|
|
67
|
-
tags['mapbox_clip_start'] = geom.start / geom.size;
|
|
68
|
-
tags['mapbox_clip_end'] = geom.end / geom.size;
|
|
69
|
-
/* eslint-enable dot-notation */
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const tileFeature = {
|
|
73
|
-
geometry: simplified,
|
|
74
|
-
type: type === 'Polygon' || type === 'MultiPolygon' ? 3 :
|
|
75
|
-
(type === 'LineString' || type === 'MultiLineString' ? 2 : 1),
|
|
76
|
-
tags
|
|
77
|
-
};
|
|
78
|
-
if (feature.id !== null) {
|
|
79
|
-
tileFeature.id = feature.id;
|
|
80
|
-
}
|
|
81
|
-
tile.features.push(tileFeature);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function addLine(result, geom, tile, tolerance, isPolygon, isOuter) {
|
|
86
|
-
const sqTolerance = tolerance * tolerance;
|
|
87
|
-
|
|
88
|
-
if (tolerance > 0 && (geom.size < (isPolygon ? sqTolerance : tolerance))) {
|
|
89
|
-
tile.numPoints += geom.length / 3;
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const ring = [];
|
|
94
|
-
|
|
95
|
-
for (let i = 0; i < geom.length; i += 3) {
|
|
96
|
-
if (tolerance === 0 || geom[i + 2] > sqTolerance) {
|
|
97
|
-
tile.numSimplified++;
|
|
98
|
-
ring.push(geom[i], geom[i + 1]);
|
|
99
|
-
}
|
|
100
|
-
tile.numPoints++;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (isPolygon) rewind(ring, isOuter);
|
|
104
|
-
|
|
105
|
-
result.push(ring);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function rewind(ring, clockwise) {
|
|
109
|
-
let area = 0;
|
|
110
|
-
for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) {
|
|
111
|
-
area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
|
|
112
|
-
}
|
|
113
|
-
if (area > 0 === clockwise) {
|
|
114
|
-
for (let i = 0, len = ring.length; i < len / 2; i += 2) {
|
|
115
|
-
const x = ring[i];
|
|
116
|
-
const y = ring[i + 1];
|
|
117
|
-
ring[i] = ring[len - 2 - i];
|
|
118
|
-
ring[i + 1] = ring[len - 1 - i];
|
|
119
|
-
ring[len - 2 - i] = x;
|
|
120
|
-
ring[len - 1 - i] = y;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
package/src/transform.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// Transforms the coordinates of each feature in the given tile from
|
|
3
|
-
// mercator-projected space into (extent x extent) tile space.
|
|
4
|
-
export default function transformTile(tile, extent) {
|
|
5
|
-
if (tile.transformed) return tile;
|
|
6
|
-
|
|
7
|
-
const z2 = 1 << tile.z;
|
|
8
|
-
const tx = tile.x;
|
|
9
|
-
const ty = tile.y;
|
|
10
|
-
|
|
11
|
-
for (const feature of tile.features) {
|
|
12
|
-
const geom = feature.geometry;
|
|
13
|
-
const type = feature.type;
|
|
14
|
-
|
|
15
|
-
feature.geometry = [];
|
|
16
|
-
|
|
17
|
-
if (type === 1) {
|
|
18
|
-
for (let j = 0; j < geom.length; j += 2) {
|
|
19
|
-
feature.geometry.push(transformPoint(geom[j], geom[j + 1], extent, z2, tx, ty));
|
|
20
|
-
}
|
|
21
|
-
} else {
|
|
22
|
-
for (let j = 0; j < geom.length; j++) {
|
|
23
|
-
const ring = [];
|
|
24
|
-
for (let k = 0; k < geom[j].length; k += 2) {
|
|
25
|
-
ring.push(transformPoint(geom[j][k], geom[j][k + 1], extent, z2, tx, ty));
|
|
26
|
-
}
|
|
27
|
-
feature.geometry.push(ring);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
tile.transformed = true;
|
|
33
|
-
|
|
34
|
-
return tile;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function transformPoint(x, y, extent, z2, tx, ty) {
|
|
38
|
-
return [
|
|
39
|
-
Math.round(extent * (x * z2 - tx)),
|
|
40
|
-
Math.round(extent * (y * z2 - ty))];
|
|
41
|
-
}
|
package/src/wrap.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import clip from './clip.js';
|
|
3
|
-
import createFeature from './feature.js';
|
|
4
|
-
|
|
5
|
-
export default function wrap(features, options) {
|
|
6
|
-
const buffer = options.buffer / options.extent;
|
|
7
|
-
let merged = features;
|
|
8
|
-
const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
|
|
9
|
-
const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
|
|
10
|
-
|
|
11
|
-
if (left || right) {
|
|
12
|
-
merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
|
|
13
|
-
|
|
14
|
-
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
|
|
15
|
-
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return merged;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function shiftFeatureCoords(features, offset) {
|
|
22
|
-
const newFeatures = [];
|
|
23
|
-
|
|
24
|
-
for (let i = 0; i < features.length; i++) {
|
|
25
|
-
const feature = features[i];
|
|
26
|
-
const type = feature.type;
|
|
27
|
-
|
|
28
|
-
let newGeometry;
|
|
29
|
-
|
|
30
|
-
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
|
|
31
|
-
newGeometry = shiftCoords(feature.geometry, offset);
|
|
32
|
-
|
|
33
|
-
} else if (type === 'MultiLineString' || type === 'Polygon') {
|
|
34
|
-
newGeometry = [];
|
|
35
|
-
for (const line of feature.geometry) {
|
|
36
|
-
newGeometry.push(shiftCoords(line, offset));
|
|
37
|
-
}
|
|
38
|
-
} else if (type === 'MultiPolygon') {
|
|
39
|
-
newGeometry = [];
|
|
40
|
-
for (const polygon of feature.geometry) {
|
|
41
|
-
const newPolygon = [];
|
|
42
|
-
for (const line of polygon) {
|
|
43
|
-
newPolygon.push(shiftCoords(line, offset));
|
|
44
|
-
}
|
|
45
|
-
newGeometry.push(newPolygon);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
newFeatures.push(createFeature(feature.id, type, newGeometry, feature.tags));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return newFeatures;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function shiftCoords(points, offset) {
|
|
56
|
-
const newPoints = [];
|
|
57
|
-
newPoints.size = points.size;
|
|
58
|
-
|
|
59
|
-
if (points.start !== undefined) {
|
|
60
|
-
newPoints.start = points.start;
|
|
61
|
-
newPoints.end = points.end;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
for (let i = 0; i < points.length; i += 3) {
|
|
65
|
-
newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
|
|
66
|
-
}
|
|
67
|
-
return newPoints;
|
|
68
|
-
}
|