Geo-Spatial Indexes

ArangoDB features a Google S2 based geospatial index since version 3.4.0, which supersedes the previous geo index implementation. Indexing is supported for a subset of the GeoJSON geometry types as well as simple latitude longitude pairs.

AQL’s geospatial functions and GeoJSON constructors are described in Geo functions.

Using a Geo-Spatial Index

The geospatial index supports containment and intersection queries for various geometric 2D shapes. You should be mainly using AQL queries to perform these types of operations. The index can operate in two different modes, depending on if you want to use the GeoJSON data-format or not. The modes are mainly toggled by using the geoJson field when creating the index.

This index assumes coordinates with the latitude between -90 and 90 degrees and the longitude between -180 and 180 degrees. A geo index will ignore all documents which do not fulfill these requirements.

GeoJSON Mode

To create an index in GeoJSON mode execute:

  1. collection.ensureIndex({ type: "geo", fields: [ "geometry" ], geoJson:true })

This creates the index on all documents and uses geometry as the attributed field where the value is either a Geometry Object or a coordinate array. The array must contain at least two numeric values with longitude (first value) and the latitude (second value). This corresponds to the format described in RFC 7946 Position.

All documents, which do not have the attribute path or have a non-conform value in it, are excluded from the index.

A geo index is implicitly sparse, and there is no way to control its sparsity. In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.

Non-GeoJSON mode

This index mode exclusively supports indexing on coordinate arrays. Values that contain GeoJSON or other types of data will be ignored. In the non-GeoJSON mode the index can be created on one or two fields.

The following examples will work in the arangosh command shell.

To create a geo-spatial index on all documents using latitude and longitude as separate attribute paths, two paths need to be specified in the fields array:

collection.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] })

The first field is always defined to be the latitude and the second is the longitude. The geoJson flag is implicitly false in this mode.

Alternatively you can specify only one field:

collection.ensureIndex({ type: "geo", fields: [ "location" ], geoJson:false })

It creates a geospatial index on all documents using location as the path to the coordinates. The value of the attribute has to be an array with at least two numeric values. The array must contain the latitude (first value) and the longitude (second value).

All documents, which do not have the attribute path(s) or have a non-conforming value in it, are excluded from the index.

A geo index is implicitly sparse, and there is no way to control its sparsity. In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.

In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.

Indexed GeoSpatial Queries

The geospatial index supports a variety of AQL queries, which can be built with the help of the geo utility functions. There are three specific geo functions that can be optimized, provided that they are used correctly: GEO_DISTANCE, GEO_CONTAINS, GEO_INTERSECTS. Additionally, there is a built-in support to optimize the older geo functions DISTANCE, NEAR and WITHIN (the last two only if they are used in their 4 argument version, without distanceName).

When in doubt whether your query is being properly optimized, check the AQL explain output to check for index usage.

Query for Results near Origin (NEAR type query)

A basic example of a query for results near an origin point:

  1. FOR x IN geo_collection
  2. FILTER GEO_DISTANCE([@lng, @lat], x.geometry) <= 100000
  3. RETURN x._key

The first parameter can be a GeoJSON object or a coordinate array in [longitude, latitude] ordering. The second parameter is the document field on which the index was created. The function GEO_DISTANCE always returns the distance in meters, so will receive results up until 100km.

Query for Sorted Results near Origin (NEAR type query)

A basic example of a query for the 1000 nearest results to an origin point (ascending sorting):

  1. FOR x IN geo_collection
  2. SORT GEO_DISTANCE([@lng, @lat], x.geometry) ASC
  3. LIMIT 1000
  4. RETURN x._key

The first parameter can be a GeoJSON object or a coordinate array in [longitude, latitude] ordering. The second parameter is the documents field on which the index was created.

You may also get results farthest away (distance sorted in descending order):

  1. FOR x IN geo_collection
  2. SORT GEO_DISTANCE([@lng, @lat], x.geometry) DESC
  3. LIMIT 1000
  4. RETURN x._key

Query for Results within Distance

