Geometries

A geometry is a type based on the GeoJSON spec that is optimised for working with data pertaining to locations on Earth.

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 longitude and latitude
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

There are two main points to keep in mind when creating a geometry type in SurrealDB. They are:

  • Points are defined according to the GeoJSON spec, which specificies longitude before latitude. Many sites - including Google Maps - provide location data in the opposite order, so be sure to confirm that any data being used to create a Point is in the order (longitude, latitude), and not the other way around.

  • A geometry created from an object must contain a type field and a coordinates field, no more and no less.

This can be shown by calling the type::is_geometry() function on some sample objects.

-- true: has `type` and `coordinates` field with valid data
{ type: "Point", coordinates: [-0.118092, 51.509865] }.is_geometry();

-- false: lacks 'type' field
{ coordinates: [-0.118092, 51.509865] }.is_geometry();

-- false: has an extra field
{ type: "Point", coordinates: [-0.118092, 51.509865], unnecessary: "data" }.is_geometry();

The simplest form of GeoJSON that SurrealDB supports is a geolocation point. These can be written using two different formats. The first format is that of an object that matches the GeoJSON spec.

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

The other format is a simple 2-element tuple containing the longitude and the latitude of a location. This output for this format is no different from the above, and is simply a convenience due to the frequency of use of the Point type.

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


A GeoJSON LineString value for storing a geometric path.

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


A GeoJSON Polygon value for storing a geometric area.

CREATE 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]
]]
};


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

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


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

CREATE 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] ]
]
}


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

CREATE 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] ]
]
]
};


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

CREATE 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] ]
]
]
}
]
};


Was this page helpful?