Union geometries faster in JTS?

2.8k views Asked by At

I wrote code to join (union) geometries. I wrapped it into Java8 streams Collector. Inside it it just uses Geometry#union to union geometries:

geometries[0] = geometries[0].union(geometry);

Unfortunately, it works rather slow.

Is it possible to make it faster with some usage of prepared geometry or some other hacks?

UPDATE

Geometries are like this:enter image description here

and they are of very different scales.

2

There are 2 answers

4
Ohlsen1980 On BEST ANSWER
0
dr_jts On

Your stated approach will be slow, because it merges each geometry sequentially into the result, which likely gets larger and larger with each union.

Unary union uses a spatial index to cluster the geometries and merge them hierarchically, which provides better performance. Unfortunately this might not work well with the sequential nature of Java streams.

PreparedGeometry doesn't offer any speedup for overlay operations like union.