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.
| Type | Description |
|---|---|
Point | A geolocation point with longitude and latitude |
LineString | A GeoJSON LineString value for storing a geometric path |
Polygon | A GeoJSON Polygon value for storing a geometric area |
MultiPoint | A value which contains multiple geometry points |
MultiLineString | A value which contains multiple geometry lines |
MultiPolygon | A value which contains multiple geometry polygons |
Collection | A value which contains multiple different geometry types |
The GeoJSON spec
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
Pointis in the order(longitude, latitude), and not the other way around.A
geometrycreated from an object must contain atypefield and acoordinatesfield, no more and no less.
This can be shown by calling the type::is_geometry() function on some sample objects.
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 that of an object that matches the GeoJSON spec.
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.
LineString
A GeoJSON LineString value for storing a geometric path.
Polygon
A GeoJSON Polygon value for storing a geometric area.
MultiPoint
MultiPoints can be used to store multiple geometry points in a single value.
MultiLineString
A MultiLineString can be used to store multiple geometry lines in a single value.
MultiPolygon
MultiPolygons can be used to store multiple geometry polygons in a single value.
Collection
Collections can be used to store multiple different geometry types in a single value.
Example
The following example includes five records from an open database with cities worldwide that have of a population of at least 1000. The queries below create a city record from each entry that includes their name, location, and name. Next, it uses the geo::distance function to find their two closest neighbours, relating them via the close_to relation table. The final query can be viewed in traditional form to see each city's neighbours, or on Surrealist's graph view to see a visual representation of the network of closely linked cities.
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.