Skip to content

autk-db - Named selections and SQL

Named selections and SQL ​

A NamedSelection is a named, colored group of places and directed flows. It is independent of the DOM and database. A map can edit it; selectionSql() turns it into a condition on your table.

Unlike MapEvent.CLICK or plot selections, feature places contain semantic IDs (string or number), not positions in a collection. A region contains a closed WGS84 [longitude, latitude] ring, even when the map or database uses a projected workspace.

Query a drawn region ​

This self-contained example selects two observations. Change the region's bounds or the point coordinates and rerun it.

The spatial extension is loaded by db.init(). The playground releases this database before rerunning or leaving the page; an application should call await db.destroy() when it no longer needs the data.

Declare where rows are located ​

A TableLayout describes each named place of a row:

ts
const layout = {
  pickup: { location: ['pickup_lon', 'pickup_lat'] },
  dropoff: { location: ['dropoff_lon', 'dropoff_lat'] },
}
  • Scalar location columns default to EPSG:4326.
  • A DuckDB geometry column defaults to the workspace default, EPSG:3395. Supply crs if the actual column uses another CRS; it is not inferred from a particular database instance.
  • links: { zones: 'pickup_zone_id' } relates a row to a semantic feature ID in a layer named zones.

For projected data, use { geometry: 'geometry', crs: 'EPSG:32618' }. Rings stay WGS84; the compiler transforms them to the declared coordinate system.

Places and flows ​

{ at: ['pickup', 'dropoff'] } matches a place at either named location. For a directed origin–destination condition, use:

ts
const where = selectionSql(selection, layout, { flow: ['pickup', 'dropoff'] })

The selection must contain flows with from and to places; reversing them changes the condition. Arrow anchors a and b describe its display, not extra SQL predicates. See Manhattan taxi flows for native drawing, live SQL, and linked subset charts.

For a region without a named group, use regionPredicate(ring, ['lon', 'lat']) or regionPredicate(ring, 'geometry', 'EPSG:3395'). Boundaries count as inside (ST_Covers). SQL regions must be closed, have at least four vertices including the closing point, contain finite coordinates, and not self-intersect.

Identifiers and feature values are quoted by the helpers. A custom region callback supplies trusted SQL, not a sandbox for untrusted input. rawQuery() still accepts only one SELECT/CTE statement.

Next steps ​

Released under the MIT License.