Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions packages/turf-boolean-contains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Feature,
Geometry,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Expand Down Expand Up @@ -87,8 +88,18 @@ function booleanContains(
}
case "MultiPolygon":
switch (type2) {
case "Point":
return isPointInMultiPolygon(geom1, geom2);
case "MultiPoint":
return isMultiPointInMultiPolygon(geom1, geom2);
case "LineString":
return isLineInMultiPolygon(geom1, geom2);
case "MultiLineString":
return isMultiLineStringInMultiPolygon(geom1, geom2);
case "Polygon":
return isPolygonInMultiPolygon(geom1, geom2);
case "MultiPolygon":
return isMultiPolygonInMultiPolygon(geom1, geom2);
default:
throw new Error("feature2 " + type2 + " geometry not supported");
}
Expand All @@ -103,6 +114,122 @@ function isPolygonInMultiPolygon(multiPolygon: MultiPolygon, polygon: Polygon) {
);
}

/**
* Is Point inside MultiPolygon
*
* @private
* @param {MultiPolygon} multiPolygon MultiPolygon geometry
* @param {Point} point Point geometry
* @returns {boolean} true if point is inside the interior of any polygon in the MultiPolygon
*/
function isPointInMultiPolygon(multiPolygon: MultiPolygon, point: Point) {
return multiPolygon.coordinates.some((coords) =>
booleanPointInPolygon(
point,
{ type: "Polygon", coordinates: coords },
{
ignoreBoundary: true,
}
)
);
}

/**
* Is MultiPoint inside MultiPolygon
*
* @private
* @param {MultiPolygon} multiPolygon MultiPolygon geometry
* @param {MultiPoint} multiPoint MultiPoint geometry
* @returns {boolean} true if every point is inside the interior of some polygon in the MultiPolygon
*/
function isMultiPointInMultiPolygon(
multiPolygon: MultiPolygon,
multiPoint: MultiPoint
) {
for (const coord of multiPoint.coordinates) {
const pointInside = multiPolygon.coordinates.some((polyCoords) =>
booleanPointInPolygon(
coord,
{ type: "Polygon", coordinates: polyCoords },
{ ignoreBoundary: true }
)
);
if (!pointInside) {
return false;
}
}
return true;
}

/**
* Is LineString inside MultiPolygon
*
* @private
* @param {MultiPolygon} multiPolygon MultiPolygon geometry
* @param {LineString} lineString LineString geometry
* @returns {boolean} true if the LineString is fully contained within a single polygon of the MultiPolygon
*/
function isLineInMultiPolygon(
multiPolygon: MultiPolygon,
lineString: LineString
) {
return multiPolygon.coordinates.some((coords) =>
isLineInPoly({ type: "Polygon", coordinates: coords }, lineString)
);
}

/**
* Is MultiLineString inside MultiPolygon
*
* @private
* @param {MultiPolygon} multiPolygon MultiPolygon geometry
* @param {MultiLineString} multiLineString MultiLineString geometry
* @returns {boolean} true if every LineString is fully contained within some single polygon of the MultiPolygon
*/
function isMultiLineStringInMultiPolygon(
multiPolygon: MultiPolygon,
multiLineString: MultiLineString
) {
for (const lineCoords of multiLineString.coordinates) {
const lineInside = multiPolygon.coordinates.some((polyCoords) =>
isLineInPoly(
{ type: "Polygon", coordinates: polyCoords },
{ type: "LineString", coordinates: lineCoords }
)
);
if (!lineInside) {
return false;
}
}
return true;
}

/**
* Is MultiPolygon inside MultiPolygon
*
* @private
* @param {MultiPolygon} multiPolygon1 MultiPolygon geometry (container)
* @param {MultiPolygon} multiPolygon2 MultiPolygon geometry (contained)
* @returns {boolean} true if every polygon of multiPolygon2 is fully contained within some single polygon of multiPolygon1
*/
function isMultiPolygonInMultiPolygon(
multiPolygon1: MultiPolygon,
multiPolygon2: MultiPolygon
) {
for (const poly2Coords of multiPolygon2.coordinates) {
const polyInside = multiPolygon1.coordinates.some((poly1Coords) =>
isPolyInPoly(
{ type: "Polygon", coordinates: poly1Coords },
{ type: "Polygon", coordinates: poly2Coords }
)
);
if (!polyInside) {
return false;
}
}
return true;
}

function isMultiPolyInPoly(polygon: Polygon, multiPolygon: MultiPolygon) {
return multiPolygon.coordinates.every((coords) =>
isPolyInPoly(polygon, { type: "Polygon", coordinates: coords })
Expand Down
3 changes: 2 additions & 1 deletion packages/turf-boolean-contains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"contributors": [
"Rowan Winsemius <@rowanwins>",
"Denis Carriere <@DenisCarriere>",
"Samuel Arbibe <@samuelarbibe>"
"Samuel Arbibe <@samuelarbibe>",
"Espen Hovlandsdal <@rexxars>"
],
"license": "MIT",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
],
[
[
[20, 0],
[30, 0],
[30, 10],
[20, 10],
[20, 0]
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[0, 2],
[0, 8]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
],
[
[
[20, 0],
[30, 0],
[30, 10],
[20, 10],
[20, 0]
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[12, 5],
[18, 5]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
],
[
[
[20, 0],
[30, 0],
[30, 10],
[20, 10],
[20, 0]
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[5, 5],
[25, 5]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
],
[
[
[20, 0],
[30, 0],
[30, 10],
[20, 10],
[20, 0]
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[2, 2],
[8, 8]
],
[
[12, 2],
[18, 8]
]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0]
]
],
[
[
[20, 0],
[30, 0],
[30, 10],
[20, 10],
[20, 0]
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[2, 2],
[8, 8]
],
[
[5, 5],
[25, 5]
]
]
}
}
]
}
Loading