A query which returns documents at a distance of 1km or farther away, up to 100km from the origin. This will return the documents with a GeoJSON value that is located in the specified search annulus.

  1. FOR x IN geo_collection
  2. FILTER GEO_DISTANCE([@lng, @lat], x.geometry) <= 100000
  3. FILTER GEO_DISTANCE([@lng, @lat], x.geometry) >= 1000
  4. RETURN x

Query for Results contained in Polygon

A query which returns documents whose stored geometry is contained within a GeoJSON Polygon.

  1. LET polygon = GEO_POLYGON([[[60,35],[50,5],[75,10],[70,35]]])
  2. FOR x IN geo_collection
  3. FILTER GEO_CONTAINS(polygon, x.geometry)
  4. RETURN x

The first parameter of GEO_CONTAINS must be a polygon. Other types are not valid. The second parameter must contain the document field on which the index was created.

Query for Results Intersecting a Polygon

A query which returns documents with an intersection of their stored geometry and a GeoJSON Polygon.

  1. LET polygon = GEO_POLYGON([[[60,35],[50,5],[75,10],[70,35]]])
  2. FOR x IN geo_collection
  3. FILTER GEO_INTERSECTS(polygon, x.geometry)
  4. RETURN x

The first parameter of GEO_INTERSECTS must be a polygon. Other types are not valid. The second parameter must contain the document field on which the index was created.

GeoJSON

GeoJSON is a geospatial data format based on JSON. It defines several different types of JSON objects and the way in which they can be combined to represent data about geographic shapes on the earth surface. GeoJSON uses a geographic coordinate reference system, World Geodetic System 1984 (WGS 84), and units of decimal degrees.

Internally ArangoDB maps all coordinates onto a unit sphere. Distances are projected onto a sphere with the Earth’s Volumetric mean radius of 6371 km. ArangoDB implements a useful subset of the GeoJSON format (RFC 7946). Feature Objects and the GeometryCollection type are not supported. Supported geometry object types are:

  • Point
  • MultiPoint
  • LineString
  • MultiLineString
  • Polygon
  • MultiPolygon

Point

A GeoJSON Point is a position comprised of a longitude and a latitude:

  1. {
  2. "type": "Point",
  3. "coordinates": [100.0, 0.0]
  4. }

MultiPoint

A GeoJSON MultiPoint is an array of positions:

  1. {
  2. "type": "MultiPoint",
  3. "coordinates": [
  4. [100.0, 0.0],
  5. [101.0, 1.0]
  6. ]
  7. }

LineString

A GeoJSON LineString is an array of two or more positions:

  1. {
  2. "type": "LineString",
  3. "coordinates": [
  4. [100.0, 0.0],
  5. [101.0, 1.0]
  6. ]
  7. }

MultiLineString

A GeoJSON MultiLineString is an array of LineString coordinate arrays:

  1. {
  2. "type": "MultiLineString",
  3. "coordinates": [
  4. [
  5. [100.0, 0.0],
  6. [101.0, 1.0]
  7. ],
  8. [
  9. [102.0, 2.0],
  10. [103.0, 3.0]
  11. ]
  12. ]
  13. }

Polygon

A GeoJSON Polygon consists of a series of closed LineString objects (ring-like). These Linear Ring objects consist of four or more vertices with the first and last coordinate pairs being equal. Coordinates of a Polygon are an array of linear ring coordinate arrays. The first element in the array represents the exterior ring. Any subsequent elements represent interior rings (holes within the surface).

  • A linear ring may not be empty, it needs at least three distinct coordinates
  • Within the same linear ring consecutive coordinates may be the same, otherwise (except the first and last one) all coordinates need to be distinct
  • A linear ring defines two regions on the sphere. ArangoDB will always interpret the region of smaller area to be the interior of the ring. This introduces a practical limitation that no polygon may have an outer ring enclosing more than half the Earth’s surface

No Holes:

  1. {
  2. "type": "Polygon",
  3. "coordinates": [
  4. [
  5. [100.0, 0.0],
  6. [101.0, 0.0],
  7. [101.0, 1.0],
  8. [100.0, 1.0],
  9. [100.0, 0.0]
  10. ]
  11. ]
  12. }

