turf
turf copied to clipboard
[email protected] supports multiple polygon param but [email protected] not support
We also ran into the issue, the docs still state that it supports more than 2 polygons though http://turfjs.org/docs/#union
It has been an issue for quite some time it appears: https://github.com/Turfjs/turf/issues/1669
My workaround for now:
export const multiUnion = (
thingsToUnion: Array<
| turf.Feature<turf.Polygon | turf.MultiPolygon>
| turf.Polygon
| turf.MultiPolygon
>,
): turf.Feature<turf.Polygon | turf.MultiPolygon> => {
const [firstThing, ...remainingThings] = thingsToUnion;
if (!firstThing) {
throw new Error("Expected at least one feature or geometry, got none");
}
let result =
firstThing.type === "Feature" ? firstThing : turf.feature(firstThing);
for (const remainingThing of remainingThings) {
result = turf.union(result, remainingThing);
}
return result;
};
multiUnion([]); // throws
multiUnion([geomOrFeature]); // noop for feature, converts geometry to feature
multiUnion([geomOrFeature1, geomOrFeature2]); // works like turf.union(geomOrFeature1, geomOrFeature2)
multiUnion([geomOrFeature1, ...moreGeomsOrFeatures]); // also works