diff --git a/packages/turf-boolean-contains/index.ts b/packages/turf-boolean-contains/index.ts index c49a0f1fd7..620a0db9d9 100644 --- a/packages/turf-boolean-contains/index.ts +++ b/packages/turf-boolean-contains/index.ts @@ -79,6 +79,8 @@ export default function booleanContains( return isPolyInPoly(geom1, geom2); case "MultiPoint": return isMultiPointInPoly(geom1, geom2); + case "MultiPolygon": + return isMultiPolyInPoly(geom1, geom2); default: throw new Error("feature2 " + type2 + " geometry not supported"); } @@ -103,6 +105,15 @@ export function isPolygonInMultiPolygon( ); } +export function isMultiPolyInPoly( + polygon: Polygon, + multiPolygon: MultiPolygon +) { + return multiPolygon.coordinates.every((coords) => + isPolyInPoly(polygon, { type: "Polygon", coordinates: coords }) + ); +} + export function isPointInMultiPoint(multiPoint: MultiPoint, pt: Point) { let i; let output = false; diff --git a/packages/turf-boolean-contains/test/false/MultiPolygon/Polygon/MultiPolygonIsNotContainedByPolygon.geojson b/packages/turf-boolean-contains/test/false/MultiPolygon/Polygon/MultiPolygonIsNotContainedByPolygon.geojson new file mode 100644 index 0000000000..332c246b8c --- /dev/null +++ b/packages/turf-boolean-contains/test/false/MultiPolygon/Polygon/MultiPolygonIsNotContainedByPolygon.geojson @@ -0,0 +1,48 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [5.697231218450497, 39.505906288651715], + [5.697231218450497, 40.50986523812119], + [4.30801420435526, 40.50986523812119], + [4.30801420435526, 39.505906288651715], + [5.697231218450497, 39.505906288651715] + ] + ] + } + }, + { + "type": "Feature", + "properties": null, + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [3.6968994140625, 39.65645604812829], + [4.622497558593749, 39.65645604812829], + [4.622497558593749, 40.168380093142446], + [3.6968994140625, 40.168380093142446], + [3.6968994140625, 39.65645604812829] + ] + ], + [ + [ + [4.73236083984375, 39.69450749856091], + [5.416259765624999, 39.69450749856091], + [5.416259765624999, 40.386304853509046], + [4.73236083984375, 40.386304853509046], + [4.73236083984375, 39.69450749856091] + ] + ] + ] + } + } + ] +} diff --git a/packages/turf-boolean-contains/test/true/MultiPolygon/Polygon/MultiPolygonIsContainedByPolygon.geojson b/packages/turf-boolean-contains/test/true/MultiPolygon/Polygon/MultiPolygonIsContainedByPolygon.geojson new file mode 100644 index 0000000000..eb8c9b1ff1 --- /dev/null +++ b/packages/turf-boolean-contains/test/true/MultiPolygon/Polygon/MultiPolygonIsContainedByPolygon.geojson @@ -0,0 +1,48 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [5.860668514226688, 40.60423664824944], + [3.248940527728166, 40.60423664824944], + [3.248940527728166, 39.35441976583036], + [5.860668514226688, 39.35441976583036], + [5.860668514226688, 40.60423664824944] + ] + ] + } + }, + { + "type": "Feature", + "properties": null, + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [3.6968994140625, 39.65645604812829], + [4.622497558593749, 39.65645604812829], + [4.622497558593749, 40.168380093142446], + [3.6968994140625, 40.168380093142446], + [3.6968994140625, 39.65645604812829] + ] + ], + [ + [ + [4.73236083984375, 39.69450749856091], + [5.416259765624999, 39.69450749856091], + [5.416259765624999, 40.386304853509046], + [4.73236083984375, 40.386304853509046], + [4.73236083984375, 39.69450749856091] + ] + ] + ] + } + } + ] +}