With Holes:

  • The exterior ring should not self-intersect.
  • The interior rings must be contained in the outer ring
  • No two rings can cross each other, i.e. no ring may intersect both the interior and exterior face of another ring
  • Rings cannot share edges, they may however share vertices
  • No ring may be empty
  • Polygon rings should follow the right-hand rule for orientation (counterclockwise external rings, clockwise internal rings).
  1. {
  2. "type": "Polygon",
  3. "coordinates": [
  4. [
  5. [100.0, 0.0],
  6. [101.0, 0.0],
  7. [101.0, 1.0],
  8. [100.0, 1.0],
  9. [100.0, 0.0]
  10. ],
  11. [
  12. [100.8, 0.8],
  13. [100.8, 0.2],
  14. [100.2, 0.2],
  15. [100.2, 0.8],
  16. [100.8, 0.8]
  17. ]
  18. ]
  19. }

MultiPolygon

A GeoJSON MultiPolygon consists of multiple polygons. The “coordinates” member is an array of Polygon coordinate arrays.

  • Polygons in the same MultiPolygon may not share edges, they may share coordinates
  • Polygons and rings must not be empty
  • A linear ring defines two regions on the sphere. ArangoDB will always interpret the region of smaller area to be the interior of the ring. This introduces a practical limitation that no polygon may have an outer ring enclosing more than half the Earth’s surface
  • Linear rings must follow the right-hand rule for orientation (counterclockwise external rings, clockwise internal rings).

Example with two polygons, the second one with a hole:

  1. {
  2. "type": "MultiPolygon",
  3. "coordinates": [
  4. [
  5. [
  6. [102.0, 2.0],
  7. [103.0, 2.0],
  8. [103.0, 3.0],
  9. [102.0, 3.0],
  10. [102.0, 2.0]
  11. ]
  12. ],
  13. [
  14. [
  15. [100.0, 0.0],
  16. [101.0, 0.0],
  17. [101.0, 1.0],
  18. [100.0, 1.0],
  19. [100.0, 0.0]
  20. ],
  21. [
  22. [100.2, 0.2],
  23. [100.2, 0.8],
  24. [100.8, 0.8],
  25. [100.8, 0.2],
  26. [100.2, 0.2]
  27. ]
  28. ]
  29. ]
  30. }

arangosh Examples

ensures that a geo index exists collection.ensureIndex({ type: "geo", fields: [ "location" ] })

Creates a geospatial index on all documents using location as the path to the coordinates. The value of the attribute has to be an array with at least two numeric values. The array must contain the latitude (first value) and the longitude (second value).

All documents, which do not have the attribute path or have a non-conforming value in it, are excluded from the index.

A geo index is implicitly sparse, and there is no way to control its sparsity.

The index does not provide a unique option because of its limited usability. It would prevent identical coordinates from being inserted only, but even a slightly different location (like 1 inch or 1 cm off) would be unique again and not considered a duplicate, although it probably should. The desired threshold for detecting duplicates may vary for every project (including how to calculate the distance even) and needs to be implemented on the application layer as needed. You can write a Foxx service for this purpose and make use of the AQL geo functions to find nearby coordinates supported by a geo index.

In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.

To create a geo index on an array attribute that contains longitude first, set the geoJson attribute to true. This corresponds to the format described in RFC 7946 Position

collection.ensureIndex({ type: "geo", fields: [ "location" ], geoJson: true })

To create a geo-spatial index on all documents using latitude and longitude as separate attribute paths, two paths need to be specified in the fields array:

collection.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] })

In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.

Examples

Create a geo index for an array attribute:

  1. arangosh> db.geo.ensureIndex({ type: "geo", fields: [ "loc" ] });

Show execution results

