The SDK provides GeoJSON-compatible geometry types for working with SurrealDB's spatial data. All geometry classes extend the Geometry base class.
from surrealdb import (
GeometryPoint,
GeometryLine,
GeometryPolygon,
GeometryMultiPoint,
GeometryMultiLine,
GeometryMultiPolygon,
GeometryCollection,
)Individual types can also be imported from surrealdb.data.types.geometry.
GeometryPoint
A single geographic point defined by longitude and latitude.
Constructor
GeometryPoint(longitude, latitude)Parameter | Type | Description |
|---|---|---|
longitude | float | The longitude coordinate. |
latitude | float | The latitude coordinate. |
Examples
point = GeometryPoint(-0.1278, 51.5074) GeometryLine
A line defined by two or more points.
Constructor
GeometryLine(points)Parameter | Type | Description |
|---|---|---|
points | list[GeometryPoint] | An ordered list of points that define the line. |
Examples
line = GeometryLine([
GeometryPoint(-0.1278, 51.5074),
GeometryPoint(-3.1883, 55.9533),
]) GeometryPolygon
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.
Constructor
GeometryPolygon(rings)Parameter | Type | Description |
|---|---|---|
rings | list[GeometryLine] | A list of linear rings. The first is the exterior ring; others are holes. |
Examples
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),
]),
]) GeometryMultiPoint
A collection of points.
Constructor
GeometryMultiPoint(points)Parameter | Type | Description |
|---|---|---|
points | list[GeometryPoint] | A list of points. |
Examples
multi_point = GeometryMultiPoint([
GeometryPoint(-0.1278, 51.5074),
GeometryPoint(-3.1883, 55.9533),
GeometryPoint(-1.8904, 52.4862),
]) GeometryMultiLine
A collection of lines.
Constructor
GeometryMultiLine(lines)Parameter | Type | Description |
|---|---|---|
lines | list[GeometryLine] | A list of lines. |
Examples
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),
]),
]) GeometryMultiPolygon
A collection of polygons.
Constructor
GeometryMultiPolygon(polygons)Parameter | Type | Description |
|---|---|---|
polygons | list[GeometryPolygon] | A list of polygons. |
Examples
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),
]),
]),
]) GeometryCollection
A heterogeneous collection of geometry objects. Unlike the other multi-types, a GeometryCollection can contain a mix of different geometry types.
Constructor
GeometryCollection(geometries)Parameter | Type | Description |
|---|---|---|
geometries | list[Geometry] | A list of geometry objects of any type. |
Examples
collection = GeometryCollection([
GeometryPoint(-0.1278, 51.5074),
GeometryLine([
GeometryPoint(0.0, 0.0),
GeometryPoint(1.0, 1.0),
]),
])Usage
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),
})See also
Data types — All SDK data types
RecordID — Record identifier
SurrealQL Geometry Functions — Geospatial query functions