Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion packages/turf-boolean-contains/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ $ npm install @turf/turf

### Diagrams

![esri-contains](diagrams/esri-contains.gif)
![esri-contains](diagrams/esri-contains.png)
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 27 additions & 13 deletions packages/turf-boolean-contains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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";
import { feature } from "@turf/helpers";
import { lineSplit } from "@turf/line-split";

/**
* Boolean-contains returns True if the second geometry is completely contained by the first geometry.
Expand Down Expand Up @@ -178,29 +180,41 @@ function isLineOnLine(lineString1: LineString, lineString2: LineString) {
}

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]

for (const coord of linestring.coordinates) {
if (!booleanPointInPolygon(coord, polygon)) {
return false;
}
}

let isContainedByPolygonBoundary = false;
const lineSegments = lineSplit(feature(linestring), feature(polygon));

for (const lineSegment of lineSegments.features) {
const midpoint = getMidpoint(
lineSegment.geometry.coordinates[0],
lineSegment.geometry.coordinates[1]
);

if (!booleanPointInPolygon(midpoint, polygon)) {
return false;
}

if (
booleanPointInPolygon({ type: "Point", coordinates: midPoint }, polygon, {
ignoreBoundary: true,
})
!isContainedByPolygonBoundary &&
booleanPointInPolygon(midpoint, polygon, { ignoreBoundary: true })
) {
output = true;
break;
isContainedByPolygonBoundary = true;
}
}
return output;

return isContainedByPolygonBoundary;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/turf-boolean-contains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"author": "Turf Authors",
"contributors": [
"Rowan Winsemius <@rowanwins>",
"Denis Carriere <@DenisCarriere>"
"Denis Carriere <@DenisCarriere>",
"Samuel Arbibe <@samuelarbibe>"
],
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -73,6 +74,7 @@
"@turf/boolean-point-on-line": "workspace:*",
"@turf/helpers": "workspace:*",
"@turf/invariant": "workspace:*",
"@turf/line-split": "workspace:*",
"@types/geojson": "^7946.0.10",
"tslib": "^2.8.1"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[9.6459207, -1.2281524],
[11.0822393, -1.2461022],
[11.405411, -0.1869151],
[11.1181473, 1.9133448],
[10.0588623, 1.4646914],
[10.6692977, -0.0253296],
[10.6692977, -0.0253296],
[9.6459207, -1.2281524]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[10.2166256, -0.7379163],
[11.0362904, 1.794324]
]
}
}
]
}
5 changes: 5 additions & 0 deletions packages/turf-boolean-within/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ Or install the all-encompassing @turf/turf module that includes all modules as f
```sh
$ npm install @turf/turf
```


### Diagrams

![esri-within](diagrams/esri-within.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed packages/turf-boolean-within/digrams/esri-within.gif
Binary file not shown.
48 changes: 28 additions & 20 deletions packages/turf-boolean-within/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { bbox as calcBbox } from "@turf/bbox";
import { booleanPointOnLine } from "@turf/boolean-point-on-line";
import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
import { getGeom } from "@turf/invariant";
import { feature } from "@turf/helpers";
import { lineSplit } from "@turf/line-split";

/**
* Boolean-within returns true if the first geometry is completely within the second geometry.
Expand Down Expand Up @@ -166,35 +168,41 @@ function isLineOnLine(lineString1: LineString, lineString2: LineString) {
}

function isLineInPoly(linestring: LineString, polygon: Polygon) {
var polyBbox = calcBbox(polygon);
var lineBbox = calcBbox(linestring);
const polyBbox = calcBbox(polygon);
const lineBbox = calcBbox(linestring);

if (!doBBoxOverlap(polyBbox, lineBbox)) {
return false;
}
var foundInsidePoint = false;

for (var i = 0; i < linestring.coordinates.length; i++) {
if (!booleanPointInPolygon(linestring.coordinates[i], polygon)) {
for (const coord of linestring.coordinates) {
if (!booleanPointInPolygon(coord, polygon)) {
return false;
}
if (!foundInsidePoint) {
foundInsidePoint = booleanPointInPolygon(
linestring.coordinates[i],
polygon,
{ ignoreBoundary: true }
);
}

let isWithinPolygonBoundary = false;
const lineSegments = lineSplit(feature(linestring), feature(polygon));

for (const lineSegment of lineSegments.features) {
const midpoint = getMidpoint(
lineSegment.geometry.coordinates[0],
lineSegment.geometry.coordinates[1]
);

if (!booleanPointInPolygon(midpoint, polygon)) {
return false;
}
if (!foundInsidePoint && i < linestring.coordinates.length - 1) {
var midpoint = getMidpoint(
linestring.coordinates[i],
linestring.coordinates[i + 1]
);
foundInsidePoint = booleanPointInPolygon(midpoint, polygon, {
ignoreBoundary: true,
});

if (
!isWithinPolygonBoundary &&
booleanPointInPolygon(midpoint, polygon, { ignoreBoundary: true })
) {
isWithinPolygonBoundary = true;
}
}
return foundInsidePoint;

return isWithinPolygonBoundary;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/turf-boolean-within/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Determines whether the first geometry is completely within the second geometry.",
"author": "Turf Authors",
"contributors": [
"Rowan Winsemius <@rowanwins>"
"Rowan Winsemius <@rowanwins>",
"Samuel Arbibe <@samuelarbibe>"
],
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -75,6 +76,7 @@
"@turf/boolean-point-on-line": "workspace:*",
"@turf/helpers": "workspace:*",
"@turf/invariant": "workspace:*",
"@turf/line-split": "workspace:*",
"@types/geojson": "^7946.0.10",
"tslib": "^2.8.1"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[10.2166256, -0.7379163],
[11.0362904, 1.794324]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[9.6459207, -1.2281524],
[11.0822393, -1.2461022],
[11.405411, -0.1869151],
[11.1181473, 1.9133448],
[10.0588623, 1.4646914],
[10.6692977, -0.0253296],
[10.6692977, -0.0253296],
[9.6459207, -1.2281524]
]
]
}
}
]
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.