Spatial Queries

SELECT name, ST_Distance(location, ST_Point(-73.990, 40.750)) AS dist
FROM restaurants
WHERE ST_DWithin(location, ST_Point(-73.990, 40.750), 1000)
ORDER BY dist;

Geofencing (Point-in-Polygon)

SELECT name FROM restaurants
WHERE ST_Within(location, ST_GeomFromGeoJSON('{
    "type": "Polygon",
    "coordinates": [[[-74.0, 40.7], [-73.9, 40.7], [-73.9, 40.8], [-74.0, 40.8], [-74.0, 40.7]]]
}'));

OGC Predicates

FunctionDescription
ST_ContainsGeometry A contains geometry B
ST_IntersectsGeometries share any space
ST_WithinGeometry A is within geometry B
ST_DWithinWithin a given distance
ST_DistanceDistance between two geometries
ST_IntersectionIntersection geometry
ST_BufferBuffer around geometry
ST_EnvelopeBounding box
ST_UnionUnion of geometries

Spatial Join

SELECT r.name, z.zone_name FROM restaurants r, delivery_zones z
WHERE ST_Contains(z.boundary, r.location);

H3 Hexagonal Indexing

SELECT h3_to_string(h3_encode(40.748, -73.985, 9)) AS hex;

Hybrid Spatial-Vector

SELECT name, vector_distance(embedding, $query_vec) AS similarity
FROM restaurants
WHERE ST_DWithin(location, ST_Point(-73.990, 40.750), 2000) AND embedding <-> $query_vec
LIMIT 10;
View page sourceLast updated on Apr 16, 2026 by Farhan Syah