Skip to content

Commit

Permalink
Update self intersection argument, doc gen, add self intersection geo…
Browse files Browse the repository at this point in the history
…json test case, add self intersection test
  • Loading branch information
pm0u committed Oct 30, 2024
1 parent 1dec2f2 commit f5df121
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 19 deletions.
7 changes: 2 additions & 5 deletions packages/turf-line-intersect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
* `line2` **[GeoJSON][1]** any LineString or Polygon
* `options` **[Object][2]** Optional parameters (optional, default `{}`)

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

### Examples

```javascript
var line1 = turf.lineString([[126, -11], [129, -21]]);
var line2 = turf.lineString([[123, -18], [131, -14]]);
var intersects = turf.lineIntersect(line1, line2);

//addToMap
var addToMap = [line1, line2, intersects]
```

Returns **[FeatureCollection][4]<[Point][5]>** point(s) that intersect both
Expand Down
9 changes: 3 additions & 6 deletions packages/turf-line-intersect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ import { sweeplineIntersections as findIntersections } from "./lib/sweepline-int
* @param {GeoJSON} line1 any LineString or Polygon
* @param {GeoJSON} line2 any LineString or Polygon
* @param {Object} [options={}] Optional parameters
* @param {boolean} [options.removeDuplicates=true] remove duplicate intersections
* @param {boolean} [options.ignoreSelfIntersections=false] ignores self-intersections on input features
* @param {boolean} [options.removeDuplicates=true] remove duplicate intersections (default `true`)
* @param {boolean} [options.ignoreSelfIntersections=true] ignores self-intersections on input features (default `true`)
* @returns {FeatureCollection<Point>} point(s) that intersect both
* @example
* var line1 = turf.lineString([[126, -11], [129, -21]]);
* var line2 = turf.lineString([[123, -18], [131, -14]]);
* var intersects = turf.lineIntersect(line1, line2);
*
* //addToMap
* var addToMap = [line1, line2, intersects]
*/
function lineIntersect<
G1 extends LineString | MultiLineString | Polygon | MultiPolygon,
Expand All @@ -40,7 +37,7 @@ function lineIntersect<
ignoreSelfIntersections?: boolean;
} = {}
): FeatureCollection<Point> {
const { removeDuplicates = true, ignoreSelfIntersections = false } = options;
const { removeDuplicates = true, ignoreSelfIntersections = true } = options;
let features: Feature<G1 | G2>[] = [];
if (line1.type === "FeatureCollection")
features = features.concat(line1.features);
Expand Down
61 changes: 53 additions & 8 deletions packages/turf-line-intersect/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import path from "path";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { truncate } from "@turf/truncate";
import {
featureCollection,
// geometryCollection,
lineString,
polygon,
} from "@turf/helpers";
import { featureCollection, lineString, polygon } from "@turf/helpers";
import { lineIntersect } from "./index.js";
import { Feature, LineString, Point } from "geojson";

const directories = {
in: path.join("test", "in") + path.sep,
Expand All @@ -21,14 +17,18 @@ const fixtures = fs.readdirSync(directories.in).map((filename) => {
return {
filename,
name: path.parse(filename).name,
geojson: loadJsonFileSync(directories.in + filename),
geojson: loadJsonFileSync(
directories.in + filename
) as GeoJSON.FeatureCollection<LineString>,
};
});

test("turf-line-intersect", (t) => {
for (const { filename, name, geojson } of fixtures) {
const [line1, line2] = geojson.features;
const results = truncate(lineIntersect(line1, line2));
const results: GeoJSON.FeatureCollection<LineString | Point> = truncate(
lineIntersect(line1, line2)
);
results.features.push(line1);
results.features.push(line2);

Expand Down Expand Up @@ -132,3 +132,48 @@ test("turf-line-intersect - polygon support #586", (t) => {
t.equal(results.features.length, 1, "should return single point");
t.end();
});

test("turf-line-intersect - self intersection behavior", (t) => {
const line1: Feature<LineString> = {
type: "Feature",
properties: {},
geometry: {
type: "LineString",
coordinates: [
[0, 0],
[0, 2],
[2, 1],
[-1, 1],
],
},
};
const line2: Feature<LineString> = {
type: "Feature",
properties: {},
geometry: {
type: "LineString",
coordinates: [
[3, 3],
[4, 4],
[5, 5],
],
},
};

const ignored = lineIntersect(line1, line2);
t.equal(
ignored.features.length,
0,
"self intersections should be ignored by default"
);

const included = lineIntersect(line1, line2, {
ignoreSelfIntersections: false,
});
t.equal(
included.features.length,
1,
"self intersections should be included when ignoreSelfIntersections set to false"
);
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[0, 0],
[0, 2],
[2, 1],
[-1, 1]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[3, 3],
[4, 4],
[5, 5]
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[0, 0],
[0, 2],
[2, 1],
[-1, 1]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[3, 3],
[4, 4],
[5, 5]
]
}
}
]
}

0 comments on commit f5df121

Please sign in to comment.