Skip to content

Commit

Permalink
Merge branch 'master' into proj-doc-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
smallsaucepan authored Dec 28, 2024
2 parents feb8f80 + d1a2c58 commit 3775a0a
Show file tree
Hide file tree
Showing 51 changed files with 218,587 additions and 437 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]** ignore self-intersections on input features (optional, default `true`)

### Examples

Expand Down
12 changes: 5 additions & 7 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] ignore 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 @@ -30,13 +30,12 @@ import { polygonToLine } from "@turf/polygon-to-line";
function booleanDisjoint(
feature1: Feature<any> | Geometry,
feature2: Feature<any> | Geometry,
options: {
{
ignoreSelfIntersections = true,
}: {
ignoreSelfIntersections?: boolean;
} = {}
} = { ignoreSelfIntersections: true }
): boolean {
const ignoreSelfIntersections: boolean =
options.ignoreSelfIntersections ?? false;

let bool = true;
flattenEach(feature1, (flatten1) => {
flattenEach(feature2, (flatten2) => {
Expand Down Expand Up @@ -65,7 +64,6 @@ function booleanDisjoint(
function disjoint(geom1: any, geom2: any, ignoreSelfIntersections: boolean) {
switch (geom1.type) {
case "Point":
/* eslint-disable @typescript-eslint/no-unused-vars */
switch (geom2.type) {
case "Point":
return !compareCoords(geom1.coordinates, geom2.coordinates);
Expand Down
79 changes: 55 additions & 24 deletions packages/turf-boolean-disjoint/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test("turf-boolean-disjoint", (t) => {
.sync(path.join(__dirname, "test", "true", "**", "*.geojson"))
.forEach((filepath) => {
const name = path.parse(filepath).name;
const geojson = loadJsonFileSync(filepath);
const geojson: GeoJSON.FeatureCollection = loadJsonFileSync(filepath);
const feature1 = geojson.features[0];
const feature2 = geojson.features[1];
const result = disjoint(feature1, feature2);
Expand All @@ -30,7 +30,7 @@ test("turf-boolean-disjoint", (t) => {
.sync(path.join(__dirname, "test", "false", "**", "*.geojson"))
.forEach((filepath) => {
const name = path.parse(filepath).name;
const geojson = loadJsonFileSync(filepath);
const geojson: GeoJSON.FeatureCollection = loadJsonFileSync(filepath);
const feature1 = geojson.features[0];
const feature2 = geojson.features[1];
const result = disjoint(feature1, feature2);
Expand Down 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
9 changes: 4 additions & 5 deletions packages/turf-boolean-intersects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { flattenEach } from "@turf/meta";
* @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] ignore self-intersections on input features
* @returns {boolean} true if geometries intersect, false otherwise
* @example
* var point1 = turf.point([2, 2]);
Expand All @@ -30,13 +30,12 @@ import { flattenEach } from "@turf/meta";
function booleanIntersects(
feature1: Feature<any> | Geometry,
feature2: Feature<any> | Geometry,
options: {
{
ignoreSelfIntersections = true,
}: {
ignoreSelfIntersections?: boolean;
} = {}
) {
const ignoreSelfIntersections: boolean =
options.ignoreSelfIntersections ?? false;

let bool = false;
flattenEach(feature1, (flatten1) => {
flattenEach(feature2, (flatten2) => {
Expand Down
40 changes: 20 additions & 20 deletions packages/turf-boolean-intersects/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,62 +122,62 @@ test("turf-boolean-intersects with ignoreSelfIntersections option", (t) => {
selfIntersectingLineString,
nonIntersectingLineString
);
t.true(
t.false(
result,
"[true] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=false)"
"[false] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=true)"
);
result = intersects(selfIntersectingLineString, intersectingLineString);
t.true(
result,
"[true] " +
"selfIntersectingLineString-LineString (ignoreSelfIntersections=false)"
"selfIntersectingLineString-LineString (ignoreSelfIntersections=true)"
);
result = intersects(selfIntersectingLineString, intersectingPolygon);
t.true(
result,
"[true] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=false)"
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=true)"
);
result = intersects(selfIntersectingLineString, nonIntersectingPolygon);
t.true(
t.false(
result,
"[true] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=false)"
"[false] " +
"selfIntersectingLineString-Polygon (ignoreSelfIntersections=true)"
);

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

t.end();
Expand Down
19 changes: 19 additions & 0 deletions packages/turf-boolean-point-on-line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ function isPointOnLineSegment(
} else if (cross !== 0) {
return false;
}

// Special cases for zero length lines
// https://github.com/Turfjs/turf/issues/2750
if (Math.abs(dxl) === Math.abs(dyl) && Math.abs(dxl) === 0) {
// Zero length line.
if (excludeBoundary) {
// To be on a zero length line pt has to be on the start (and end), BUT we
// are excluding start and end from possible matches.
return false;
}
if (pt[0] === lineSegmentStart[0] && pt[1] === lineSegmentStart[1]) {
// If point is same as start (and end) it's on the line segment
return true;
} else {
// Otherwise point is somewhere else
return false;
}
}

if (!excludeBoundary) {
if (Math.abs(dxl) >= Math.abs(dyl)) {
return dxl > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1;
Expand Down
35 changes: 34 additions & 1 deletion packages/turf-boolean-point-on-line/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import path from "path";
import { fileURLToPath } from "url";
import test from "tape";
import { loadJsonFileSync } from "load-json-file";
import { booleanPointOnLine as pointOnLine } from "./index.js";
import { point, lineString } from "@turf/helpers";
import booleanPointOnLine, {
booleanPointOnLine as pointOnLine,
} from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -36,3 +39,33 @@ test("turf-boolean-point-on-line", (t) => {
});
t.end();
});

test("turf-boolean-point-on-line - issue 2750", (t) => {
// Issue 2750 was that in the first test below where point is on a different
// longitude to a zero length line booleanPointOnLine gave the correct result,
// while the second test where a point on the SAME longitude, but nowhere
// near, that zero length line incorrectly returned true.
t.false(
booleanPointOnLine(
point([2, 13]),
lineString([
[1, 1],
[1, 1],
])
),
"#2750 different longitude point not on zero length line"
);

t.false(
booleanPointOnLine(
point([1, 13]),
lineString([
[1, 1],
[1, 1],
])
),
"#2750 same longitude point not on zero length line"
);

t.end();
});
8 changes: 4 additions & 4 deletions packages/turf-difference/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson";
import polygonClipping, { Geom } from "polygon-clipping";
import * as polyclip from "polyclip-ts";
import { polygon, multiPolygon } from "@turf/helpers";
import { geomEach } from "@turf/meta";

Expand Down Expand Up @@ -39,10 +39,10 @@ import { geomEach } from "@turf/meta";
function difference(
features: FeatureCollection<Polygon | MultiPolygon>
): Feature<Polygon | MultiPolygon> | null {
const geoms: Array<Geom> = [];
const geoms: Array<polyclip.Geom> = [];

geomEach(features, (geom) => {
geoms.push(geom.coordinates as Geom);
geoms.push(geom.coordinates as polyclip.Geom);
});

if (geoms.length < 2) {
Expand All @@ -51,7 +51,7 @@ function difference(

const properties = features.features[0].properties || {};

const differenced = polygonClipping.difference(geoms[0], ...geoms.slice(1));
const differenced = polyclip.difference(geoms[0], ...geoms.slice(1));
if (differenced.length === 0) return null;
if (differenced.length === 1) return polygon(differenced[0], properties);
return multiPolygon(differenced, properties);
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-difference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@turf/helpers": "workspace:^",
"@turf/meta": "workspace:^",
"@types/geojson": "^7946.0.10",
"polygon-clipping": "^0.15.3",
"polyclip-ts": "^0.16.8",
"tslib": "^2.8.1"
}
}
Loading

0 comments on commit 3775a0a

Please sign in to comment.