Hide execution results

  1. {
  2. "bestIndexedLevel" : 17,
  3. "fields" : [
  4. "loc"
  5. ],
  6. "geoJson" : false,
  7. "id" : "geo/76775",
  8. "isNewlyCreated" : true,
  9. "maxNumCoverCells" : 8,
  10. "name" : "idx_1707084125476749312",
  11. "sparse" : true,
  12. "type" : "geo",
  13. "unique" : false,
  14. "worstIndexedLevel" : 4,
  15. "code" : 201
  16. }

Create a geo index for an array attribute:

  1. arangosh> db.geo2.ensureIndex({ type: "geo", fields: [ "location.latitude", "location.longitude" ] });

Show execution results

Hide execution results

  1. {
  2. "bestIndexedLevel" : 17,
  3. "fields" : [
  4. "location.latitude",
  5. "location.longitude"
  6. ],
  7. "geoJson" : false,
  8. "id" : "geo2/76786",
  9. "isNewlyCreated" : true,
  10. "maxNumCoverCells" : 8,
  11. "name" : "idx_1707084125479895041",
  12. "sparse" : true,
  13. "type" : "geo",
  14. "unique" : false,
  15. "worstIndexedLevel" : 4,
  16. "code" : 201
  17. }

Use geo index with AQL SORT statement:

  1. arangosh> db.geoSort.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] });
  2. arangosh> for (i = -90; i <= 90; i += 10) {
  3. ........> for (j = -180; j <= 180; j += 10) {
  4. ........> db.geoSort.save({ name : "Name/" + i + "/" + j, latitude : i, longitude : j });
  5. ........> }
  6. ........> }
  7. arangosh> var query = "FOR doc in geoSort SORT DISTANCE(doc.latitude, doc.longitude, 0, 0) LIMIT 5 RETURN doc"
  8. arangosh> db._explain(query, {}, {colors: false});

Show execution results

Hide execution results

  1. {
  2. "bestIndexedLevel" : 17,
  3. "fields" : [
  4. "latitude",
  5. "longitude"
  6. ],
  7. "geoJson" : false,
  8. "id" : "geoSort/78218",
  9. "isNewlyCreated" : true,
  10. "maxNumCoverCells" : 8,
  11. "name" : "idx_1707084125733650432",
  12. "sparse" : true,
  13. "type" : "geo",
  14. "unique" : false,
  15. "worstIndexedLevel" : 4,
  16. "code" : 201
  17. }
  18. Query String (86 chars, cacheable: true):
  19. FOR doc in geoSort SORT DISTANCE(doc.latitude, doc.longitude, 0, 0) LIMIT 5 RETURN doc
  20. Execution plan:
  21. Id NodeType Est. Comment
  22. 1 SingletonNode 1 * ROOT
  23. 7 IndexNode 703 - FOR doc IN geoSort /* geo index scan */
  24. 5 LimitNode 5 - LIMIT 0, 5
  25. 6 ReturnNode 5 - RETURN doc
  26. Indexes used:
  27. By Name Type Collection Unique Sparse Selectivity Fields Ranges
  28. 7 idx_1707084125733650432 geo geoSort false true n/a [ `latitude`, `longitude` ] (GEO_DISTANCE([ 0, 0 ], [ doc.`longitude`, doc.`latitude` ]) < "unlimited")
  29. Optimization rules applied:
  30. Id RuleName
  31. 1 geo-index-optimizer
  32. 2 remove-unnecessary-calculations-2
  33. Optimization rules with highest execution times:
  34. RuleName Duration [s]
  35. geo-index-optimizer 0.00002
  36. reduce-extraction-to-projection 0.00000
  37. replace-function-with-index 0.00000
  38. use-indexes 0.00000
  39. remove-unnecessary-calculations-2 0.00000
  40. 41 rule(s) executed, 1 plan(s) created
  41. arangosh> db._query(query);
  42. [
  43. {
  44. "_key" : "78924",
  45. "_id" : "geoSort/78924",
  46. "_rev" : "_cuv9dI2--A",
  47. "name" : "Name/0/0",
  48. "latitude" : 0,
  49. "longitude" : 0
  50. },
  51. {
  52. "_key" : "78998",
  53. "_id" : "geoSort/78998",
  54. "_rev" : "_cuv9dJq--A",
  55. "name" : "Name/10/0",
  56. "latitude" : 10,
  57. "longitude" : 0
  58. },
  59. {
  60. "_key" : "78926",
  61. "_id" : "geoSort/78926",
  62. "_rev" : "_cuv9dI6---",
  63. "name" : "Name/0/10",
  64. "latitude" : 0,
  65. "longitude" : 10
  66. },
  67. {
  68. "_key" : "78850",
  69. "_id" : "geoSort/78850",
  70. "_rev" : "_cuv9dI---_",
  71. "name" : "Name/-10/0",
  72. "latitude" : -10,
  73. "longitude" : 0
  74. },
  75. {
  76. "_key" : "78922",
  77. "_id" : "geoSort/78922",
  78. "_rev" : "_cuv9dI2--_",
  79. "name" : "Name/0/-10",
  80. "latitude" : 0,
  81. "longitude" : -10
  82. }
  83. ]
  84. [object ArangoQueryCursor, count: 5, cached: false, hasMore: false]

