-
Notifications
You must be signed in to change notification settings - Fork 991
Migrate @turf/line-chunk to TypeScript #2969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
|
@@ -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>, | ||
| segmentLength, | ||
| units, | ||
| (segment) => { | ||
| results.push(segment); | ||
| } | ||
| ); | ||
| }); | ||
| return featureCollection(results); | ||
| } | ||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -80,7 +111,7 @@ function sliceLineSegments(line, segmentLength, units, callback) { | |
| segmentLength * (i + 1), | ||
| { units: units } | ||
| ); | ||
| callback(outline, i); | ||
| callback(outline); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
GeometryCollectionandFeature<GeometryCollection>, which theoretically allow more geometry types. I think we just pass through non-line geometries though 🤷 . Kind of a weird API.There was a problem hiding this comment.
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>andFeature<GeometryCollection<T>>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I tested
Pointand 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 🤞).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't suggesting that. Leave point as is and guard against polygon throwing.
There was a problem hiding this comment.
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.