Suggestion: Generalise bboxClip to support all GeoJSON object types
There is not really an easy way to crop a featureCollection (or other geometry type) to a polygon or bbox.
There are times when I just want to do:
const newFC = turf.crop(oldFC, [xmin, ymin, xmax, ymax])
or
const newFC = turf.crop(oldFC, cropPolygon)
It's relatively easy to use intersect to crop Polygons, and booleanPointInPolygon to filter out Points, but I'm not even sure if there's a straightforward way to crop LineStrings currently.
Ah, I have eventually discovered that bboxClip does some, but not all of this:
- only handles bbox, not arbitrary polygons
- does not handle Point/MultiPoint (easy to implement)
- does not handle FeatureCollection (easy to implement)
- has an ominous and vague warning: "May result in degenerate edges when clipping Polygons."
But I suspect clip is the better term rather than crop.
Thanks @stevage. Would mask and difference do what you're hoping? mask can take a legit polygon.
Alternatively, would we be better off adding a new general purpose clip function (that takes whatever you throw at it) and deprecate bboxClip? I'm not sure the history behind only cropping with a bbox, so maybe there's a particular use case for that.
Your example would only need a minor mod:
const newFC = turf.clip(oldFC, turf.bboxPolygon([xmin, ymin, xmax, ymax]))
I don't think mask and difference would achieve the same thing.
In any case, operations using a general polygon (as opposed to a bbox) are orders of magnitude slower, and mostly what I (and probably others) need is the bbox clip.
So I've gone and implemented this in #2814.
Ah ok. From the initial post you mentioned there being no easy way to "crop ... to a polygon or bbox" so thought you wanted to add clipping with a polygon as well. Will take a look at #2814.
Yeah, well, I did. But on further reflection, a general clipping function and a bbox clipping function are probably best kept as two separate concepts.
Well, this is bizarre, I've just discovered issue #1565.
Apparently I made exactly the same suggestion in January 2019. I have no memory of this.
That issue got a bit derailed by the topic of spatial indexing.