Use geo index with AQL FILTER statement:

  1. arangosh> db.geoFilter.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] });
  2. arangosh> for (i = -90; i <= 90; i += 10) {
  3. ........> for (j = -180; j <= 180; j += 10) {
  4. ........> db.geoFilter.save({ name : "Name/" + i + "/" + j, latitude : i, longitude : j });
  5. ........> }
  6. ........> }
  7. arangosh> var query = "FOR doc in geoFilter FILTER DISTANCE(doc.latitude, doc.longitude, 0, 0) < 2000 RETURN doc"
  8. arangosh> db._explain(query, {}, {colors: false});

Show execution results

Hide execution results

  1. {
  2. "bestIndexedLevel" : 17,
  3. "fields" : [
  4. "latitude",
  5. "longitude"
  6. ],
  7. "geoJson" : false,
  8. "id" : "geoFilter/76797",
  9. "isNewlyCreated" : true,
  10. "maxNumCoverCells" : 8,
  11. "name" : "idx_1707084125485137920",
  12. "sparse" : true,
  13. "type" : "geo",
  14. "unique" : false,
  15. "worstIndexedLevel" : 4,
  16. "code" : 201
  17. }
  18. Query String (89 chars, cacheable: true):
  19. FOR doc in geoFilter FILTER DISTANCE(doc.latitude, doc.longitude, 0, 0) < 2000 RETURN doc
  20. Execution plan:
  21. Id NodeType Est. Comment
  22. 1 SingletonNode 1 * ROOT
  23. 6 IndexNode 703 - FOR doc IN geoFilter /* geo index scan */
  24. 5 ReturnNode 703 - RETURN doc
  25. Indexes used:
  26. By Name Type Collection Unique Sparse Selectivity Fields Ranges
  27. 6 idx_1707084125485137920 geo geoFilter false true n/a [ `latitude`, `longitude` ] (GEO_DISTANCE([ 0, 0 ], [ doc.`longitude`, doc.`latitude` ]) < 2000)
  28. Optimization rules applied:
  29. Id RuleName
  30. 1 geo-index-optimizer
  31. 2 remove-unnecessary-calculations-2
  32. Optimization rules with highest execution times:
  33. RuleName Duration [s]
  34. geo-index-optimizer 0.00002
  35. reduce-extraction-to-projection 0.00000
  36. replace-function-with-index 0.00000
  37. use-indexes 0.00000
  38. optimize-subqueries 0.00000
  39. 41 rule(s) executed, 1 plan(s) created
  40. arangosh> db._query(query);
  41. [
  42. {
  43. "_key" : "77503",
  44. "_id" : "geoFilter/77503",
  45. "_rev" : "_cuv9c6K---",
  46. "name" : "Name/0/0",
  47. "latitude" : 0,
  48. "longitude" : 0
  49. }
  50. ]
  51. [object ArangoQueryCursor, count: 1, cached: false, hasMore: false]