-
Notifications
You must be signed in to change notification settings - Fork 952
/
Copy pathindex.ts
291 lines (273 loc) · 7.53 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import {
BBox,
Feature,
Geometry,
LineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
} from "geojson";
import { bbox as calcBbox } from "@turf/bbox";
import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
import { booleanPointOnLine as isPointOnLine } from "@turf/boolean-point-on-line";
import { getGeom } from "@turf/invariant";
/**
* Boolean-contains returns True if the second geometry is completely contained by the first geometry.
* The interiors of both geometries must intersect and, the interior and boundary of the secondary (geometry b)
* must not intersect the exterior of the primary (geometry a).
* Boolean-contains returns the exact opposite result of the `@turf/boolean-within`.
*
* @function
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
* @returns {boolean} true/false
* @example
* var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
* var point = turf.point([1, 2]);
*
* turf.booleanContains(line, point);
* //=true
*/
function booleanContains(
feature1: Feature<any> | Geometry,
feature2: Feature<any> | Geometry
) {
const geom1 = getGeom(feature1);
const geom2 = getGeom(feature2);
const type1 = geom1.type;
const type2 = geom2.type;
const coords1 = geom1.coordinates;
const coords2 = geom2.coordinates;
switch (type1) {
case "Point":
switch (type2) {
case "Point":
return compareCoords(coords1, coords2);
default:
throw new Error("feature2 " + type2 + " geometry not supported");
}
case "MultiPoint":
switch (type2) {
case "Point":
return isPointInMultiPoint(geom1, geom2);
case "MultiPoint":
return isMultiPointInMultiPoint(geom1, geom2);
default:
throw new Error("feature2 " + type2 + " geometry not supported");
}
case "LineString":
switch (type2) {
case "Point":
return isPointOnLine(geom2, geom1, { ignoreEndVertices: true });
case "LineString":
return isLineOnLine(geom1, geom2);
case "MultiPoint":
return isMultiPointOnLine(geom1, geom2);
default:
throw new Error("feature2 " + type2 + " geometry not supported");
}
case "Polygon":
switch (type2) {
case "Point":
return booleanPointInPolygon(geom2, geom1, { ignoreBoundary: true });
case "LineString":
return isLineInPoly(geom1, geom2);
case "Polygon":
return isPolyInPoly(geom1, geom2);
case "MultiPoint":
return isMultiPointInPoly(geom1, geom2);
default:
throw new Error("feature2 " + type2 + " geometry not supported");
}
case "MultiPolygon":
switch (type2) {
case "Polygon":
return isPolygonInMultiPolygon(geom1, geom2);
default:
throw new Error("feature2 " + type2 + " geometry not supported");
}
default:
throw new Error("feature1 " + type1 + " geometry not supported");
}
}
function isPolygonInMultiPolygon(multiPolygon: MultiPolygon, polygon: Polygon) {
return multiPolygon.coordinates.some((coords) =>
isPolyInPoly({ type: "Polygon", coordinates: coords }, polygon)
);
}
function isPointInMultiPoint(multiPoint: MultiPoint, pt: Point) {
let i;
let output = false;
for (i = 0; i < multiPoint.coordinates.length; i++) {
if (compareCoords(multiPoint.coordinates[i], pt.coordinates)) {
output = true;
break;
}
}
return output;
}
function isMultiPointInMultiPoint(
multiPoint1: MultiPoint,
multiPoint2: MultiPoint
) {
for (const coord2 of multiPoint2.coordinates) {
let matchFound = false;
for (const coord1 of multiPoint1.coordinates) {
if (compareCoords(coord2, coord1)) {
matchFound = true;
break;
}
}
if (!matchFound) {
return false;
}
}
return true;
}
function isMultiPointOnLine(lineString: LineString, multiPoint: MultiPoint) {
let haveFoundInteriorPoint = false;
for (const coord of multiPoint.coordinates) {
if (isPointOnLine(coord, lineString, { ignoreEndVertices: true })) {
haveFoundInteriorPoint = true;
}
if (!isPointOnLine(coord, lineString)) {
return false;
}
}
if (haveFoundInteriorPoint) {
return true;
}
return false;
}
function isMultiPointInPoly(polygon: Polygon, multiPoint: MultiPoint) {
for (const coord of multiPoint.coordinates) {
if (!booleanPointInPolygon(coord, polygon, { ignoreBoundary: true })) {
return false;
}
}
return true;
}
function isLineOnLine(lineString1: LineString, lineString2: LineString) {
let haveFoundInteriorPoint = false;
for (const coords of lineString2.coordinates) {
if (
isPointOnLine({ type: "Point", coordinates: coords }, lineString1, {
ignoreEndVertices: true,
})
) {
haveFoundInteriorPoint = true;
}
if (
!isPointOnLine({ type: "Point", coordinates: coords }, lineString1, {
ignoreEndVertices: false,
})
) {
return false;
}
}
return haveFoundInteriorPoint;
}
function isLineInPoly(polygon: Polygon, linestring: LineString) {
let output = false;
let i = 0;
const polyBbox = calcBbox(polygon);
const lineBbox = calcBbox(linestring);
if (!doBBoxOverlap(polyBbox, lineBbox)) {
return false;
}
for (i; i < linestring.coordinates.length - 1; i++) {
const midPoint = getMidpoint(
linestring.coordinates[i],
linestring.coordinates[i + 1]
);
if (
booleanPointInPolygon({ type: "Point", coordinates: midPoint }, polygon, {
ignoreBoundary: true,
})
) {
output = true;
break;
}
}
return output;
}
/**
* Is Polygon2 in Polygon1
* Only takes into account outer rings
*
* @private
* @param {Geometry|Feature<Polygon>} feature1 Polygon1
* @param {Geometry|Feature<Polygon>} feature2 Polygon2
* @returns {boolean} true/false
*/
function isPolyInPoly(
feature1: Feature<Polygon> | Polygon,
feature2: Feature<Polygon> | Polygon
) {
// Handle Nulls
if (feature1.type === "Feature" && feature1.geometry === null) {
return false;
}
if (feature2.type === "Feature" && feature2.geometry === null) {
return false;
}
const poly1Bbox = calcBbox(feature1);
const poly2Bbox = calcBbox(feature2);
if (!doBBoxOverlap(poly1Bbox, poly2Bbox)) {
return false;
}
const coords = getGeom(feature2).coordinates;
for (const ring of coords) {
for (const coord of ring) {
if (!booleanPointInPolygon(coord, feature1)) {
return false;
}
}
}
return true;
}
function doBBoxOverlap(bbox1: BBox, bbox2: BBox) {
if (bbox1[0] > bbox2[0]) {
return false;
}
if (bbox1[2] < bbox2[2]) {
return false;
}
if (bbox1[1] > bbox2[1]) {
return false;
}
if (bbox1[3] < bbox2[3]) {
return false;
}
return true;
}
/**
* compareCoords
*
* @private
* @param {Position} pair1 point [x,y]
* @param {Position} pair2 point [x,y]
* @returns {boolean} true/false if coord pairs match
*/
function compareCoords(pair1: number[], pair2: number[]) {
return pair1[0] === pair2[0] && pair1[1] === pair2[1];
}
function getMidpoint(pair1: number[], pair2: number[]) {
return [(pair1[0] + pair2[0]) / 2, (pair1[1] + pair2[1]) / 2];
}
export {
booleanContains,
isPolygonInMultiPolygon,
isPointInMultiPoint,
isMultiPointInMultiPoint,
isMultiPointOnLine,
isMultiPointInPoly,
isLineOnLine,
isLineInPoly,
isPolyInPoly,
doBBoxOverlap,
compareCoords,
getMidpoint,
};
export default booleanContains;