turf icon indicating copy to clipboard operation
turf copied to clipboard

[email protected] supports multiple polygon param but [email protected] not support

Open 862881015 opened this issue 5 years ago • 2 comments

862881015 avatar May 08 '20 14:05 862881015

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

BerndSchrooten avatar Jun 03 '20 09:06 BerndSchrooten

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

kachkaev avatar Feb 14 '21 19:02 kachkaev