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

Add Hilbert Packed Rtree #494

Merged
merged 11 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.GeometryFilter;
import org.locationtech.jts.index.SpatialIndex;
import org.locationtech.jts.index.hprtree.HPRtree;
import org.locationtech.jts.index.kdtree.KdTree;
import org.locationtech.jts.index.quadtree.Quadtree;
import org.locationtech.jts.index.strtree.AbstractNode;
Expand Down Expand Up @@ -65,7 +67,8 @@ private static KdTree buildKdTree(Geometry geom, double tolerance) {

public static Geometry strTreeBounds(Geometry geoms)
{
STRtree index = buildSTRtree(geoms);
STRtree index = new STRtree();
loadIndex(geoms, index);
List bounds = new ArrayList();
addBounds(index.getRoot(), bounds, geoms.getFactory());
return geoms.getFactory().buildGeometry(bounds);
Expand All @@ -88,17 +91,17 @@ private static void addBounds(Boundable bnd, List bounds,
}
}

public static Geometry strTreeQuery(Geometry geoms, Geometry queryEnv)
public static Geometry hprTreeQuery(Geometry geoms, Geometry queryEnv)
{
STRtree index = buildSTRtree(geoms);
HPRtree index = new HPRtree();
loadIndex(geoms, index);
// if no query env provided query everything inserted
if (queryEnv == null) queryEnv = geoms;
List result = index.query(queryEnv.getEnvelopeInternal());
return geoms.getFactory().buildGeometry(result);
}

private static STRtree buildSTRtree(Geometry geom) {
final STRtree index = new STRtree();
private static void loadIndex(Geometry geom, SpatialIndex index) {
geom.apply(new GeometryFilter() {

public void filter(Geometry geom) {
Expand All @@ -108,27 +111,53 @@ public void filter(Geometry geom) {
}

});
return index;
}

public static Geometry hprTreeBounds(Geometry geoms)
{
HPRtree index = new HPRtree();
loadIndex(geoms, index);
index.build();
Envelope[] bounds = index.getBounds();
Geometry[] polys = new Geometry[bounds.length];
int i = 0;
for (Envelope env : bounds) {
polys[i++] = geoms.getFactory().toGeometry(env);
}
return geoms.getFactory().createGeometryCollection(polys);
}

public static Geometry strTreeQuery(Geometry geoms, Geometry queryEnv)
{
STRtree index = new STRtree();
loadIndex(geoms, index);
// if no query env provided query everything inserted
if (queryEnv == null) queryEnv = geoms;
List result = index.query(queryEnv.getEnvelopeInternal());
return geoms.getFactory().buildGeometry(result);
}

public static Geometry strTreeNN(Geometry geoms, Geometry geom)
{
STRtree index = buildSTRtree(geoms);
STRtree index = new STRtree();
loadIndex(geoms, index);
Object result = index.nearestNeighbour(geom.getEnvelopeInternal(), geom, new GeometryItemDistance());
return (Geometry) result;
}

public static Geometry strTreeNNInSet(Geometry geoms)
{
STRtree index = buildSTRtree(geoms);
STRtree index = new STRtree();
loadIndex(geoms, index);
Object[] result = index.nearestNeighbour(new GeometryItemDistance());
Geometry[] resultGeoms = new Geometry[] { (Geometry) result[0], (Geometry) result[1] };
return geoms.getFactory().createGeometryCollection(resultGeoms);
}

public static Geometry strTreeNNk(Geometry geoms, Geometry geom, int k)
{
STRtree index = buildSTRtree(geoms);
STRtree index = new STRtree();
loadIndex(geoms, index);
Object[] knnObjects = index.nearestNeighbour(geom.getEnvelopeInternal(), geom, new GeometryItemDistance(), k);
List knnGeoms = new ArrayList(Arrays.asList(knnObjects));
Geometry geometryCollection = geoms.getFactory().buildGeometry(knnGeoms);
Expand Down
Loading