-
Notifications
You must be signed in to change notification settings - Fork 993
cleanCoords: Add collection support and typescript fixes #3012
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
base: master
Are you sure you want to change the base?
cleanCoords: Add collection support and typescript fixes #3012
Conversation
- Added support for FeatureCollection and GeometryCollection types - Improved GeoJSON type signatures, returning the same type that as passed - Added ability to pass epsilon through to booleanPointOnLine - Ensured clarity on structural sharing and documented - Removed some redundant checks
- Update benchmark results - Fix test formatting - Minor typescript enhancements
- Explict infer for better inference on union types - Added overload equivalent to old type signature with deprecation warning
|
After initially failed CI fixed the types to add better inference for union types and also put in a deprecated signature matching the old behavior to not break any build systems that might error on failed type resolution. This signature can be remove in v8. |
|
Great additions. Thank you @bratter! Reckon we should move away from Regarding mutation, if this model is used consistently throughout Turf, it might be worth adding a section on it to the website, so we can keep the per-function documentation as brief as possible, and cross reference instead. Btw, our approach to mutation gives me the heebie-jeebies. To call and then find out changing f2.properties.whatever changes f1? Know it's a performance thing, though possibly worth documenting more clearly. Can you please remind me about the collapse idea? Is it basically that truncating a geometry makes it more likely for points to become coincident and from there get cleaned beyond what's technically valid? |
| geojson: T, | ||
| options?: { mutate?: boolean; epsilon?: number } | ||
| ): CleanCoordsResult<T>; | ||
| function cleanCoords( |
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.
Do we need this variation if as well as the one above it?
| ): any; | ||
| /** | ||
| * Removes redundant coordinates from any GeoJSON Geometry. | ||
| * Removes redundant coordinates from any GeoJSON Type. |
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.
"any GeoJSON object" might be better here.
| geojson: GeoJSON, | ||
| options?: { mutate?: boolean; epsilon?: number } | ||
| ): GeoJSON; | ||
| /** @deprecated loosely typed version deprecated. Will be removed in next major version */ |
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.
Let's mark as deprecated now but not necessarily commit to when it will be removed.
| ): Position[][] { | ||
| // Re-use the polygon's rings array when mutating to maximize sharing | ||
| // It would be possible to follow a similar approach to cleanLine and only | ||
| // create a new raings array when we know something has changed even for |
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.
Typo "raings"
|
Thanks for the review! Will get to this when I have a sec including responding to your queries. |
Hmmmm. It seems that the term is also used differently in different places too. in the context of On reflection, the best approach might be to just drop it from the API here again (back to how it was), then consider a change in
True, but the alternatives are really only a deep clone on every function that mutates or immutable data structures... so maybe no alternative. If it were me, I'd have all coordinate mutation functions just mutate by default so that a user would be forced to assume mutation 100% of the time and they do a deep clone if want to preserve the original. Barring that, the current approach is probably best, and my being explicit about it is really just a codification of what was generally happening with some slight consistency fixes. If you think this works, I can probably work through all the coordinate mutation methods and check/refine their mutation so they are all consistent, then do as you suggest and take out of the docs here. Let me know if you want to take out of docs now.
The two cases that I'd be trying to address is (a) someone passes in a valid polygon, truncates it, then runs What I would propose for the collapse option is that LineStrings that end with only two co-incident points get dropped instead of passed through, and polygon rings that end up with 3 or fewer points get dropped instead of throwing. If this completely eliminates the geometry, then we can either return a null or a null geometry in the feature. I think this provides a nice way of capturing degenerate geometries during cleaning in a consistent way without the user having to set up try...catch. I suspect many users would just drop the whole shape in such a block, but if, say, the issue is just a small hole in a polygon, you'd likely drop the whole polygon for something very easily fixed. That may not have made sense... lol. I can make a repro if that'll help. Would likely do as another PR anyway. |
Addresses #2998.
Summary
Adds some features to clean-coords:
FeatureCollectionandGeometryCollectiontypes through a recursive call.booleanPointOnLinecalls.Additional Notes
Geometryare always reused when mutate=true and always new when mutate=falseFuture Work
I do wonder if you would be interested in a follow up PR that adds a
collapseoption? Reason being I am concerned by the workflow of input > truncate > clean throwing errors for valid input. The collapse option would remove the offending geometry at the ring level rather than throw an error. Thoughts?Please provide the following when creating a PR:
contributorsfield ofpackage.json- you've earned it! 👏