@math.gl/polygon 4.0.0 → 4.1.0-alpha.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/dist/cut-by-grid.d.ts +1 -2
- package/dist/cut-by-grid.js +194 -228
- package/dist/cut-by-mercator-bounds.d.ts +1 -2
- package/dist/cut-by-mercator-bounds.js +124 -124
- package/dist/earcut.d.ts +1 -2
- package/dist/earcut.js +577 -490
- package/dist/index.cjs +16 -36
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +7 -8
- package/dist/index.js +1 -1
- package/dist/lineclip.d.ts +1 -2
- package/dist/lineclip.js +163 -114
- package/dist/polygon-utils.d.ts +5 -6
- package/dist/polygon-utils.js +124 -107
- package/dist/polygon.d.ts +2 -3
- package/dist/polygon.js +61 -49
- package/dist/utils.d.ts +0 -1
- package/dist/utils.js +26 -34
- package/package.json +5 -5
- package/dist/cut-by-grid.d.ts.map +0 -1
- package/dist/cut-by-grid.js.map +0 -1
- package/dist/cut-by-mercator-bounds.d.ts.map +0 -1
- package/dist/cut-by-mercator-bounds.js.map +0 -1
- package/dist/earcut.d.ts.map +0 -1
- package/dist/earcut.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lineclip.d.ts.map +0 -1
- package/dist/lineclip.js.map +0 -1
- package/dist/polygon-utils.d.ts.map +0 -1
- package/dist/polygon-utils.js.map +0 -1
- package/dist/polygon.d.ts.map +0 -1
- package/dist/polygon.js.map +0 -1
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js.map +0 -1
|
@@ -1,145 +1,145 @@
|
|
|
1
1
|
import { cutPolylineByGrid, cutPolygonByGrid } from "./cut-by-grid.js";
|
|
2
2
|
import { getPointAtIndex, push } from "./utils.js";
|
|
3
|
+
// https://en.wikipedia.org/wiki/Web_Mercator_projection
|
|
3
4
|
const DEFAULT_MAX_LATITUDE = 85.051129;
|
|
5
|
+
/** https://user-images.githubusercontent.com/2059298/78465769-938b7a00-76ae-11ea-9b95-1f4c26425ab9.png */
|
|
4
6
|
export function cutPolylineByMercatorBounds(positions, options) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
endIndex
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
for (const part of parts) {
|
|
22
|
-
shiftLongitudesIntoRange(part, size);
|
|
7
|
+
const { size = 2, startIndex = 0, endIndex = positions.length, normalize = true } = options || {};
|
|
8
|
+
// Remap longitudes so that each segment takes the shorter path
|
|
9
|
+
const newPositions = positions.slice(startIndex, endIndex);
|
|
10
|
+
wrapLongitudesForShortestPath(newPositions, size, 0, endIndex - startIndex);
|
|
11
|
+
const parts = cutPolylineByGrid(newPositions, {
|
|
12
|
+
size,
|
|
13
|
+
broken: true,
|
|
14
|
+
gridResolution: 360,
|
|
15
|
+
gridOffset: [-180, -180]
|
|
16
|
+
});
|
|
17
|
+
if (normalize) {
|
|
18
|
+
// Each part is guaranteed to be in a single copy of the world
|
|
19
|
+
// Map longitudes back to [-180, 180]
|
|
20
|
+
for (const part of parts) {
|
|
21
|
+
shiftLongitudesIntoRange(part, size);
|
|
22
|
+
}
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return parts;
|
|
24
|
+
return parts;
|
|
27
25
|
}
|
|
26
|
+
/** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
|
|
28
27
|
export function cutPolygonByMercatorBounds(positions, holeIndices = null, options) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
28
|
+
const { size = 2, normalize = true, edgeTypes = false } = options || {};
|
|
29
|
+
holeIndices = holeIndices || [];
|
|
30
|
+
const newPositions = [];
|
|
31
|
+
const newHoleIndices = [];
|
|
32
|
+
let srcStartIndex = 0;
|
|
33
|
+
let targetIndex = 0;
|
|
34
|
+
for (let ringIndex = 0; ringIndex <= holeIndices.length; ringIndex++) {
|
|
35
|
+
// srcStartIndex/srcEndIndex define the ring in the original positions
|
|
36
|
+
const srcEndIndex = holeIndices[ringIndex] || positions.length;
|
|
37
|
+
// targetStartIndex/targetIndex define the ring in newPositions
|
|
38
|
+
const targetStartIndex = targetIndex;
|
|
39
|
+
// In case the ring contains a pole (e.g. Antarctica), we'll have to insert vertices
|
|
40
|
+
// The insertion point is defined by the vertex closest to the pole
|
|
41
|
+
// Split the the ring by the insertion point when copying to newPositions
|
|
42
|
+
const splitIndex = findSplitIndex(positions, size, srcStartIndex, srcEndIndex);
|
|
43
|
+
for (let i = splitIndex; i < srcEndIndex; i++) {
|
|
44
|
+
newPositions[targetIndex++] = positions[i];
|
|
45
|
+
}
|
|
46
|
+
for (let i = srcStartIndex; i < splitIndex; i++) {
|
|
47
|
+
newPositions[targetIndex++] = positions[i];
|
|
48
|
+
}
|
|
49
|
+
// Remap longitudes so that each segment takes the shorter path
|
|
50
|
+
wrapLongitudesForShortestPath(newPositions, size, targetStartIndex, targetIndex);
|
|
51
|
+
// Handle the case when the ring contains a pole
|
|
52
|
+
insertPoleVertices(newPositions, size, targetStartIndex, targetIndex, options?.maxLatitude);
|
|
53
|
+
srcStartIndex = srcEndIndex;
|
|
54
|
+
newHoleIndices[ringIndex] = targetIndex;
|
|
47
55
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
newHoleIndices.pop();
|
|
57
|
+
const parts = cutPolygonByGrid(newPositions, newHoleIndices, {
|
|
58
|
+
size,
|
|
59
|
+
gridResolution: 360,
|
|
60
|
+
gridOffset: [-180, -180],
|
|
61
|
+
edgeTypes
|
|
62
|
+
});
|
|
63
|
+
if (normalize) {
|
|
64
|
+
// Each part is guaranteed to be in a single copy of the world
|
|
65
|
+
// Map longitudes back to [-180, 180]
|
|
66
|
+
for (const part of parts) {
|
|
67
|
+
// @ts-expect-error (mutates readonly array) May mutate newPositions, which is created by us
|
|
68
|
+
shiftLongitudesIntoRange(part.positions, size);
|
|
69
|
+
}
|
|
51
70
|
}
|
|
52
|
-
|
|
53
|
-
wrapLongitudesForShortestPath(newPositions, size, targetStartIndex, targetIndex);
|
|
54
|
-
insertPoleVertices(newPositions, size, targetStartIndex, targetIndex, options === null || options === void 0 ? void 0 : options.maxLatitude);
|
|
55
|
-
srcStartIndex = srcEndIndex;
|
|
56
|
-
newHoleIndices[ringIndex] = targetIndex;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
newHoleIndices.pop();
|
|
60
|
-
const parts = cutPolygonByGrid(newPositions, newHoleIndices, {
|
|
61
|
-
size,
|
|
62
|
-
gridResolution: 360,
|
|
63
|
-
gridOffset: [-180, -180],
|
|
64
|
-
edgeTypes
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
if (normalize) {
|
|
68
|
-
for (const part of parts) {
|
|
69
|
-
shiftLongitudesIntoRange(part.positions, size);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return parts;
|
|
71
|
+
return parts;
|
|
74
72
|
}
|
|
75
|
-
|
|
73
|
+
/* Helpers */
|
|
74
|
+
// See comments for insertPoleVertices
|
|
76
75
|
function findSplitIndex(positions, size, startIndex, endIndex) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
pointIndex = i - 1;
|
|
76
|
+
let maxLat = -1;
|
|
77
|
+
let pointIndex = -1;
|
|
78
|
+
for (let i = startIndex + 1; i < endIndex; i += size) {
|
|
79
|
+
const lat = Math.abs(positions[i]);
|
|
80
|
+
if (lat > maxLat) {
|
|
81
|
+
maxLat = lat;
|
|
82
|
+
pointIndex = i - 1;
|
|
83
|
+
}
|
|
86
84
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return pointIndex;
|
|
85
|
+
return pointIndex;
|
|
90
86
|
}
|
|
91
|
-
|
|
87
|
+
// https://user-images.githubusercontent.com/2059298/78857483-5987e400-79de-11ea-98fc-0631287a8431.png
|
|
88
|
+
//
|
|
89
|
+
// If the polygon contains a pole, to tesselate it correctly, we need to insert the edge
|
|
90
|
+
// of map into the polygon. This requires adding two vertices that represent the pole, by
|
|
91
|
+
// drawing a perpendicular line to the Mercator map edge from a selected vertex on the ring.
|
|
92
|
+
//
|
|
93
|
+
// We select the insertion position carefully so that the inserted line segments do not
|
|
94
|
+
// intersect with the ring itself. This is ensured by findSplitIndex, which returns the
|
|
95
|
+
// vertex closest to the pole.
|
|
92
96
|
function insertPoleVertices(positions, size, startIndex, endIndex, maxLatitude = DEFAULT_MAX_LATITUDE) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
97
|
+
// Check if the ring contains a pole
|
|
98
|
+
const firstLng = positions[startIndex];
|
|
99
|
+
const lastLng = positions[endIndex - size];
|
|
100
|
+
if (Math.abs(firstLng - lastLng) > 180) {
|
|
101
|
+
// The ring does not make a round trip
|
|
102
|
+
// Add the nearest pole to the vertices so that the polygon tesselates correctly
|
|
103
|
+
const p = getPointAtIndex(positions, 0, size, startIndex);
|
|
104
|
+
// Copy the first vertex to the world of the last vertex
|
|
105
|
+
p[0] += Math.round((lastLng - firstLng) / 360) * 360;
|
|
106
|
+
push(positions, p);
|
|
107
|
+
// Project the copied vertex to the edge of the map
|
|
108
|
+
p[1] = Math.sign(p[1]) * maxLatitude;
|
|
109
|
+
push(positions, p);
|
|
110
|
+
// Project the first vertex to the edge of the map
|
|
111
|
+
p[0] = firstLng;
|
|
112
|
+
push(positions, p);
|
|
113
|
+
}
|
|
105
114
|
}
|
|
106
|
-
|
|
107
115
|
function wrapLongitudesForShortestPath(positions, size, startIndex, endIndex) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
let prevLng = positions[0];
|
|
117
|
+
let lng;
|
|
118
|
+
for (let i = startIndex; i < endIndex; i += size) {
|
|
119
|
+
lng = positions[i];
|
|
120
|
+
const delta = lng - prevLng;
|
|
121
|
+
if (delta > 180 || delta < -180) {
|
|
122
|
+
lng -= Math.round(delta / 360) * 360;
|
|
123
|
+
}
|
|
124
|
+
positions[i] = prevLng = lng;
|
|
117
125
|
}
|
|
118
|
-
|
|
119
|
-
positions[i] = prevLng = lng;
|
|
120
|
-
}
|
|
121
126
|
}
|
|
122
|
-
|
|
123
127
|
function shiftLongitudesIntoRange(positions, size) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
128
|
+
let refLng;
|
|
129
|
+
const pointCount = positions.length / size;
|
|
130
|
+
// Find a longitude that is not on the edge of a world
|
|
131
|
+
// Which we will use to determine which world copy it is
|
|
132
|
+
for (let i = 0; i < pointCount; i++) {
|
|
133
|
+
refLng = positions[i * size];
|
|
134
|
+
if ((refLng + 180) % 360 !== 0) {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const delta = -Math.round(refLng / 360) * 360;
|
|
139
|
+
if (delta === 0) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
for (let i = 0; i < pointCount; i++) {
|
|
143
|
+
positions[i * size] += delta;
|
|
132
144
|
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const delta = -Math.round(refLng / 360) * 360;
|
|
136
|
-
|
|
137
|
-
if (delta === 0) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
for (let i = 0; i < pointCount; i++) {
|
|
142
|
-
positions[i * size] += delta;
|
|
143
|
-
}
|
|
144
145
|
}
|
|
145
|
-
//# sourceMappingURL=cut-by-mercator-bounds.js.map
|
package/dist/earcut.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { NumericArray } from '@math.gl/core';
|
|
2
|
-
import { Plane2D } from
|
|
2
|
+
import { Plane2D } from "./polygon-utils.js";
|
|
3
3
|
/**
|
|
4
4
|
* Computes a triangulation of a polygon
|
|
5
5
|
* @param positions a flat array of the vertex positions that define the polygon.
|
|
@@ -10,4 +10,3 @@ import { Plane2D } from './polygon-utils';
|
|
|
10
10
|
* Adapted from https://github.com/mapbox/earcut
|
|
11
11
|
*/
|
|
12
12
|
export declare function earcut(positions: NumericArray, holeIndices?: NumericArray, dim?: number, areas?: NumericArray, plane?: Plane2D): number[];
|
|
13
|
-
//# sourceMappingURL=earcut.d.ts.map
|