Skip to content

Commit

Permalink
Boolean disjoint default to ignore self intersections. Update tests. …
Browse files Browse the repository at this point in the history
…Add test case for self intersecting polygon
  • Loading branch information
pm0u committed Oct 29, 2024
1 parent 1dec2f2 commit 7eba673
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/turf-boolean-disjoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Boolean-disjoint returns (TRUE) if the intersection of the two geometries is an
* `feature2` **([Geometry][1] | [Feature][2]\<any>)** GeoJSON Feature or Geometry
* `options` **[Object][3]** Optional parameters (optional, default `{}`)

* `options.ignoreSelfIntersections` **[boolean][4]** ignores self-intersections on input features (optional, default `false`)
* `options.ignoreSelfIntersections` **[boolean][4]** ignores self-intersections on input features (optional, default `true`)

### Examples

Expand Down
4 changes: 2 additions & 2 deletions packages/turf-boolean-disjoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { polygonToLine } from "@turf/polygon-to-line";
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
* @param {Object} [options={}] Optional parameters
* @param {boolean} [options.ignoreSelfIntersections=false] ignores self-intersections on input features
* @param {boolean} [options.ignoreSelfIntersections=true] ignores self-intersections on input features
* @returns {boolean} true if the intersection is an empty set, false otherwise
* @example
* var point = turf.point([2, 2]);
Expand All @@ -35,7 +35,7 @@ function booleanDisjoint(
} = {}
): boolean {
const ignoreSelfIntersections: boolean =
options.ignoreSelfIntersections ?? false;
options.ignoreSelfIntersections ?? true;

let bool = true;
flattenEach(feature1, (flatten1) => {
Expand Down
75 changes: 53 additions & 22 deletions packages/turf-boolean-disjoint/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,64 +117,95 @@ test("turf-boolean-disjoin with ignoreSelfIntersections option", (t) => {
},
};

// Test without ignoringSelfIntersections option (default behavior)
const selfIntersectingPolygon: GeoJSON.Feature<GeoJSON.Polygon> = {
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates: [
[
[1.5, 1],
[2, 1.5],

[3, 0.5],
[-1, 3],
[1.5, 1],
],
],
},
};

// Test with ignoringSelfIntersections = true (default behavior)
let result = disjoint(selfIntersectingLineString, nonIntersectingLineString);
t.false(
t.true(
result,
"[false] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=false)"
"[true] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=true)"
);
result = disjoint(selfIntersectingLineString, intersectingLineString);
t.false(
result,
"[false] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=false)"
"selfIntersectingLineString-LineString (ignoreSelfIntersections=true)"
);
result = disjoint(selfIntersectingLineString, intersectingPolygon);
t.false(
result,
"[false] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=false)"
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=true)"
);
result = disjoint(selfIntersectingLineString, nonIntersectingPolygon);
t.false(
t.true(
result,
"[false] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=false)"
"[true] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=true)"
);
result = disjoint(selfIntersectingPolygon, nonIntersectingPolygon);
t.true(
result,
"[true] " + "selfIntersectingPolygon-Polygon (ignoreSelfIntersections=true)"
);

// Test with ignoringSelfIntersections option
// Test with ignoringSelfIntersections option set to false
result = disjoint(selfIntersectingLineString, nonIntersectingLineString, {
ignoreSelfIntersections: true,
ignoreSelfIntersections: false,
});
t.true(
t.false(
result,
"[true] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=true)"
"[false] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=false)"
);
result = disjoint(selfIntersectingLineString, intersectingLineString, {
ignoreSelfIntersections: true,
ignoreSelfIntersections: false,
});
t.false(
result,
"[false] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=true)"
"selfIntersectingLineString-LineString (ignoreSelfIntersections=false)"
);
result = disjoint(selfIntersectingLineString, intersectingPolygon, {
ignoreSelfIntersections: true,
ignoreSelfIntersections: false,
});
t.false(
result,
"[false] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=true)"
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=false)"
);
result = disjoint(selfIntersectingLineString, nonIntersectingPolygon, {
ignoreSelfIntersections: true,
ignoreSelfIntersections: false,
});
t.true(
t.false(
result,
"[true] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=true)"
"[false] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=false)"
);
result = disjoint(selfIntersectingPolygon, nonIntersectingPolygon, {
ignoreSelfIntersections: false,
});
t.false(
result,
"[false] " +
"selfIntersectingPolygon-Polygon (ignoreSelfIntersections=false)"
);

t.end();
Expand Down

0 comments on commit 7eba673

Please sign in to comment.