Skip to content

Latest commit

 

History

History
 
 

nitrite-spatial

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Nitrite Spatial Data

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.

How To Install

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'

How To Use

Initialize Spatial Module

Nitrite db = Nitrite.builder()
        .loadModule(new JacksonMapperModule(new GeometryModule()))
        .loadModule(new SpatialModule())
        .openOrCreate();

Create Spatial Index

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");

Query Spatial Data

There are 3 types of spatial filter available in Nitrite Spatial.

Intersect Filter

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));

Within Filter

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));

Near Filter

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));