Nitrite Spatial is an extension of Nitrite database to support spatial data and spatial queries. It uses JTS Topology Suite as the underlying spatial library.
To use this add required dependencies:
Maven
<dependencies>
<dependency>
<groupId>org.dizitart</groupId>
<artifactId>nitrite-spatial</artifactId>
</dependency>
</dependencies>
Gradle
implementation 'org.dizitart:nitrite-spatial'
Nitrite db = Nitrite.builder()
.loadModule(new JacksonMapperModule(new GeometryModule()))
.loadModule(new SpatialModule())
.openOrCreate();
Spatial index can be created on a field of type Geometry
using annotation.
@Data
@Index(fields = "geometry", type = SpatialIndexer.SPATIAL_INDEX)
public class SpatialData {
@Id
private Long id;
private Geometry geometry;
}
It can also be created programmatically.
collection.createIndex(IndexOptions.indexOptions(SpatialIndexer.SPATIAL_INDEX), "location");
There are 3 types of spatial filter available in Nitrite Spatial.
A spatial filter which matches documents where the spatial data of a field intersects the specified Geometry
value.
WKTReader reader = new WKTReader();
Geometry search = reader.read("POLYGON ((490 490, 536 490, 536 515, 490 515, 490 490))");
Cursor<SpatialData> cursor = repository.find(where("geometry").intersects(search));
A spatial filter which matches documents where the spatial data of a field is within the specified Geometry
value.
WKTReader reader = new WKTReader();
Geometry search = reader.read("POLYGON ((490 490, 536 490, 536 515, 490 515, 490 490))");
Cursor<SpatialData> cursor = repository.find(where("geometry").within(search));
A spatial filter which matches documents where the spatial data of a field is near the specified coordinate within a distance.
WKTReader reader = new WKTReader();
Point search = (Point) reader.read("POINT (490 490)");
Cursor<SpatialData> cursor = repository.find(where("geometry").near(search, 20.0));