Geometries - SurrealTools/Documentation GitHub Wiki

a quick example of storing a bunch of gps coordinates and then querying to find out which you're closest to if, and only if, you're within a certain distance of any of them? "you're closest to" is, of course, a gps derived coordinate that I would have to pass into the query.

LET $points = [(60.0, 50.0), (10.0, -50.0), (49.0, 49.0), (49.5, 49.5)];

$points
    .map(|$point| [$point, (50.0, 50.0)])
    .filter(|$points| geo::distance($points[0], $points[1]) < 500000)
    .map(|$points| {
        from: $points[1],
        to: $points[0],
        distance: geo::distance($points[0], $points[1])
}).sort();

Otherwise I think you'd have to do something like this to use an ORDER BY clause:

LET $points = [(60.0, 50.0), (10.0, -50.0), (49.0, 49.0), (49.5, 49.5)];

SELECT * FROM ($points
    .map(|$point| [$point, (50.0, 50.0)])
    .filter(|$points| geo::distance($points[0], $points[1]) < 500000)
    .map(|$points| {
        from: $points[1],
        to: $points[0],
        zdistance: geo::distance($points[0], $points[1])
})) ORDER BY zdistance;

Calculate sum of distance between multiple points (tracking distances covered by person during a walk.)

DEFINE FUNCTION fn::sumDistancesOfAllWaypoints($waypoints: array<point>) -> number {
  RETURN math::sum($waypoints.windows(2).map(|$p| geo::distance($p[0], $p[1])))
};

-- Then you can do
LET $waypoints = [
	(-0.03921743611083, 51.88106875736589), -- London
	(30.48112752349519, 50.68377089794912), -- Kyiv
	(23.66174524001544, 42.94500782833793), -- Sofia
	(1.92481534361859, 41.69698118125476) -- Barcelona
];

SELECT fn::sumDistancesOfAllWaypoints($waypoints);

Geo Fields

Define geometries in SQL as part of a schema. Geometry Points in SurrealDB are specified using longitude and then latitude (lng, lat)

-- Allow any geometry type
DEFINE FIELD location ON TABLE restaurant TYPE geometry(feature);
-- Allow just geo points
DEFINE FIELD location ON TABLE restaurant TYPE geometry(point);
-- Allow just geo polygons
DEFINE FIELD location ON TABLE restaurant TYPE geometry(polygon);
-- Allow polygons or multipolygons
DEFINE FIELD location ON TABLE restaurant TYPE geometry(polygon, multipolygon);
-- Allow multipolygons or geojson collections
DEFINE FIELD location ON TABLE restaurant TYPE geometry(multipolygon, collection);
-- Available types are ... feature, point, line, polygon, multipoint, multiline, multipolygon, collection

Query between two points

LET $me = (-0.118092, 51.509865);
SELECT * FROM restaurant WHERE geo::distance(location, $me) < 1000; -- location is less than 1km away

type::point() function

  1. The type::point() function expects 2 arguments (both floats).
  2. If you pass 1 argument, then it returns a NULL value.
  3. If you pass 2 arguments, then it converts each of them into a .
  4. When converting null to a it is 0.0.

Distance Between two points

let $p1 = (select geo.coordinates from point:1 LIMIT 1); -- we only want 1 result
let $p2 = (select geo.coordinates from point:2 LIMIT 1); -- we only want 1 result
RETURN geo::distance($p1, $p2);
⚠️ **GitHub.com Fallback** ⚠️