Geospatial Queries with Apache® Ignite™

Nowadays, there are tons of applications, services and use cases when it's needed to gather, store and process spatial data constantly. Generally speaking, when we talk about geospatial data, we imply location or dimension of an object like a building, mountain, car or group of people. Applications and services like Foursquare and Google Maps or solutions built for logistics and delivery planning have to deal with spatial data all the time which means that they have to store and process this data efficiently.

There are a variety of conventional databases that are suitable for geospatial scenarios. Not many in-memory platforms provide geospatial support, though. However, the Apache Ignite In-Memory Data Fabric not only allows storing, maintaining and processing spatial data but does it in a distributed and fault-tolerant fashion across a cluster of machines.

This blog post will demonstrate how to create a simple application that stores geographical points in an Apache Ignite cluster and use Ignite's API to query this data. 

Apache Ignite Geospatial Component

The capabilities of spatial queries, as well as available functions and operands, are defined in Apache Ignite by Simple Features Specification for SQL. The specification is fully implemented by JTS Topology Suite which is used by Apache Ignite along with H2 to build the unique geospatial component that works in a distributed and fault-tolerant fashion.

To start using the geospatial component, you have to add Apache Ignite's geospatial module as a Maven dependency into your project along with other required Apache Ignite libraries


<dependency>
  <groupId>org.apache.ignite</groupId>
  <artifactId>ignite-geospatial</artifactId>
  <version>${ignite.version}</version>
</dependency>

However, since ignite-geospatial relies on JTS Topology Suites, which is available under the LGPL license, it is not possible to host ignite-geospatial on Maven Central. The module is hosted in the repository shown below and must be added to your Maven configuration.


<repositories>
  <repository>
    <id>GridGain External Repository</id>
    <url>http://www.gridgainsystems.com/nexus/content/repositories/external</url>
  </repository>
</repositories>

Storing and Querying Spatial Data

As a part of the example, let's create a geographical point object that will be defined by its coordinates and additional parameters stored inside of com.vividsolutions.jts.geom.Geometry field. Note that in Apache Ignite, geospatial querying and indexing capabilities are applicable only for the objects of com.vividsolutions.jts types.
 


private static class MapPoint {
    /** Coordinates. */
    @QuerySqlField(index = true)
    private Geometry coords;

    /**
     * @param coords Coordinates.
     */
    private MapPoint(Geometry coords) {
        this.coords = coords;
    }
}

The coords field of the object will be treated by Apache Ignite as an index field which will boost the performance when the field's value will be used by geospatial queries and functions. To learn more about how to configure indexes in Apache Ignite, please refer to the following documentation page.

Now, let's fill out an Ignite cache with random MapPoint objects.


Random rnd = new Random();

WKTReader r = new WKTReader();

// Adding geometry points into the cache.
for (int i = 0; i < 1000; i++) {
    int x = rnd.nextInt(10000);
    int y = rnd.nextInt(10000);

    Geometry geo = r.read("POINT(" + x + " " + y + ")");

    cache.put(i, new MapPoint(geo));
}

 And find out how many of them fit a specific region.


// Query to find points that fit into a polygon.
SqlQuery<Integer, MapPoint> query = new SqlQuery<>(MapPoint.class, "coords && ?");

// Defining the polygon's boundaries.
query.setArgs("POLYGON((0 0, 0 99, 400 500, 300 0, 0 0))");

// Executing the query.
Collection<Cache.Entry<Integer, MapPoint>> entries = cache.query(query).getAll();

// Printing number of points that fit into the area defined by the polygon.
System.out.println("Fetched points [" + entries.size() + ']');

 

That's it. This is all you need to know if you are planning to work with spatial data in Apache Ignite. The complete example is available on GitHub.