[@turf/coordsMap] proposal
See original GitHub issueExplaination
Hey 😃
Yesterday, searching for a tool to loop and map over coordinates, I did not succeed to find one.
The idea was basically simple : loop over all coordinates, and map each of them with a new item (new position, i.e); but the key part is leaving the whole geojson object untouched
I can think of 3 example use-cases, including mines :
- Reproject a whole GeoJSON object
- Remove
z
([lng, lat, z]
) from coordinates in a whole, dirty, GeoJSON object - Round coordinates to 6 decimal places into a whole GeoJSON object
- (do the 3 previous operations in a single motion, to improve performance)
I found coordsEach (which just loop), coordsReduce (which would suffice to recompose a new geojson, with indexes and stuff, but would not be very readable), and perliedman/reproject (which is too linked with reprojection stuff, and provide a too-rough cloning strategy IMHO). FYI I opened an issue there to propose extraction of “mapping coordinates logic” in another repo.
I ended up implementing it myself, but I think it could be a great addition to @turfjs 😄
Here is it : traverse-geojson
I used some gjtk tools, and the implementation is very basic. I’d like to use @turfjs formalism and helpers (invariants, etc.), but I’m not really used to it.
API
coordsMap(input, transformer)
=> returns output
(same shape as input)
Params
input
: GeoJSON<Any> (FeatureCollection<Any>|Geometry<Any>|Coordinates<Any>|Position|GeometryCollection)transfomer
:function(position)
=> returnstransformedPosition
position
: [lng, lat[, …rest]]transformedPosition
: any, ideally a position
output
: GeoJSON<Any>, exactinput
deep clone, with only coordinates mapped through transformer
Questions :
Is that a good idea ?
Could it be integrated into turf ?
What about also
featuresMap
, which would basically do the same stuff, but with features ?
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
Untested, but this would be the general direction of implementing each suggestion. And, yes, this mutates the data, but… just clone the data ahead of time, which is just the same as what a function would do internally.
And about the API in itself (returning geoJSON) object… well from my external POV I understood “coordsWhatever” functions like “mimic array functions on GeoJSON objects”. I kinda agree with you, but returning an array of transformed cords would be pointless, I guess (as you loose every indexes, tree logic, features)