GeoJSON tutorial

GeoJSON tutorial

Motivation

Starting with the mass-market availability of smartphones and continuing with IoT devices, self-driving cars ever more data is generated with geo information attached to it. Analyzing this data in real-time requires the use of clever indexing data-structures.

Up to version 3.3, ArangoDB supported a basic subset of geospatial functions, which were able to use an existent geo index:

  • NEAR(): Return documents which a are near to a given tuple coordinate pair
  • WITHIN(): Return documents which are located inside a specified circle.
  • WITHIN_RECTANGLE(): Return documents which are located inside a specified rectangle.

Calculating e.g. the distance between two coordinate tuples or checking whether a coordinate pair is located inside a polygon was possible, but those functions could not benefit by using the geo index optimizations. Those operations need to be as fast as possible to prevent them from being a show stopper.

Of course, speed is not everything, so we also want to provide a broader set of geo functionality by integrating full GeoJSON support including Polygons, Multi-Polygons and other geometry primitives.

With these functionalities, one can do more complex queries and build e.g. location-aware recommendation engines by combining the graph data model with geo-location aspects or use multiple data models. For instance, in the age of self-driving cars, one can find the nearest available maintenance team (geo query) with the right permission (graph model) to repair a given problem (sent automatically to the DB as e.g. a JSON document or key/value pair).

Full GeoJSON Support in ArangoDB 3.4

ArangoDB 3.4 includes the new GeoJSON functionalities based on Google’s S2 geospatial index which will replace the old geospatial index without dropping support of our old geospatial functions or index. We support indexing on a subset of the GeoJSON standard, as well as simple latitude-longitude pairs (Non-GeoJSON mode). This article will also introduce new geo functions and geo constructors. Also, the AQL Editor got an improvement to render GeoJSON based data directly with OpenStreetMap.

Improved AQL Editor

While boosting up ArangoDB’s backend and working with several geo use-cases during development, we
noticed that something was missing. Even if everything was working as expected and our tests were green, it felt not like this whole geo feature is complete. Restaurants, Districts, Streets etc. are all locations based on our earth’s surface. So you want to see them! That is the reason we also implemented a new layer inside the WebUI’s AQL Editor which is detecting if your queries arreturningng GeoJSON objects. And if that is the case, we’ll show them!

GeoJSON

GeoJSON is an open standard format for geographical features or data structures including properties and their spatial extends based on JSON. Back in the days it started as a community project by developers, but today it is fully specified and released by the IETF (RFC7946). GeoJSON uses a geographic coordinate reference system (WGS84) and units of decimal degrees for representing positions on earth with their longitude and latitude values.

ArangoDB currently supports Point, MultiPoint, LineString, MultiLineString, Polygon and MultiPolygon as GeoJSON types.
A GeoJSON representation of a Point:

Where 10.0 stands for the longitude value and 20.0 for the latitude value.

GeoJSON Supported Index

To create an geospatial index, which supports GeoJSON, on a collection named restaurants, use this command:

The newly created index is expecting a valid GeoJSON object in each document inside the location property. A valid document would look like this:

Invalid documents will be ignored. You are also able to create indices via the Web UI.

Added AQL Features

This chapter will introduce the new GeoJSON constructors (helpers) and geospatial functions, which actually will gain optimization through the geo index.

Constructors

To keep things simple, ArangoDB offers some AQL constructor functions to create those types easily:

will return above’s GeoJSON Point representation. Whereas the GEO_POLYGON function

will create the following GeoJSON output (representing a simplified shape around Germany):

Available constructors are:

  • GEO_POINT()
  • GEO_LINESTRING()
  • GEO_MULTILINESTRING()
  • GEO_POLYGON()
  • GEO_MULTIPOLYGON()

To get all usage information, please refer to the documentation.

Functions

With ArangoDB 3.4+ we’ve included new geo functions, which are much more powerful as in our previous releases. This chapter introduces each function with a small example snippet:

GEO_DISTANCE()

Return all restaurants with a maximum distance of 30km starting from the Statue of Liberty.

Return all restaurants with a distance between 25km and 30km starting from the Statue of Liberty.

Return 100 nearest restaurants sorted by their distance starting from the Statue of Liberty.

GEO_CONTAINS()

First get the document representing the district “Chinatown”. Then iterate and filter trough all available restaurants and only show those, which are located inside “Chinatown”.

GEO_INTERSECTS()

Define some area in NYC which potentially covers more neighborhoods. Then return all neighborhoods which will intersect with the defined area (Polygon).

GEO_EQUALS()

This function checks whether GeoJSON objects are equal or not.

Try it out

This blog post uses a dataset of restaurants and neighbourhoods in New York City. To run all examples, create two collections called neighborhoods and code>restaurants and import the dataset with arangoimp or directly via ArangoDB’s WebUI.
The dataset can be found here:

It contains two JSON files. The neighborhood dataset consists of GeoJSON specified Polygons representing districts in NYC (~200 entries). The restaurant dataset contains restaurants located in NYC which are stored as GeoJSON Points (~20k entries).
Import with arangoimp:

Feel free to use the given dataset and queries to play around.

Updated Geo Foxx Service

If you would like to take a look into Foxx and a basic geo example, feel free to download and examine our updated Geo Foxx Service here:

Do you like ArangoDB?
icon-githubStar this project on GitHub.
close-link