Skip to main content
Version: 2.x(alpha)

Geometries

SurrealDB makes working with GeoJSON easy, with support for Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and Collection values. SurrealQL automatically detects GeoJSON objects converting them into a single data type.

TypeDescription
PointA geolocation point with latitude and longitude
LineStringA GeoJSON LineString value for storing a geometric path
PolygonA GeoJSON Polygon value for storing a geometric area
MultiPointA value which contains multiple geometry points
MultiLineStringA value which contains multiple geometry lines
MultiPolygonA value which contains multiple geometry polygons
CollectionA value which contains multiple different geometry types

Point

The simplest form of GeoJSON that SurrealDB supports is a geolocation point. These can be written using two different formats. The first format is a simple 2-element tuple (longitude, latitude).

UPDATE city:london SET centre = (-0.118092, 51.509865);

In addition, SurrealDB supports entering GeoJSON points using the traditional format.

INFO: No other properties must be present in the Point object.

UPDATE city:london SET centre = {
type: "Point",
coordinates: [-0.118092, 51.509865],
};

LineString

A GeoJSON LineString value for storing a geometric path.

INFO: No other properties must be present in the LineString object.

UPDATE city:london SET distance = {
type: "LineString",
coordinates: [[-0.118092, 51.509865],[0.1785278, 51.37692386]],
};

Polygon

A GeoJSON Polygon value for storing a geometric area.

INFO: No other properties must be present in the Polygon object.

UPDATE city:london SET boundary = {
type: "Polygon",
coordinates: [[
[-0.38314819, 51.37692386], [0.1785278, 51.37692386],
[0.1785278, 51.61460570], [-0.38314819, 51.61460570],
[-0.38314819, 51.37692386]
]]
};

MultiPoint

MultiPoints can be used to store multiple geometry points in a single value.

INFO: No other properties must be present in the MultiPoint object.

UPDATE person:tobie SET locations = {
type: "MultiPoint",
coordinates: [
[10.0, 11.2],
[10.5, 11.9]
],
};

MultiLineString

A MultiLineString can be used to store multiple geometry lines in a single value.

INFO: No other properties must be present in the MultiLineString object.

UPDATE travel:yellowstone SET routes = {
type: "MultiLineString",
coordinates: [
[ [10.0, 11.2], [10.5, 11.9] ],
[ [11.0, 12.2], [11.5, 12.9], [12.0, 13.0] ]
]
}

MultiPolygon

MultiPolygons can be used to store multiple geometry polygons in a single value.

INFO: No other properties must be present in the MultiPolygon object.

UPDATE university:oxford SET locations = {
type: "MultiPolygon",
coordinates: [
[
[ [10.0, 11.2], [10.5, 11.9], [10.8, 12.0], [10.0, 11.2] ]
],
[
[ [9.0, 11.2], [10.5, 11.9], [10.3, 13.0], [9.0, 11.2] ]
]
]
};

Collection

Collections can be used to store multiple different geometry types in a single value.

INFO: No other properties must be present in the Collection object.

UPDATE university:oxford SET buildings = {
type: "GeometryCollection",
geometries: [
{
type: "MultiPoint",
coordinates: [
[10.0, 11.2],
[10.5, 11.9]
],
},
{
type: "Polygon",
coordinates: [[
[-0.38314819, 51.37692386], [0.1785278, 51.37692386],
[0.1785278, 51.61460570], [-0.38314819, 51.61460570],
[-0.38314819, 51.37692386]
]]
},
{
type: "MultiPolygon",
coordinates: [
[
[ [10.0, 11.2], [10.5, 11.9], [10.8, 12.0], [10.0, 11.2] ]
],
[
[ [9.0, 11.2], [10.5, 11.9], [10.3, 13.0], [9.0, 11.2] ]
]
]
}
]
};

Next steps

You've now seen how to use geometries to store locations, paths, and polygonal areas in SurrealDB. For more advanced functionality, take a look at the operators and geo functions, which enable area, distance, and bearing geometric calculations, and the ability to detect whether geometries contain or intersect other geometry types.