Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@turf/union gives 'RangeError: Maximum call stack size exceeded' error after upgrading #2317

Open
cherryshoe opened this issue Jul 26, 2022 · 2 comments
Labels
dependencies Pull requests that update a dependency file @turf/union

Comments

@cherryshoe
Copy link

cherryshoe commented Jul 26, 2022

@turf/union@6.5.0 gives 'RangeError: Maximum call stack size exceeded' error after upgrading from @turf/union@5.1.5, with same input parameters.

Version 6.5.0 snippet of code that calls turf.union fails with the error:

  _todo = () => {
    const multipolygon1Feature = <contents from https://gist.githubusercontent.com/cherryshoe/dbcf102f33813cf4ceaa3fdced471a3b/raw/1cdc5714df2f52973b44d5e3c2060428b3c5125a/multipolygon1Feature.geojson>;
    const multipolygon2 = <contents from https://gist.githubusercontent.com/cherryshoe/446520bcffe988428de71f54569af13b/raw/0fb97ec414f7c2968b137b5863281d86e4c665c6/multipolygon2.geojson>;
    const multipolygon1 = multipolygon1Feature.geometry;
	
    console.log("Before");
    // 6.5.0 works without Feature<Multipolygon|Polygon>
    const newMultiPolygon = turf.union(multipolygon1, multipolygon2);
    console.log("Done");
  }

Version 5.1.5 snippet of code that calls turf.union works:

  _todo = () => {
    const multipolygon1Feature = <contents from https://gist.githubusercontent.com/cherryshoe/dbcf102f33813cf4ceaa3fdced471a3b/raw/1cdc5714df2f52973b44d5e3c2060428b3c5125a/multipolygon1Feature.geojson>;
    const multipolygon2 = <contents from https://gist.githubusercontent.com/cherryshoe/446520bcffe988428de71f54569af13b/raw/0fb97ec414f7c2968b137b5863281d86e4c665c6/multipolygon2.geojson>;
    const multipolygon2Feature = turf.feature(multipolygon2);
	
    console.log("Before");
    // 5.1.5 works with Feature<Multipolygon|Polygon>
    const newMultiPolygon = turf.union(multipolygon1Feature, multipolygon2Feature);
    console.log("Done");
  }
@twelch
Copy link
Collaborator

twelch commented Jul 26, 2022

I think this is an issue with the upstream polygon-clipping library, which was probably swapped in as the implementation for turf.union between the two versions you are using. See this related issue and feel free to add to it with your example -- mfogel/polygon-clipping#111.

Resolving this would need to happen upstream it looks like so I can leave this issue open.

When I hit this error, I can usually workaround it by breaking down the inputs into smaller chunks. In your case it might be sufficient to split the first or second input from one into two multipolygons, each with half the features. Then call union for each half, and combine the partial results with a final union. You can increase the number of chunks (reducing the input size for each call) until you find something that works. A little clunky but functional.

I use this chunking technique for intersection and maybe for difference as well. You just have to be aware of what the operation is doing to know how to combine the partial results.

Here's an example of a chunk function (typescript) that takes an array of anything and returns an array of arrays with length no greater than chunkSize. The last chunk will have the remainder. Modify to suit your needs.

/**
 * Splits an array into chunks of size
 */
export function chunk<T>(array: T[], chunkSize: number): T[][] {
  const R: any[] = [];
  for (let i = 0, len = array.length; i < len; i += chunkSize)
    R.push(array.slice(i, i + chunkSize));
  return R;
}

@rowanwins rowanwins added dependencies Pull requests that update a dependency file @turf/union labels Oct 6, 2022
@tnlogy
Copy link

tnlogy commented Sep 8, 2023

I've tried this both on 6.5.0 and 7.0.0-alpha0 with the same bug. Attaching two geojson objects that create the bug when called with union(featureCollection([geom1.type === 'Feature' ? geom1 : feature(geom1), geom2.type === 'Feature' ? geom2 : feature(geom2)]))

scratch_20.txt
scratch_21.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file @turf/union
Projects
None yet
Development

No branches or pull requests

4 participants