locationtech / jts

The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Other
1.94k stars 442 forks source link

Add option to start building another STRTree from other STRTree #967

Open zdila opened 1 year ago

zdila commented 1 year ago

We need a mean to "clone" existing STRTree to another one. Cloning should not build any of the source or destination STRTree.

STRTree index1 = new STRTree();

index1.insert(...);

STRTree index2 = new STRTree(index1);

// optional API: index2.insertAll(index1);

index1.insert(...);

index2.insert(...);

This could be suitable also for other SpatialIndexes.

jiayuasu commented 1 year ago

@zdila in Apache Sedona (https://github.com/apache/sedona), we have been dealing with this issue for a while and I contributed a few functions to JTS to make the clone possible (in Sedona, we call it Serialize/Deserialize): https://github.com/locationtech/jts/pull/634 The necessary building blocks in JTS are package-private so it seems to be impossible to implement but it is actually possible.

Please feel free to use our Sedona serializer to clone STRtree and QuadTree.

Implementation: https://github.com/apache/sedona/tree/master/common/src/main/java/org/locationtech/jts/index Coding example: https://github.com/apache/sedona/blob/master/common/src/test/java/org/apache/sedona/common/geometrySerde/SpatialIndexSerdeTest.java#L83

To use it, add the following dependencies in addition to your JTS dependency

<!-- https://mvnrepository.com/artifact/org.apache.sedona/sedona-common -->
<dependency>
    <groupId>org.apache.sedona</groupId>
    <artifactId>sedona-common</artifactId>
    <version>1.4.0</version>
</dependency>

<dependency>
    <groupId>com.esotericsoftware</groupId>
    <artifactId>kryo</artifactId>
    <version>4.0.2</version>
</dependency>