Add featureMap function to complement featureEach and featureReduce
There is featureEach and featureReduce, but no featureMap. It would be useful when creating derived datasets, where you don't want to interfere with the existing data.
My particular use case involves wanting to apply bboxClip to each feature within a FeatureCollection.
Hey @stevage
I think there's been some debate over the years as to how many of these iterator style modules we should support when the same thing is readily achievable using something like
const out = fc.features.map(function (f){
bboxClip(f, bbox)
})
Is there any particular reason why would you use a turf module rather than doing it natively?
Cheers, Rowan
Yeah, that's a fair point.
Btw it should be return bboxClip(f, bbox) :p
Actually, FWIW, it should be:
const out = turf.featureCollection(fc.features.map(function (f){
return turf.bboxClip(f, bbox)
}));
const out = turf.featureMap(fc, function (f){
return turf.bboxClip(f, bbox)
}));
So the added value is basically the automatic accessing of the .features property, then reconstructing a featureCollection afterwards.
yeah I take your point... it's one of those trade off's. I don't have a strong opinion either way, the main downside I see it that it adds another module that needs to be tested and maintained, which might not seem like much but they add up when refactoring has to happen.
Yeah, I think you're right.
Maybe a different approach to this kind of stuff might be having turf add a .map() function to an FeatureCollection it returns from anything that acts on its features property. Probably downsides to that too. It'd solve a perennial problem I have with manipulating GeoJSON and Turf, in which I'm never 100% sure whether my object is a FeatureCollection or an array of Feature. Or whether it's a Feature, or that Feature's properties.
Good discussion and thank you for the suggestion @stevage. I can see the logic in adding a featureMap function, since we do have featureEach. To be honest, I never use featureEach, and strongly prefer using language conventions over custom iterators. We should consider adding featureMap or deprecating featureEach for consistency.