Skip to content
Merged
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
28 changes: 0 additions & 28 deletions packages/turf-line-chunk/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { length } from "@turf/length";
import { lineSliceAlong } from "@turf/line-slice-along";
import { flattenEach } from "@turf/meta";
import { featureCollection, isObject } from "@turf/helpers";
import { featureCollection, isObject, Units } from "@turf/helpers";
import {
Feature,
FeatureCollection,
GeometryCollection,
LineString,
MultiLineString,
} from "geojson";

/**
* Divides a {@link LineString} into chunks of a specified length.
Expand All @@ -22,30 +29,47 @@ import { featureCollection, isObject } from "@turf/helpers";
* //addToMap
* var addToMap = [chunk];
*/
function lineChunk(geojson, segmentLength, options) {
function lineChunk<T extends LineString | MultiLineString>(
geojson:
| Feature<T>
| FeatureCollection<T>
| T
| GeometryCollection
| Feature<GeometryCollection>,
segmentLength: number,
options: {
units?: Units;
reverse?: boolean;
} = {}
): FeatureCollection<LineString> {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error("options is invalid");
var units = options.units;
var reverse = options.reverse;
const { units = "kilometers", reverse = false } = options;

// Validation
if (!geojson) throw new Error("geojson is required");
if (segmentLength <= 0)
if (segmentLength <= 0) {
throw new Error("segmentLength must be greater than 0");
}

// Container
var results = [];
const results: Feature<LineString>[] = [];

// Flatten each feature to simple LineString
flattenEach(geojson, function (feature) {
flattenEach(geojson, (feature: Feature<T>) => {
// reverses coordinates to start the first chunked segment at the end
if (reverse)
if (reverse) {
feature.geometry.coordinates = feature.geometry.coordinates.reverse();
}

sliceLineSegments(feature, segmentLength, units, function (segment) {
results.push(segment);
});
sliceLineSegments(
feature as Feature<LineString>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit of a sketchy cast, but I can't change this without changing the input signature which also takes GeometryCollection and Feature<GeometryCollection>, which theoretically allow more geometry types. I think we just pass through non-line geometries though 🤷 . Kind of a weird API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quickly checked and the code throws at runtime if passed a polygon, and fails silently if passed a point. Do we add a runtime check to avoid the throw for polys (fixing a bug), and print a warning for everything that won't be handled e.g."Passed a <geometry.type> to lineChunk, probably in your GeometryCollection, ignoring."?

Longer term I'm pretty sure we can narrow to GeometryCollection<T> and Feature<GeometryCollection<T>>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I tested Point and it didn't throw an error, so I was hesitant to change the runtime behavior by starting to throw one. Lets land the js -> ts migration and I'll come behind and make the GeometryCollection typings more strict and look at what to do for the unsupported geometry types in a separate PR (for easier revert, if need be 🤞).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

starting to throw one

Wasn't suggesting that. Leave point as is and guard against polygon throwing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah lets continue discussing here: #2970 (comment) :)
Happy to do either way though.

segmentLength,
units,
(segment) => {
results.push(segment);
}
);
});
return featureCollection(results);
}
Expand All @@ -60,11 +84,18 @@ function lineChunk(geojson, segmentLength, options) {
* @param {Function} callback iterate over sliced line segments
* @returns {void}
*/
function sliceLineSegments(line, segmentLength, units, callback) {
function sliceLineSegments(
line: Feature<LineString>,
segmentLength: number,
units: Units,
callback: (feature: Feature<LineString>) => void
): void {
var lineLength = length(line, { units: units });

// If the line is shorter than the segment length then the orginal line is returned.
if (lineLength <= segmentLength) return callback(line);
if (lineLength <= segmentLength) {
return callback(line);
}

var numberOfSegments = lineLength / segmentLength;

Expand All @@ -80,7 +111,7 @@ function sliceLineSegments(line, segmentLength, units, callback) {
segmentLength * (i + 1),
{ units: units }
);
callback(outline, i);
callback(outline);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just standardized on having no index arg since it wasn't being used and the calls conflicted a bit.

}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/turf-line-chunk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@
"tape": "^5.9.0",
"tsup": "^8.4.0",
"tsx": "^4.19.4",
"typescript": "^5.8.3",
"write-json-file": "^6.0.0"
},
"dependencies": {
"@turf/helpers": "workspace:*",
"@turf/length": "workspace:*",
"@turf/line-slice-along": "workspace:*",
"@turf/meta": "workspace:*",
"@types/geojson": "^7946.0.10"
"@types/geojson": "^7946.0.10",
"tslib": "^2.8.1"
}
}
17 changes: 13 additions & 4 deletions packages/turf-line-chunk/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { truncate } from "@turf/truncate";
import { featureEach } from "@turf/meta";
import { lineString, featureCollection } from "@turf/helpers";
import {
lineString,
featureCollection,
point,
geometryCollection,
} from "@turf/helpers";
import { lineChunk } from "./index.js";
import { Feature } from "geojson";

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

Expand All @@ -17,7 +23,10 @@ const directories = {
};

const fixtures = fs.readdirSync(directories.in).map((filename) => {
return { filename, geojson: loadJsonFileSync(directories.in + filename) };
return {
filename,
geojson: loadJsonFileSync(directories.in + filename) as any,
};
});

test("turf-line-chunk: shorter", (t) => {
Expand Down Expand Up @@ -103,8 +112,8 @@ test("turf-line-chunk: Prevent input mutation", (t) => {
* @param {FeatureCollection|Feature<any>} geojson Feature or FeatureCollection
* @returns {FeatureCollection<any>} colorized FeatureCollection
*/
function colorize(geojson) {
const results = [];
function colorize(geojson: any) {
const results: Feature[] = [];
featureEach(geojson, (feature, index) => {
const r = index % 2 === 0 ? "F" : "0";
const g = index % 2 === 0 ? "0" : "0";
Expand Down
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.