spatialNearest method

Future<List<Map<String, Object?>>> spatialNearest(
  1. String table,
  2. String geomColumn,
  3. String wkt, {
  4. int limit = 10,
  5. double searchBuffer = 1.0,
})

The limit rows of table whose geomColumn is nearest to wkt, ordered by ascending distance. Each returned row includes a distance column.

Pre-filters using the R*Tree spatial index against wkt's bounding box expanded by searchBuffer (in the spatial reference system's units) on every side, then ranks the candidates by exact ST_Distance. Rows outside the expanded search box are never considered — widen searchBuffer if fewer than limit rows come back.

Implementation

Future<List<Map<String, Object?>>> spatialNearest(
  String table,
  String geomColumn,
  String wkt, {
  int limit = 10,
  double searchBuffer = 1.0,
}) => rawQuery(
  'SELECT t.*, ST_Distance(t.$geomColumn, GeomFromText(?)) AS distance '
  'FROM $table AS t, (SELECT ST_Expand(GeomFromText(?), ?) AS bbox) AS s '
  'WHERE t.ROWID IN ('
  '  SELECT ROWID FROM ${_rtreeName(table, geomColumn)}'
  '  WHERE xmin <= MbrMaxX(s.bbox) AND xmax >= MbrMinX(s.bbox)'
  '    AND ymin <= MbrMaxY(s.bbox) AND ymax >= MbrMinY(s.bbox)'
  ') '
  'ORDER BY distance LIMIT ?',
  [wkt, wkt, searchBuffer, limit],
);