-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Merged
+68
−48
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.