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.
GeometryPoint
A single geographic point defined by longitude and latitude.
Constructor
Syntax
GeometryPoint(longitude, latitude)
| Parameter | Type | Description |
|---|
longitude required | float | The longitude coordinate. |
latitude required | float | The latitude coordinate. |
Examples
point = GeometryPoint(-0.1278, 51.5074)
GeometryLine
A line defined by two or more points.
Constructor
Syntax
GeometryLine(points)
| Parameter | Type | Description |
|---|
points required | 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
Syntax
GeometryPolygon(rings)
| Parameter | Type | Description |
|---|
rings required | 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
Syntax
GeometryMultiPoint(points)
| Parameter | Type | Description |
|---|
points required | 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
Syntax
GeometryMultiLine(lines)
| Parameter | Type | Description |
|---|
lines required | 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
Syntax
GeometryMultiPolygon(polygons)
| Parameter | Type | Description |
|---|
polygons required | 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
Syntax
GeometryCollection(geometries)
| Parameter | Type | Description |
|---|
geometries required | 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