• Start

Languages

/

Python

/

API Reference

/

Values

Geometry

GeoJSON-compatible geometry types for spatial data.

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)

Parameter

Type

Description

longitudefloat

The longitude coordinate.

latitudefloat

The latitude coordinate.

point = GeometryPoint(-0.1278, 51.5074)

A line defined by two or more points.

Syntax
GeometryLine(points)

Parameter

Type

Description

pointslist[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)

Parameter

Type

Description

ringslist[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)

Parameter

Type

Description

pointslist[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)

Parameter

Type

Description

lineslist[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)

Parameter

Type

Description

polygonslist[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)

Parameter

Type

Description

geometrieslist[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?