geojson-polygon-self-intersections 1.2.1 → 2.0.0
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 +51 -30
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +210 -0
- package/package.json +48 -11
- package/index.js +0 -196
package/README.md
CHANGED
|
@@ -1,34 +1,52 @@
|
|
|
1
1
|
# geojson-polygon-self-intersections
|
|
2
2
|
|
|
3
|
-
A very simple
|
|
3
|
+
A very simple module to compute all self-intersections in coordinates of a polygon.
|
|
4
4
|
|
|
5
|
-
According to the [Simple Features standard](https://en.wikipedia.org/wiki/Simple_Features), polygons may not self-intersect. GeoJSON, however, doesn't care about this. You can use this tool to check for self-intersections
|
|
5
|
+
According to the [Simple Features standard](https://en.wikipedia.org/wiki/Simple_Features), polygons may not self-intersect. GeoJSON, however, doesn't care about this. You can use this tool to check for and/or retrieve self-intersections.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Installation
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
This package works in browsers and in Node.js as an ESM module.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Install Node.js or any compatible JavaScript runtime, then install this package with your favorite package manager.
|
|
12
12
|
|
|
13
|
-
```
|
|
13
|
+
```sh
|
|
14
14
|
npm install geojson-polygon-self-intersections
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
You can optionally build this package locally by running:
|
|
18
18
|
|
|
19
|
-
```
|
|
20
|
-
|
|
19
|
+
```sh
|
|
20
|
+
npm run build
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import gpsi from 'geojson-polygon-self-intersections'
|
|
21
27
|
|
|
22
|
-
//
|
|
28
|
+
// feature = {type: "Feature", geometry: {type: "Polygon", coordinates: [[[1, 10], [11, 13], ...]]}}
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
const isects = gpsi(feature.geometry.coordinates);
|
|
25
31
|
|
|
26
|
-
// isects =
|
|
32
|
+
// isects = [[5, 8], [7, 3], ...]
|
|
27
33
|
```
|
|
28
34
|
|
|
29
|
-
Where `
|
|
35
|
+
Where `feature` is a GeoJSON Feature of Polygon *geometry*, who's coordinates are an Array of Array of Positions, where each position has at least two coordinates. Only the first two coordinates (typically *X* and *Y* or *lon* and *lat*) are taken into account.
|
|
36
|
+
|
|
37
|
+
### Options
|
|
38
|
+
|
|
39
|
+
The following options can be passed as a second argument to control the behaviour of the algorithm.
|
|
40
|
+
|
|
41
|
+
| Option | Description |
|
|
42
|
+
|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
43
|
+
| `useSpatialIndex` | Whether a spatial index should be used to filter for possible intersections. Default: `true` |
|
|
44
|
+
| `reportVertexOnVertex` | If the same vertex (or almost the same vertex) appears more than once in the input, should this be reported as an intersection? Default: `false` |
|
|
45
|
+
| `reportVertexOnEdge` | If a vertex lies (almost) exactly on an edge segment, should this be reported as an intersection? Default: `false` |
|
|
46
|
+
| `epsilon` | It is almost never a good idea to compare floating point numbers for identity. Therefor, if we say "the same vertex" or "exactly on an edge segment", we need to define how "close" is "close enough". Note that the value is *not* used as an euclidian distance but always relative to the length of some edge segment. Default: `0` |
|
|
47
|
+
| `callbackFunction` | A callback function can be specified to process the output. See below. Default: `{ isect } => isect` |
|
|
30
48
|
|
|
31
|
-
|
|
49
|
+
For the callback function, you have access to the following data per intersection point:
|
|
32
50
|
|
|
33
51
|
- [x,y] intersection coordinates: `isect`
|
|
34
52
|
- ring index of the first edge: `ring0`
|
|
@@ -39,25 +57,28 @@ Alternatively, you can use a filter function to specify the output. You have acc
|
|
|
39
57
|
- idem for the second edge: `ring1`, `edge1`, `start1`, `end1`, `frac1`
|
|
40
58
|
- boolean indicating if the intersection is unique: `unique`
|
|
41
59
|
|
|
42
|
-
Finally, you can pass an option object to control the behaviour of the algorithm.
|
|
43
|
-
The following options are supported:
|
|
44
|
-
|
|
45
|
-
|Option|Description|
|
|
46
|
-
|------|-----------|
|
|
47
|
-
| `useSpatialIndex` | Whether a spatial index should be used to filter for possible intersections. Default: `true` |
|
|
48
|
-
| `reportVertexOnVertex` | If the same vertex (or almost the same vertex) appears more than once in the input, should this be reported as an intersection? Default: `false`|
|
|
49
|
-
| `reportVertexOnEdge` | If a vertex lies (almost) exactly on an edge segment, should this be reported as an intersection? Default: `false` |
|
|
50
|
-
| `epsilon` | It is almost never a good idea to compare floating point numbers for identity. Therefor, if we say "the same vertex" or "exactly on an edge segment", we need to define how "close" is "close enough". Note that the value is *not* used as an euclidian distance but always relative to the length of some edge segment. Default: `0`|
|
|
51
60
|
|
|
52
61
|
Together, this may look like so:
|
|
53
62
|
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
useSpatialIndex:false
|
|
57
|
-
}
|
|
58
|
-
|
|
63
|
+
```js
|
|
64
|
+
const options = {
|
|
65
|
+
useSpatialIndex: false,
|
|
66
|
+
callbackFunction: ({ isect, frac0, frac1 }) => { return [isect, frac0, frac1] }
|
|
67
|
+
}
|
|
68
|
+
const isects = gpsi(feature.geometry.coordinates, options);
|
|
59
69
|
|
|
60
70
|
// isects = [[[5, 8], 0.4856, 0.1865]], [[[7, 3], 0.3985, 0.9658]], ...]
|
|
61
71
|
```
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
|
|
73
|
+
## Speed
|
|
74
|
+
|
|
75
|
+
This tool uses the [rbush](https://github.com/mourner/rbush) spatial index by default to speed up the detection of intersections. This is especially useful when are many edges but only few intersections. If you prefer, you can opt-out using an options parameter (see below) and it will perform a brute-force search for intersections instead. This might be preferable in case of few edges, as it allows you to avoid some overhead.
|
|
76
|
+
|
|
77
|
+
## Changelog
|
|
78
|
+
|
|
79
|
+
### 2.0.0 - 2025-12-22
|
|
80
|
+
|
|
81
|
+
- This is now an ESM module written in TypeScript
|
|
82
|
+
- Inputs and outputs are now bare polygon coordinates and no longer GeoJSON Features with a polygon geometry. The package name is kept for continuity, and because it relates to GeoJSON not being as strict as the Simple Feature standard w.r.t. self-intersection.
|
|
83
|
+
- *Filter function* is now called *callback function* and part of the options
|
|
84
|
+
- Options must always be an object, only passing a boolean for `useSpatialIndex` is no longer supported
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Position } from 'geojson';
|
|
2
|
+
type CallbackFunctionInput = {
|
|
3
|
+
isect: Position;
|
|
4
|
+
ring0: number;
|
|
5
|
+
edge0: number;
|
|
6
|
+
start0: Position;
|
|
7
|
+
end0: Position;
|
|
8
|
+
frac0: number;
|
|
9
|
+
ring1: number;
|
|
10
|
+
edge1: number;
|
|
11
|
+
start1: Position;
|
|
12
|
+
end1: Position;
|
|
13
|
+
frac1: number;
|
|
14
|
+
unique: boolean;
|
|
15
|
+
};
|
|
16
|
+
type Options = {
|
|
17
|
+
useSpatialIndex: boolean;
|
|
18
|
+
epsilon: number;
|
|
19
|
+
reportVertexOnVertex: boolean;
|
|
20
|
+
reportVertexOnEdge: boolean;
|
|
21
|
+
callbackFunction: (callbackFunctionInput: CallbackFunctionInput) => any;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Return a polygon's self-intersections
|
|
25
|
+
*
|
|
26
|
+
* @export
|
|
27
|
+
* @param {Position[][]} polygon
|
|
28
|
+
* @param {?(Partial<Options>)} [partialOptions] Option
|
|
29
|
+
* @returns {any[]} Array of self-intersection points (or any type produced by the callback function)
|
|
30
|
+
*/
|
|
31
|
+
export default function gpsi(polygon: Position[][], partialOptions?: Partial<Options>): any[];
|
|
32
|
+
/**
|
|
33
|
+
* Compute where two lines intersect.
|
|
34
|
+
*
|
|
35
|
+
* From https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
|
|
36
|
+
*
|
|
37
|
+
* @param {Position} start0
|
|
38
|
+
* @param {Position} end0
|
|
39
|
+
* @param {Position} start1
|
|
40
|
+
* @param {Position} end1
|
|
41
|
+
* @returns {(Position | undefined)} The point of intersection, or undefined if no intersection
|
|
42
|
+
*/
|
|
43
|
+
export declare function intersect(start0: Position, end0: Position, start1: Position, end1: Position): Position | undefined;
|
|
44
|
+
export declare function equalArrays(array1: any[], array2: any[]): boolean;
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAGlC,KAAK,qBAAqB,GAAG;IAC3B,KAAK,EAAE,QAAQ,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,QAAQ,CAAA;IAChB,IAAI,EAAE,QAAQ,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,QAAQ,CAAA;IAChB,IAAI,EAAE,QAAQ,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,OAAO,CAAA;CAChB,CAAA;AACD,KAAK,OAAO,GAAG;IACb,eAAe,EAAE,OAAO,CAAA;IACxB,OAAO,EAAE,MAAM,CAAA;IACf,oBAAoB,EAAE,OAAO,CAAA;IAC7B,kBAAkB,EAAE,OAAO,CAAA;IAC3B,gBAAgB,EAAE,CAAC,qBAAqB,EAAE,qBAAqB,KAAK,GAAG,CAAA;CACxE,CAAA;AAkBD;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAC1B,OAAO,EAAE,QAAQ,EAAE,EAAE,EACrB,cAAc,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAChC,GAAG,EAAE,CAsHP;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,QAAQ,EAChB,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,QAAQ,EAChB,IAAI,EAAE,QAAQ,GACb,QAAQ,GAAG,SAAS,CAyBtB;AAGD,wBAAgB,WAAW,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,CAkBjE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import RBush from 'rbush';
|
|
3
|
+
const defaultOptions = {
|
|
4
|
+
useSpatialIndex: true,
|
|
5
|
+
epsilon: 0,
|
|
6
|
+
reportVertexOnVertex: false,
|
|
7
|
+
reportVertexOnEdge: false,
|
|
8
|
+
callbackFunction: ({ isect }) => isect
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Return a polygon's self-intersections
|
|
12
|
+
*
|
|
13
|
+
* @export
|
|
14
|
+
* @param {Position[][]} polygon
|
|
15
|
+
* @param {?(Partial<Options>)} [partialOptions] Option
|
|
16
|
+
* @returns {any[]} Array of self-intersection points (or any type produced by the callback function)
|
|
17
|
+
*/
|
|
18
|
+
export default function gpsi(polygon, partialOptions) {
|
|
19
|
+
const options = mergeOptions(defaultOptions, partialOptions);
|
|
20
|
+
const output = [];
|
|
21
|
+
const seen = {};
|
|
22
|
+
let tree;
|
|
23
|
+
if (options.useSpatialIndex) {
|
|
24
|
+
const allEdgesAsRbushTreeItems = [];
|
|
25
|
+
for (let ring0 = 0; ring0 < polygon.length; ring0++) {
|
|
26
|
+
for (let edge0 = 0; edge0 < polygon[ring0].length - 1; edge0++) {
|
|
27
|
+
allEdgesAsRbushTreeItems.push(rbushTreeItem(polygon, ring0, edge0));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
tree = new RBush();
|
|
31
|
+
tree.load(allEdgesAsRbushTreeItems);
|
|
32
|
+
}
|
|
33
|
+
for (let ring0 = 0; ring0 < polygon.length; ring0++) {
|
|
34
|
+
for (let edge0 = 0; edge0 < polygon[ring0].length - 1; edge0++) {
|
|
35
|
+
if (options.useSpatialIndex) {
|
|
36
|
+
const bboxOverlaps = tree.search(rbushTreeItem(polygon, ring0, edge0));
|
|
37
|
+
bboxOverlaps.forEach(function (bboxIsect) {
|
|
38
|
+
const ring1 = bboxIsect.ring;
|
|
39
|
+
const edge1 = bboxIsect.edge;
|
|
40
|
+
ifIsectAddToOutput(ring0, edge0, ring1, edge1);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
for (let ring1 = 0; ring1 < polygon.length; ring1++) {
|
|
45
|
+
for (let edge1 = 0; edge1 < polygon[ring1].length - 1; edge1++) {
|
|
46
|
+
// TODO: speedup possible if only interested in unique: start last two loops at ring0 and edge0+1
|
|
47
|
+
ifIsectAddToOutput(ring0, edge0, ring1, edge1);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Check if two edges intersect and add the intersection to the output
|
|
54
|
+
function ifIsectAddToOutput(ring0, edge0, ring1, edge1) {
|
|
55
|
+
const start0 = polygon[ring0][edge0];
|
|
56
|
+
const end0 = polygon[ring0][edge0 + 1];
|
|
57
|
+
const start1 = polygon[ring1][edge1];
|
|
58
|
+
const end1 = polygon[ring1][edge1 + 1];
|
|
59
|
+
const isect = intersect(start0, end0, start1, end1);
|
|
60
|
+
if (isect == undefined)
|
|
61
|
+
return; // discard parallels and coincidence
|
|
62
|
+
let frac0;
|
|
63
|
+
let frac1;
|
|
64
|
+
if (end0[0] != start0[0]) {
|
|
65
|
+
frac0 = (isect[0] - start0[0]) / (end0[0] - start0[0]);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
frac0 = (isect[1] - start0[1]) / (end0[1] - start0[1]);
|
|
69
|
+
}
|
|
70
|
+
if (end1[0] != start1[0]) {
|
|
71
|
+
frac1 = (isect[0] - start1[0]) / (end1[0] - start1[0]);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
frac1 = (isect[1] - start1[1]) / (end1[1] - start1[1]);
|
|
75
|
+
}
|
|
76
|
+
// There are roughly three cases we need to deal with.
|
|
77
|
+
// 1. If at least one of the fracs lies outside [0,1], there is no intersection.
|
|
78
|
+
if (isOutside(frac0, options.epsilon) ||
|
|
79
|
+
isOutside(frac1, options.epsilon)) {
|
|
80
|
+
return; // require segment intersection
|
|
81
|
+
}
|
|
82
|
+
// 2. If both are either exactly 0 or exactly 1, this is not an intersection but just
|
|
83
|
+
// two edge segments sharing a common vertex.
|
|
84
|
+
if (isBoundaryCase(frac0, options.epsilon) &&
|
|
85
|
+
isBoundaryCase(frac1, options.epsilon)) {
|
|
86
|
+
if (!options.reportVertexOnVertex)
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
// 3. If only one of the fractions is exactly 0 or 1, this is
|
|
90
|
+
// a vertex-on-edge situation.
|
|
91
|
+
if (isBoundaryCase(frac0, options.epsilon) ||
|
|
92
|
+
isBoundaryCase(frac1, options.epsilon)) {
|
|
93
|
+
if (!options.reportVertexOnEdge)
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const key = isect;
|
|
97
|
+
const unique = !seen[String(key)];
|
|
98
|
+
if (unique) {
|
|
99
|
+
seen[String(key)] = true;
|
|
100
|
+
}
|
|
101
|
+
output.push(options.callbackFunction({
|
|
102
|
+
isect,
|
|
103
|
+
ring0,
|
|
104
|
+
edge0,
|
|
105
|
+
start0,
|
|
106
|
+
end0,
|
|
107
|
+
frac0,
|
|
108
|
+
ring1,
|
|
109
|
+
edge1,
|
|
110
|
+
start1,
|
|
111
|
+
end1,
|
|
112
|
+
frac1,
|
|
113
|
+
unique
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
return output;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Compute where two lines intersect.
|
|
120
|
+
*
|
|
121
|
+
* From https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
|
|
122
|
+
*
|
|
123
|
+
* @param {Position} start0
|
|
124
|
+
* @param {Position} end0
|
|
125
|
+
* @param {Position} start1
|
|
126
|
+
* @param {Position} end1
|
|
127
|
+
* @returns {(Position | undefined)} The point of intersection, or undefined if no intersection
|
|
128
|
+
*/
|
|
129
|
+
export function intersect(start0, end0, start1, end1) {
|
|
130
|
+
if (equalArrays(start0, start1) ||
|
|
131
|
+
equalArrays(start0, end1) ||
|
|
132
|
+
equalArrays(end0, start1) ||
|
|
133
|
+
equalArrays(end1, start1)) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
const x0 = start0[0], y0 = start0[1], x1 = end0[0], y1 = end0[1], x2 = start1[0], y2 = start1[1], x3 = end1[0], y3 = end1[1];
|
|
137
|
+
const denom = (x0 - x1) * (y2 - y3) - (y0 - y1) * (x2 - x3);
|
|
138
|
+
if (denom == 0)
|
|
139
|
+
return undefined;
|
|
140
|
+
const x4 = ((x0 * y1 - y0 * x1) * (x2 - x3) - (x0 - x1) * (x2 * y3 - y2 * x3)) / denom;
|
|
141
|
+
const y4 = ((x0 * y1 - y0 * x1) * (y2 - y3) - (y0 - y1) * (x2 * y3 - y2 * x3)) / denom;
|
|
142
|
+
return [x4, y4];
|
|
143
|
+
}
|
|
144
|
+
// Check if arrays are equal.
|
|
145
|
+
export function equalArrays(array1, array2) {
|
|
146
|
+
// If the other array is a falsy value, return
|
|
147
|
+
if (!array1 || !array2)
|
|
148
|
+
return false;
|
|
149
|
+
// Compare lengths - can save a lot of time
|
|
150
|
+
if (array1.length != array2.length)
|
|
151
|
+
return false;
|
|
152
|
+
for (let i = 0, l = array1.length; i < l; i++) {
|
|
153
|
+
// Check if we have nested arrays
|
|
154
|
+
if (array1[i] instanceof Array && array2[i] instanceof Array) {
|
|
155
|
+
// Recurse into the nested arrays
|
|
156
|
+
if (!equalArrays(array1[i], array2[i]))
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
else if (array1[i] != array2[i]) {
|
|
160
|
+
// Warning - two different object instances will never be equal: {x:20} != {x:20}
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
// Check if boundary case: true if frac is (almost) 1.0 or 0.0
|
|
167
|
+
function isBoundaryCase(frac, epsilon) {
|
|
168
|
+
const e2 = epsilon * epsilon;
|
|
169
|
+
return e2 >= (frac - 1) * (frac - 1) || e2 >= frac * frac;
|
|
170
|
+
}
|
|
171
|
+
// Check if outside
|
|
172
|
+
function isOutside(frac, epsilon) {
|
|
173
|
+
return frac < 0 - epsilon || frac > 1 + epsilon;
|
|
174
|
+
}
|
|
175
|
+
// Return a rbush tree item given an ring and edge number
|
|
176
|
+
function rbushTreeItem(polygon, ring, edge) {
|
|
177
|
+
const start = polygon[ring][edge];
|
|
178
|
+
const end = polygon[ring][edge + 1];
|
|
179
|
+
let minX;
|
|
180
|
+
let maxX;
|
|
181
|
+
let minY;
|
|
182
|
+
let maxY;
|
|
183
|
+
if (start[0] < end[0]) {
|
|
184
|
+
minX = start[0];
|
|
185
|
+
maxX = end[0];
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
minX = end[0];
|
|
189
|
+
maxX = start[0];
|
|
190
|
+
}
|
|
191
|
+
if (start[1] < end[1]) {
|
|
192
|
+
minY = start[1];
|
|
193
|
+
maxY = end[1];
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
minY = end[1];
|
|
197
|
+
maxY = start[1];
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
minX: minX,
|
|
201
|
+
minY: minY,
|
|
202
|
+
maxX: maxX,
|
|
203
|
+
maxY: maxY,
|
|
204
|
+
ring: ring,
|
|
205
|
+
edge: edge
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function mergeOptions(option0, options1) {
|
|
209
|
+
return { ...option0, ...options1 };
|
|
210
|
+
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "geojson-polygon-self-intersections",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Manuel Claeys Bouuaert",
|
|
6
|
+
"email": "manuel.claeys.b@gmail.com",
|
|
7
|
+
"url": "https://manuelclaeysbouuaert.be"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
5
25
|
"repository": {
|
|
6
26
|
"type": "git",
|
|
7
27
|
"url": "git+https://github.com/mclaeysb/geojson-polygon-self-intersections.git"
|
|
8
28
|
},
|
|
29
|
+
"homepage": "https://github.com/mclaeysb/geojson-polygon-self-intersections#readme",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/mclaeysb/geojson-polygon-self-intersections/issues"
|
|
32
|
+
},
|
|
33
|
+
"description": "Find self-intersections in geojson polygon (possibly with interior rings)",
|
|
9
34
|
"keywords": [
|
|
10
35
|
"polygon",
|
|
11
36
|
"complex polygon",
|
|
12
37
|
"simple polygon",
|
|
13
38
|
"self-intersection"
|
|
14
39
|
],
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
40
|
+
"scripts": {
|
|
41
|
+
"watch": "tsc --watch",
|
|
42
|
+
"build": "tsc",
|
|
43
|
+
"test": "NODE_ENV=test vitest run",
|
|
44
|
+
"lint": "prettier --check src test && eslint src test --ext .js,.ts",
|
|
45
|
+
"format": "prettier --write src test",
|
|
46
|
+
"types": "tsc --noEmit"
|
|
21
47
|
},
|
|
22
48
|
"dependencies": {
|
|
23
|
-
"rbush": "
|
|
49
|
+
"rbush": "4.0.1"
|
|
24
50
|
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@eslint/eslintrc": "3.3.3",
|
|
53
|
+
"@eslint/js": "9.39.2",
|
|
54
|
+
"@types/geojson": "7946.0.16",
|
|
55
|
+
"@types/rbush": "4.0.0",
|
|
56
|
+
"eslint": "9.39.2",
|
|
57
|
+
"eslint-config-prettier": "10.1.8",
|
|
58
|
+
"globals": "16.5.0",
|
|
59
|
+
"prettier": "3.7.4",
|
|
60
|
+
"typescript": "5.9.3",
|
|
61
|
+
"typescript-eslint": "8.50.0",
|
|
62
|
+
"vitest": "4.0.16"
|
|
63
|
+
}
|
|
27
64
|
}
|
package/index.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
// Find self-intersections in geojson polygon (possibly with interior rings)
|
|
2
|
-
var rbush = require('rbush');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var merge = function(){
|
|
6
|
-
var output = {};
|
|
7
|
-
Array.prototype.slice.call(arguments).forEach(function(arg){
|
|
8
|
-
if(arg){
|
|
9
|
-
Object.keys(arg).forEach(function(key){
|
|
10
|
-
output[key]=arg[key];
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
return output;
|
|
15
|
-
};
|
|
16
|
-
var defaults = {
|
|
17
|
-
useSpatialIndex: true,
|
|
18
|
-
epsilon: 0,
|
|
19
|
-
reportVertexOnVertex: false,
|
|
20
|
-
reportVertexOnEdge: false
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
module.exports = function(feature, filterFn, options0) {
|
|
24
|
-
var options;
|
|
25
|
-
if("object" === typeof options0){
|
|
26
|
-
options = merge(defaults,options0);
|
|
27
|
-
} else {
|
|
28
|
-
options = merge(defaults,{useSpatialIndex:options0});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (feature.geometry.type != "Polygon") throw new Error("The input feature must be a Polygon");
|
|
32
|
-
|
|
33
|
-
var coord = feature.geometry.coordinates;
|
|
34
|
-
|
|
35
|
-
var output = [];
|
|
36
|
-
var seen = {};
|
|
37
|
-
|
|
38
|
-
if (options.useSpatialIndex) {
|
|
39
|
-
var allEdgesAsRbushTreeItems = [];
|
|
40
|
-
for (var ring0 = 0; ring0 < coord.length; ring0++) {
|
|
41
|
-
for (var edge0 = 0; edge0 < coord[ring0].length-1; edge0++) {
|
|
42
|
-
allEdgesAsRbushTreeItems.push(rbushTreeItem(ring0, edge0))
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
var tree = rbush();
|
|
46
|
-
tree.load(allEdgesAsRbushTreeItems);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
for (var ring0 = 0; ring0 < coord.length; ring0++) {
|
|
50
|
-
for (var edge0 = 0; edge0 < coord[ring0].length-1; edge0++) {
|
|
51
|
-
if (options.useSpatialIndex) {
|
|
52
|
-
var bboxOverlaps = tree.search(rbushTreeItem(ring0, edge0));
|
|
53
|
-
bboxOverlaps.forEach(function(bboxIsect) {
|
|
54
|
-
var ring1 = bboxIsect.ring;
|
|
55
|
-
var edge1 = bboxIsect.edge;
|
|
56
|
-
ifIsectAddToOutput(ring0, edge0, ring1, edge1);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
for (var ring1 = 0; ring1 < coord.length; ring1++) {
|
|
61
|
-
for (var edge1 = 0 ; edge1 < coord[ring1].length-1; edge1++) {
|
|
62
|
-
// TODO: speedup possible if only interested in unique: start last two loops at ring0 and edge0+1
|
|
63
|
-
ifIsectAddToOutput(ring0, edge0, ring1, edge1);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (!filterFn) output = {type: "Feature", geometry: {type: "MultiPoint", coordinates: output}};
|
|
71
|
-
return output;
|
|
72
|
-
|
|
73
|
-
// true if frac is (almost) 1.0 or 0.0
|
|
74
|
-
function isBoundaryCase(frac){
|
|
75
|
-
var e2 = options.epsilon * options.epsilon;
|
|
76
|
-
return e2 >= (frac-1)*(frac-1) || e2 >= frac*frac;
|
|
77
|
-
}
|
|
78
|
-
function isOutside(frac){
|
|
79
|
-
return frac < 0 - options.epsilon || frac > 1 + options.epsilon;
|
|
80
|
-
}
|
|
81
|
-
// Function to check if two edges intersect and add the intersection to the output
|
|
82
|
-
function ifIsectAddToOutput(ring0, edge0, ring1, edge1) {
|
|
83
|
-
var start0 = coord[ring0][edge0];
|
|
84
|
-
var end0 = coord[ring0][edge0+1];
|
|
85
|
-
var start1 = coord[ring1][edge1];
|
|
86
|
-
var end1 = coord[ring1][edge1+1];
|
|
87
|
-
|
|
88
|
-
var isect = intersect(start0, end0, start1, end1);
|
|
89
|
-
|
|
90
|
-
if (isect == null) return; // discard parallels and coincidence
|
|
91
|
-
frac0, frac1;
|
|
92
|
-
if (end0[0] != start0[0]) {
|
|
93
|
-
var frac0 = (isect[0]-start0[0])/(end0[0]-start0[0]);
|
|
94
|
-
} else {
|
|
95
|
-
var frac0 = (isect[1]-start0[1])/(end0[1]-start0[1]);
|
|
96
|
-
};
|
|
97
|
-
if (end1[0] != start1[0]) {
|
|
98
|
-
var frac1 = (isect[0]-start1[0])/(end1[0]-start1[0]);
|
|
99
|
-
} else {
|
|
100
|
-
var frac1 = (isect[1]-start1[1])/(end1[1]-start1[1]);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// There are roughly three cases we need to deal with.
|
|
104
|
-
// 1. If at least one of the fracs lies outside [0,1], there is no intersection.
|
|
105
|
-
if (isOutside(frac0) || isOutside(frac1)) {
|
|
106
|
-
return; // require segment intersection
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// 2. If both are either exactly 0 or exactly 1, this is not an intersection but just
|
|
110
|
-
// two edge segments sharing a common vertex.
|
|
111
|
-
if (isBoundaryCase(frac0) && isBoundaryCase(frac1)){
|
|
112
|
-
if(! options.reportVertexOnVertex) return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 3. If only one of the fractions is exactly 0 or 1, this is
|
|
116
|
-
// a vertex-on-edge situation.
|
|
117
|
-
if (isBoundaryCase(frac0) || isBoundaryCase(frac1)){
|
|
118
|
-
if(! options.reportVertexOnEdge) return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
var key = isect;
|
|
122
|
-
var unique = !seen[key];
|
|
123
|
-
if (unique) {
|
|
124
|
-
seen[key] = true;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (filterFn) {
|
|
128
|
-
output.push(filterFn(isect, ring0, edge0, start0, end0, frac0, ring1, edge1, start1, end1, frac1, unique));
|
|
129
|
-
} else {
|
|
130
|
-
output.push(isect);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Function to return a rbush tree item given an ring and edge number
|
|
135
|
-
function rbushTreeItem(ring, edge) {
|
|
136
|
-
|
|
137
|
-
var start = coord[ring][edge];
|
|
138
|
-
var end = coord[ring][edge+1];
|
|
139
|
-
|
|
140
|
-
if (start[0] < end[0]) {
|
|
141
|
-
var minX = start[0], maxX = end[0];
|
|
142
|
-
} else {
|
|
143
|
-
var minX = end[0], maxX = start[0];
|
|
144
|
-
};
|
|
145
|
-
if (start[1] < end[1]) {
|
|
146
|
-
var minY = start[1], maxY = end[1];
|
|
147
|
-
} else {
|
|
148
|
-
var minY = end[1], maxY = start[1];
|
|
149
|
-
}
|
|
150
|
-
return {minX: minX, minY: minY, maxX: maxX, maxY: maxY, ring: ring, edge: edge};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Function to compute where two lines (not segments) intersect. From https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
|
|
156
|
-
function intersect(start0, end0, start1, end1) {
|
|
157
|
-
if (equalArrays(start0,start1) || equalArrays(start0,end1) || equalArrays(end0,start1) || equalArrays(end1,start1)) return null;
|
|
158
|
-
var x0 = start0[0],
|
|
159
|
-
y0 = start0[1],
|
|
160
|
-
x1 = end0[0],
|
|
161
|
-
y1 = end0[1],
|
|
162
|
-
x2 = start1[0],
|
|
163
|
-
y2 = start1[1],
|
|
164
|
-
x3 = end1[0],
|
|
165
|
-
y3 = end1[1];
|
|
166
|
-
var denom = (x0 - x1) * (y2 - y3) - (y0 - y1) * (x2 - x3);
|
|
167
|
-
if (denom == 0) return null;
|
|
168
|
-
var x4 = ((x0 * y1 - y0 * x1) * (x2 - x3) - (x0 - x1) * (x2 * y3 - y2 * x3)) / denom;
|
|
169
|
-
var y4 = ((x0 * y1 - y0 * x1) * (y2 - y3) - (y0 - y1) * (x2 * y3 - y2 * x3)) / denom;
|
|
170
|
-
return [x4, y4];
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Function to compare Arrays of numbers. From http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript
|
|
174
|
-
function equalArrays(array1, array2) {
|
|
175
|
-
// if the other array is a falsy value, return
|
|
176
|
-
if (!array1 || !array2)
|
|
177
|
-
return false;
|
|
178
|
-
|
|
179
|
-
// compare lengths - can save a lot of time
|
|
180
|
-
if (array1.length != array2.length)
|
|
181
|
-
return false;
|
|
182
|
-
|
|
183
|
-
for (var i = 0, l=array1.length; i < l; i++) {
|
|
184
|
-
// Check if we have nested arrays
|
|
185
|
-
if (array1[i] instanceof Array && array2[i] instanceof Array) {
|
|
186
|
-
// recurse into the nested arrays
|
|
187
|
-
if (!equalArrays(array1[i],array2[i]))
|
|
188
|
-
return false;
|
|
189
|
-
}
|
|
190
|
-
else if (array1[i] != array2[i]) {
|
|
191
|
-
// Warning - two different object instances will never be equal: {x:20} != {x:20}
|
|
192
|
-
return false;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return true;
|
|
196
|
-
}
|