turf icon indicating copy to clipboard operation
turf copied to clipboard

Add featureMap function to complement featureEach and featureReduce

Open stevage opened this issue 7 years ago • 6 comments

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.

stevage avatar Jan 03 '19 03:01 stevage

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

rowanwins avatar Jan 03 '19 03:01 rowanwins

Yeah, that's a fair point.

Btw it should be return bboxClip(f, bbox) :p

stevage avatar Jan 03 '19 03:01 stevage

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.

stevage avatar Jan 03 '19 03:01 stevage

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.

rowanwins avatar Jan 03 '19 03:01 rowanwins

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.

stevage avatar Jan 10 '19 06:01 stevage

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.

morganherlocker avatar Jan 30 '19 18:01 morganherlocker