Geometry Types

The SDK provides GeoJSON-compatible geometry types for working with SurrealDB's spatial data. All geometry classes extend the Geometry base class.

Import

from surrealdb import (
GeometryPoint,
GeometryLine,
GeometryPolygon,
GeometryMultiPoint,
GeometryMultiLine,
GeometryMultiPolygon,
GeometryCollection,
)

Individual types can also be imported from surrealdb.data.types.geometry.

A single geographic point defined by longitude and latitude.

Syntax

GeometryPoint(longitude, latitude)
ParameterTypeDescription
longitude floatThe longitude coordinate.
latitude floatThe latitude coordinate.
point = GeometryPoint(-0.1278, 51.5074)

A line defined by two or more points.

Syntax

GeometryLine(points)
ParameterTypeDescription
points list[GeometryPoint]An ordered list of points that define the line.
line = GeometryLine([
GeometryPoint(-0.1278, 51.5074),
GeometryPoint(-3.1883, 55.9533),
])

A polygon defined by one or more linear rings. The first ring is the exterior boundary; any subsequent rings are interior holes. Rings must be closed — the first and last point must be identical, following the GeoJSON specification.

Syntax

GeometryPolygon(rings)
ParameterTypeDescription
rings list[GeometryLine]A list of linear rings. The first is the exterior ring; others are holes.
polygon = GeometryPolygon([
GeometryLine([
GeometryPoint(0.0, 0.0),
GeometryPoint(1.0, 0.0),
GeometryPoint(1.0, 1.0),
GeometryPoint(0.0, 1.0),
GeometryPoint(0.0, 0.0),
]),
])

A collection of points.

Syntax

GeometryMultiPoint(points)
ParameterTypeDescription
points list[GeometryPoint]A list of points.
multi_point = GeometryMultiPoint([
GeometryPoint(-0.1278, 51.5074),
GeometryPoint(-3.1883, 55.9533),
GeometryPoint(-1.8904, 52.4862),
])

A collection of lines.

Syntax

GeometryMultiLine(lines)
ParameterTypeDescription
lines list[GeometryLine]A list of lines.
multi_line = GeometryMultiLine([
GeometryLine([
GeometryPoint(0.0, 0.0),
GeometryPoint(1.0, 1.0),
]),
GeometryLine([
GeometryPoint(2.0, 2.0),
GeometryPoint(3.0, 3.0),
]),
])

A collection of polygons.

Syntax

GeometryMultiPolygon(polygons)
ParameterTypeDescription
polygons list[GeometryPolygon]A list of polygons.
multi_polygon = GeometryMultiPolygon([
GeometryPolygon([
GeometryLine([
GeometryPoint(0.0, 0.0),
GeometryPoint(1.0, 0.0),
GeometryPoint(1.0, 1.0),
GeometryPoint(0.0, 1.0),
GeometryPoint(0.0, 0.0),
]),
]),
])

A heterogeneous collection of geometry objects. Unlike the other multi-types, a GeometryCollection can contain a mix of different geometry types.

Syntax

GeometryCollection(geometries)
ParameterTypeDescription
geometries list[Geometry]A list of geometry objects of any type.
collection = GeometryCollection([
GeometryPoint(-0.1278, 51.5074),
GeometryLine([
GeometryPoint(0.0, 0.0),
GeometryPoint(1.0, 1.0),
]),
])
from surrealdb import Surreal, GeometryPoint

db = Surreal("ws://localhost:8000")
db.connect()
db.use("my_ns", "my_db")
db.signin({"username": "root", "password": "root"})

db.create("locations", {
"name": "London",
"coordinates": GeometryPoint(-0.1278, 51.5074),
})

result = db.query("""
SELECT * FROM locations
WHERE geo::distance(coordinates, $point) < 50000
""", {
"point": GeometryPoint(-0.1180, 51.5099),
})

Was this page helpful?