mapbench
Contents

Documentation → How-to guides

Query and model data with SQL

The SQL Workbench runs spatial SQL over the datasets you already have: joins, aggregates, buffers and filters. If you want to keep a query, save it as a model. Running a model produces a new dataset that you can tile and map like any other.

Queries and models

A queryA model
gives yourows, on screena new dataset, tiled and mappable
createsnothinga dataset you can map, share and query again
costsa momenta tiling job
needs geometrynoyes, exactly one geometry column
use it tocheck an assumption, count something, explorekeep a result and put it on a map

Use queries to explore and models to keep results. A common mistake is running a tiling job to answer a question that a query could answer in a second.

Writing a query

  1. Open SQL Workbench in the sidebar.
  2. Add the datasets you want to query as inputs. Each one gets a short alias, which you use as its table name in your SQL.
  3. Write the query. FROM parks reads the input with the alias parks.
  4. Choose Preview to run it and see the rows.

The schema browser beside the editor lists each input's columns, and the table tab shows real rows. Check field names there, because the names in published data often differ from what the source's documentation says.

The dialect

The SQL dialect is DuckDB with its spatial extension. You can use the ST_* functions and DuckDB's own extras, such as QUALIFY, GROUP BY ALL and SELECT * EXCLUDE (…).

  • The geometry column is geom on every input.
  • All data is in WGS84 (EPSG:4326), as longitude and latitude in degrees. mapbench converts each dataset to WGS84 when you import it, so joins across sources line up without reprojection.
  • You rarely want measurements in degrees. Use ST_Distance_Sphere for distances in metres, or project into a metric CRS for areas and buffers. If you reproject, pass always_xy := true to ST_Transform. Without it you get empty geometry and no error.

Reference datasets

Your operator may have loaded shared reference datasets, such as OpenStreetMap roads, waterways, buildings and points of interest. They appear in the catalogue alongside your own datasets. You can use them as model inputs or map layers without importing them, and they don't count towards your storage. They are read-only, so you can't edit or reprocess them.

Turning a query into a model

  1. Get the query returning what you want in the preview.
  2. Choose Run.
  3. Give the output dataset a name and a layer ID. The same rules apply as for any dataset, and the layer ID can't be changed later.
  4. Run it. A tiling job is queued. When the dataset is Ready, it behaves like any other dataset: you can style it, map it, share it, or use it as the input to another model.

Your result must have exactly one geometry column. A model produces a map layer, and a layer has one geometry. If you join two spatial inputs, select parks.geom instead of parks.*, or you will get two geometry columns. In an aggregate, the geometry must either appear in the GROUP BY or come from a function such as ST_Union.

Re-running a model

Edit the SQL and run it again. If the model already has an output, running it replaces that dataset. The dataset keeps the same id, so its styling and map memberships carry over and published links keep working. mapbench warns you before it replaces the output.

Models don't run on a schedule, and they don't re-run when an input changes. When you refresh an input, re-run the models built on it.

Lineage

A model records its inputs, so each dataset it produces keeps a record of where it came from. This helps you work out later which of several similarly named layers a published map is drawing, and which upstream import broke it. Archived inputs still appear in the lineage, which is useful because an archived input is often the one you are looking for.

Limits

  • A query returns at most 1000 rows. Use queries for checking and aggregating data. They aren't meant for exporting a dataset.
  • Queries are limited to 60 per minute from the app, and 10 per minute from an API key or a connected agent.
  • A query that runs too long or uses too much memory is stopped. Narrow it with a WHERE clause or a bounding box before you try again.
  • Your SQL never touches the database that holds your account and catalogue. It runs in a sandbox over copies of the data files, with no file, network or write access, so ATTACH, COPY and read_csv on a path all fail.

Common problems

A column in my result is entirely null
mapbench flags this because it usually points to a mistake in the query, such as a mistyped field name in a left join or a reprojection that failed without an error. Check the field name in the schema browser before you assume the data is empty.
A spatial join returns nothing
The two inputs probably don't overlap where you expect. Query the bounding box of each one (ST_Extent(geom)) and compare them. A dataset imported with the wrong CRS can end up in the Gulf of Guinea, and then the join correctly finds nothing.
Distances and areas are tiny decimals
You measured in degrees. See the note on WGS84 above.
“The model result must have exactly one geometry column”
You selected * across two spatial inputs. Name the geometry column you want and leave out the other.
The preview looks right but the tiled dataset is missing features
The preview shows the first rows of your result, but the model runs the whole query. If the counts disagree, compare SELECT count(*) with the tiled dataset's feature count before you look for a tiling problem.

See also