DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • How To Scan and Validate Image Uploads in Java
  • Implementing RBAC in Quarkus
  • Mainframe Development for the "No Mainframe" Generation
  • How To Become a 10x Dev: An Essential Guide
  1. DZone
  2. Data Engineering
  3. Databases
  4. Geospatial Queries With Apache Ignite

Geospatial Queries With Apache Ignite

Storing and querying location data can be useful for any number of apps for projects. Apache Ignite has a geospatial component made just for that.

Denis Magda user avatar by
Denis Magda
CORE ·
Dec. 16, 16 · Tutorial
Like (8)
Save
Tweet
Share
9.15K Views

Join the DZone community and get the full member experience.

Join For Free

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. 

Apache Ignite Database

Published at DZone with permission of Denis Magda. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How To Scan and Validate Image Uploads in Java
  • Implementing RBAC in Quarkus
  • Mainframe Development for the "No Mainframe" Generation
  • How To Become a 10x Dev: An Essential Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: