2

I have a large (2.5 MB) geoJSON file with polygons and multi-polygons.

I want to get the union of all of them to plot on a map in the browser.

In python I use shapely.ops.unary_union which takes less than a second

In javascript is use turf.union (docs) which takes almost a minute with the same file...

Is there a way to achieve fast polygon union in javascript? I am trying to avoid doing that on the server side

Python code:

from shapely.ops import unary_union
from shapely.geometry import shape
geoJSON=json.load(open(filename,'rb'))
shapes=[]
for feature in geoJSON['features']:
    shapes.append(shape(feature['geometry']))
union = unary_union(shapes)

Javascript code:

function union(geojson){
    features = geojson.features
    union = turf.union(...features)
}
OMRY VOLK
  • 1,429
  • 11
  • 24
  • Does this answer your question? [What is the fastest way to create a union of many polygons in turfjs?](https://stackoverflow.com/questions/59222059/what-is-the-fastest-way-to-create-a-union-of-many-polygons-in-turfjs) – four-eyes Apr 11 '23 at 07:56

2 Answers2

0

This ain't really a solution but found it faster than processing all the features at once

union = features.reduce((accUnion,feature,index)=>index?turf.union(accUnion,feature):feature,null);
AAS
  • 49
  • 3
0

If you need a really fast way to merge polygons on client side then I would recommend to write a polygon-union algorithm in Rust or C++ and compile it to WebAssembly.

You can then import the .wasm file into Javascript and benefit from the speed of WebAssembly.

zingi
  • 1,141
  • 13
  • 23
  • With a short google research I found [this js-angusj-clipper](https://github.com/xaviergonz/js-angusj-clipper#getting-started) js library, which supports [union](https://github.com/xaviergonz/js-angusj-clipper/blob/master/docs/apiReference/clipping/ClipType.md) operation. Maybe this one will meet your requirements. – zingi Mar 12 '21 at 14:04
  • It seems the js-angusj-clipper lib only supports integers but you could try to multiply your lat,lon coordinates with the correct power of ten. I'm not sure how much performance that would cost. – zingi Mar 12 '21 at 14:11