Geometry Types
Geometry classes provide support for spatial and geographic data using GeoJSON-compatible structures. These types are essential for location-based applications and geospatial queries.
Import:
import {
GeometryPoint,
GeometryLine,
GeometryPolygon,
GeometryMultiPoint,
GeometryMultiLine,
GeometryMultiPolygon,
GeometryCollection
} from 'surrealdb';
Source: value/geometry.ts
Geometry Types
GeometryPoint
A single point in 2D space (longitude, latitude).
Constructor
new GeometryPoint([longitude, latitude])
new GeometryPoint(point)
Example
const point = new GeometryPoint([-122.4194, 37.7749]);
await db.create(new Table('locations')).content({
name: 'Office',
position: point
});
GeometryLine
A line defined by two or more points.
Constructor
new GeometryLine([point1, point2, ...points])
new GeometryLine(line)
Example
const line = new GeometryLine([
new GeometryPoint([-122.4194, 37.7749]),
new GeometryPoint([-118.2437, 34.0522])
]);
const route = new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([1, 1]),
new GeometryPoint([2, 1]),
new GeometryPoint([3, 2])
]);
GeometryPolygon
A polygon defined by one or more lines (outer boundary and optional holes).
Constructor
new GeometryPolygon([outerBoundary, ...holes])
new GeometryPolygon(polygon)
Example
const triangle = new GeometryPolygon([
new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([4, 0]),
new GeometryPoint([2, 3]),
new GeometryPoint([0, 0])
])
]);
const donut = new GeometryPolygon([
new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([10, 0]),
new GeometryPoint([10, 10]),
new GeometryPoint([0, 10]),
new GeometryPoint([0, 0])
]),
new GeometryLine([
new GeometryPoint([2, 2]),
new GeometryPoint([8, 2]),
new GeometryPoint([8, 8]),
new GeometryPoint([2, 8]),
new GeometryPoint([2, 2])
])
]);
GeometryMultiPoint
A collection of points.
Constructor
new GeometryMultiPoint([point1, point2, ...points])
new GeometryMultiPoint(multiPoint)
Example
const stores = new GeometryMultiPoint([
new GeometryPoint([-122.4194, 37.7749]),
new GeometryPoint([-118.2437, 34.0522]),
new GeometryPoint([-87.6298, 41.8781])
]);
GeometryMultiLine
A collection of lines.
Constructor
new GeometryMultiLine([line1, line2, ...lines])
new GeometryMultiLine(multiLine)
Example
const routes = new GeometryMultiLine([
new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([1, 1])
]),
new GeometryLine([
new GeometryPoint([2, 2]),
new GeometryPoint([3, 3])
])
]);
GeometryMultiPolygon
A collection of polygons.
Constructor
new GeometryMultiPolygon([polygon1, polygon2, ...polygons])
new GeometryMultiPolygon(multiPolygon)
Example
const areas = new GeometryMultiPolygon([
new GeometryPolygon([]),
new GeometryPolygon([])
]);
GeometryCollection
A heterogeneous collection of geometry types.
Constructor
new GeometryCollection([geometry1, geometry2, ...geometries])
new GeometryCollection(collection)
Example
const collection = new GeometryCollection([
new GeometryPoint([0, 0]),
new GeometryLine([
new GeometryPoint([1, 1]),
new GeometryPoint([2, 2])
]),
new GeometryPolygon([])
]);
Common Methods
All geometry types share these methods:
.toJSON()
Convert to GeoJSON format.
const point = new GeometryPoint([-122.4194, 37.7749]);
console.log(point.toJSON());
.toString()
Convert to JSON string.
const point = new GeometryPoint([-122.4194, 37.7749]);
console.log(point.toString());
.clone()
Create a deep copy.
const original = new GeometryPoint([0, 0]);
const copy = original.clone();
.equals(other)
Check if two geometries are equal.
const a = new GeometryPoint([0, 0]);
const b = new GeometryPoint([0, 0]);
console.log(a.equals(b));
Complete Examples
Store Locations
import { Surreal, GeometryPoint, Table } from 'surrealdb';
const db = new Surreal();
await db.connect('ws://localhost:8000');
const locations = [
{
name: 'Main Office',
address: '123 Market St, San Francisco, CA',
position: new GeometryPoint([-122.4194, 37.7749])
},
{
name: 'LA Branch',
address: '456 Sunset Blvd, Los Angeles, CA',
position: new GeometryPoint([-118.2437, 34.0522])
}
];
for (const location of locations) {
await db.create(new Table('locations')).content(location);
}
Delivery Routes
const route = new GeometryLine([
new GeometryPoint([-122.4194, 37.7749]),
new GeometryPoint([-122.2711, 37.8044]),
new GeometryPoint([-122.0838, 37.3861]),
new GeometryPoint([-121.8863, 37.3382])
]);
await db.create(new Table('routes')).content({
driver: new RecordId('drivers', 'john'),
route: route,
estimated_time: Duration.parse('2h30m'),
created_at: DateTime.now()
});
Service Areas
const serviceArea = new GeometryPolygon([
new GeometryLine([
new GeometryPoint([-122.5, 37.7]),
new GeometryPoint([-122.3, 37.7]),
new GeometryPoint([-122.3, 37.8]),
new GeometryPoint([-122.5, 37.8]),
new GeometryPoint([-122.5, 37.7])
])
]);
await db.create(new Table('service_areas')).content({
name: 'SF Downtown',
area: serviceArea,
active: true
});
Geospatial Queries
const centerPoint = new GeometryPoint([-122.4194, 37.7749]);
const nearbyLocations = await db.query(`
SELECT * FROM locations
WHERE geo::distance(position, $center) < 5000
ORDER BY geo::distance(position, $center)
`, {
center: centerPoint
}).collect();
console.log('Nearby locations:', nearbyLocations);
Polygon Containment
const region = new GeometryPolygon([
new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([10, 0]),
new GeometryPoint([10, 10]),
new GeometryPoint([0, 10]),
new GeometryPoint([0, 0])
])
]);
const testPoint = new GeometryPoint([5, 5]);
const result = await db.query(`
RETURN geo::area::contains($region, $point)
`, {
region,
point: testPoint
}).collect();
console.log('Point is inside:', result[0]);
Multiple Locations (MultiPoint)
const branches = new GeometryMultiPoint([
new GeometryPoint([-122.4194, 37.7749]),
new GeometryPoint([-118.2437, 34.0522]),
new GeometryPoint([-87.6298, 41.8781]),
new GeometryPoint([-74.0060, 40.7128])
]);
await db.create(new Table('companies')).content({
name: 'Tech Corp',
headquarters: new GeometryPoint([-122.4194, 37.7749]),
all_branches: branches,
founded: DateTime.parse('2020-01-01')
});
Distance Calculations
const pointA = new GeometryPoint([-122.4194, 37.7749]);
const pointB = new GeometryPoint([-118.2437, 34.0522]);
const distance = await db.query(`
RETURN geo::distance($a, $b)
`, {
a: pointA,
b: pointB
}).collect();
console.log('Distance in meters:', distance[0]);
GeoJSON Export
const point = new GeometryPoint([-122.4194, 37.7749]);
const geoJson = point.toJSON();
Complex Region with Holes
const park = new GeometryPolygon([
new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([100, 0]),
new GeometryPoint([100, 100]),
new GeometryPoint([0, 100]),
new GeometryPoint([0, 0])
]),
new GeometryLine([
new GeometryPoint([40, 40]),
new GeometryPoint([60, 40]),
new GeometryPoint([60, 60]),
new GeometryPoint([40, 60]),
new GeometryPoint([40, 40])
])
]);
await db.create(new Table('parks')).content({
name: 'Central Park',
boundary: park,
has_lake: true
});
GeoJSON Compatibility
All geometry types are compatible with GeoJSON format:
const point = new GeometryPoint([-122.4194, 37.7749]);
const geoJson = point.toJSON();
console.log(geoJson);
Best Practices
1. Use Correct Coordinate Order
const point = new GeometryPoint([-122.4194, 37.7749]);
const wrong = new GeometryPoint([37.7749, -122.4194]);
2. Close Polygons Properly
const polygon = new GeometryPolygon([
new GeometryLine([
new GeometryPoint([0, 0]),
new GeometryPoint([10, 0]),
new GeometryPoint([10, 10]),
new GeometryPoint([0, 10]),
new GeometryPoint([0, 0])
])
]);
3. Use Appropriate Geometry Type
const office = new GeometryPoint([-122.4194, 37.7749]);
const branches = new GeometryMultiPoint([point1, point2, point3]);
const wrong = new GeometryMultiPoint([point1]);
4. Validate Coordinates
const valid = new GeometryPoint([-122.4194, 37.7749]);
const invalid = new GeometryPoint([200, 100]);
Use Cases
- Location-based Services - Store and query business locations
- Delivery Systems - Define delivery routes and service areas
- Real Estate - Property boundaries and service zones
- Transportation - Transit routes and coverage areas
- Environmental - Conservation areas, wildlife habitats
- Urban Planning - City zones, districts, infrastructure
See Also