Skip to content

Commit 5c4b040

Browse files
MartinP-CMartin Powlesland-Cooksmallsaucepan
authored
@turf/boolean-contains - support for multipolygon inside polygon (#2357)
* add tests for multipolygon contained by polygon * check for is every multipolygon inside polygon --------- Co-authored-by: Martin Powlesland-Cook <mpowlesland-cook@denseair.net> Co-authored-by: James Beard <james@smallsaucepan.com>
1 parent f615482 commit 5c4b040

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

packages/turf-boolean-contains/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ function booleanContains(
8080
return isPolyInPoly(geom1, geom2);
8181
case "MultiPoint":
8282
return isMultiPointInPoly(geom1, geom2);
83+
case "MultiPolygon":
84+
return isMultiPolyInPoly(geom1, geom2);
8385
default:
8486
throw new Error("feature2 " + type2 + " geometry not supported");
8587
}
@@ -101,6 +103,12 @@ function isPolygonInMultiPolygon(multiPolygon: MultiPolygon, polygon: Polygon) {
101103
);
102104
}
103105

106+
function isMultiPolyInPoly(polygon: Polygon, multiPolygon: MultiPolygon) {
107+
return multiPolygon.coordinates.every((coords) =>
108+
isPolyInPoly(polygon, { type: "Polygon", coordinates: coords })
109+
);
110+
}
111+
104112
function isPointInMultiPoint(multiPoint: MultiPoint, pt: Point) {
105113
let i;
106114
let output = false;
@@ -321,6 +329,7 @@ export {
321329
isLineOnLine,
322330
isLineInPoly,
323331
isPolyInPoly,
332+
isMultiPolyInPoly,
324333
doBBoxOverlap,
325334
compareCoords,
326335
getMidpoint,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "Polygon",
9+
"coordinates": [
10+
[
11+
[5.697231218450497, 39.505906288651715],
12+
[5.697231218450497, 40.50986523812119],
13+
[4.30801420435526, 40.50986523812119],
14+
[4.30801420435526, 39.505906288651715],
15+
[5.697231218450497, 39.505906288651715]
16+
]
17+
]
18+
}
19+
},
20+
{
21+
"type": "Feature",
22+
"properties": null,
23+
"geometry": {
24+
"type": "MultiPolygon",
25+
"coordinates": [
26+
[
27+
[
28+
[3.6968994140625, 39.65645604812829],
29+
[4.622497558593749, 39.65645604812829],
30+
[4.622497558593749, 40.168380093142446],
31+
[3.6968994140625, 40.168380093142446],
32+
[3.6968994140625, 39.65645604812829]
33+
]
34+
],
35+
[
36+
[
37+
[4.73236083984375, 39.69450749856091],
38+
[5.416259765624999, 39.69450749856091],
39+
[5.416259765624999, 40.386304853509046],
40+
[4.73236083984375, 40.386304853509046],
41+
[4.73236083984375, 39.69450749856091]
42+
]
43+
]
44+
]
45+
}
46+
}
47+
]
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "Polygon",
9+
"coordinates": [
10+
[
11+
[5.860668514226688, 40.60423664824944],
12+
[3.248940527728166, 40.60423664824944],
13+
[3.248940527728166, 39.35441976583036],
14+
[5.860668514226688, 39.35441976583036],
15+
[5.860668514226688, 40.60423664824944]
16+
]
17+
]
18+
}
19+
},
20+
{
21+
"type": "Feature",
22+
"properties": null,
23+
"geometry": {
24+
"type": "MultiPolygon",
25+
"coordinates": [
26+
[
27+
[
28+
[3.6968994140625, 39.65645604812829],
29+
[4.622497558593749, 39.65645604812829],
30+
[4.622497558593749, 40.168380093142446],
31+
[3.6968994140625, 40.168380093142446],
32+
[3.6968994140625, 39.65645604812829]
33+
]
34+
],
35+
[
36+
[
37+
[4.73236083984375, 39.69450749856091],
38+
[5.416259765624999, 39.69450749856091],
39+
[5.416259765624999, 40.386304853509046],
40+
[4.73236083984375, 40.386304853509046],
41+
[4.73236083984375, 39.69450749856091]
42+
]
43+
]
44+
]
45+
}
46+
}
47+
]
48+
}

0 commit comments

Comments
 (0)