Creating RTree Index on LineString Geometry – Detailed Guide

spatial-index

I'm working with NTS(NetTopologySuite), then I get some geometries with type of LineString, and I want to create the IntervalRTree Index on them.

Here are some codes:

SortedPackedIntervalRTree<LineString> tree = new SortedPackedIntervalRTree<LineString>();

and the Insert Method looks like:

public void Insert(double min, double max, T item)

so, how can I get the "min" and "max" Value of LineString?

also, the Query Method of the tree looks like

public void Query(double min, double max, IItemVisitor<T> visitor)

and how to query the tree index?

Best Answer

You can create an STRtree, which is a query-only R-tree created using the Sort-Tile-Recursive (STR) algorithm for two-dimensional spatial data. More details are at the documentation page of JTS.

To create a STRtree index in NTS,

 ISpatialIndex spatialIndex = new STRtree();

You would insert geometries into the index using the geometry's envelope,

 IGeometry geometry = WktReader.Read(//linestring WKT);
 spatialIndex.Insert(geometry.EnvelopeInternal, geometry);

Now you can retrieve the items whos bounds intersect an envelope using,

 IGeometry boundaryGeom = WktReader.Read(//polygon Wkt);
 Envelope envelope = inProcessGeometry.EnvelopeInternal;
 IList<object> intersectingObjects = spatialIndex.Query(envelope);
Related Question