MobilityDuck

MobilityDuck is a DuckDB extension, loaded by LOAD mobilityduck into a DuckDB of the version it is built for. Its version is mobilityduckVersion() and its full version mobilityduckFullVersion(). It also has three functions of its own.

temporalFooter(map) takes a map from the name of each temporal column to the name of its MobilityDB type and answers the JSON the footer of a Parquet file holds under the TemporalParquet 2.0.0 specification, so that a file DuckDB writes names the type of each of its temporal columns. pointCloudSchemaRegister(pcid, srid, compression, dimensions) registers the schema a pcid names, its SRID, its compression and the list of its dimensions, so that a point cloud value of that pcid answers about its coordinates; it answers true once registered, and registering a pcid again replaces the schema it named. mobilityduckOpenSSLVersion(text) answers the text it is given within a sentence naming the version of OpenSSL MobilityDuck links.

DuckDB and Its Raster Extension

DuckDB's raster extension, a community extension, reads a raster file with RT_Read as a table with one row per tile: the pixels of each band in a databand_N column of type RT_DATACUBE, the georeferencing of the whole file in a metadata JSON column, and the position of the tile in tile_x and tile_y. It has no raster value of its own, only these columns. A MobilityDB raster is built from the four columns of a tile, and gives back a datacube and a metadata column, the two columns the extension's own functions read a tile from. The metadata rasterMetadata writes is that of a file holding the raster as its one tile, so the raster is built back from it at the tile (0, 0).

The type RT_DATACUBE exists only once the extension is loaded, so a query naming it runs after LOAD raster; before it, DuckDB answers that no type RT_DATACUBE exists. The conversions are explicit casts and functions: a raster is never converted to a datacube by itself when it is passed to an RT_ function, since a datacube leaves the georeferencing behind.

FromToSQL
a tile of RT_Read, or any datacube with its metadatarasterraster(databand_1, tile_x, tile_y, metadata)
rasterRT_DATACUBECAST(rast AS RT_DATACUBE)
rasterthe metadata of a tilerasterMetadata(rast)
summarystatsRT_STATSCAST(stats AS RT_STATS)
RT_STATSsummarystatsCAST(stats AS summarystats)

A datacube holds one pixel type for all its bands, and each pixel type of MobilityDB becomes the datacube type that GDAL gives it, as a PostGIS raster written through GDAL does. The 1-, 2- and 4-bit types have no smaller type in a datacube: their values are kept exactly in 8 bits, and a raster built back from the datacube has the type 8BUI.

A conversion either keeps every value or raises an error; it never answers an approximation. A datacube holds one pixel type and one nodata value, so a raster whose bands differ in either cannot be cast to RT_DATACUBE. A datacube of type UINT64 or INT64 has no pixel type of MobilityDB to become. A datacube or a metadata column that is not well formed, truncated, carrying an unknown type or format, or declaring more pixels than it holds, is refused rather than read. The coordinate reference system of the metadata is read as EPSG:n and gives the SRID of the raster; a raster combined with a value of another SRID raises the error any mix of SRIDs raises. A raster cast to a datacube writes its pixels uncompressed; a datacube is read whichever compression the extension wrote it with, RAW, SNAPPY, GZIP, ZSTD or LZ4.

LOAD raster;
-- The tiles of 2 x 2 pixels of a 4 x 4 GeoTIFF of elevations, as MobilityDB rasters
CREATE TABLE elevation AS
SELECT tile_x, tile_y, raster(databand_1, tile_x, tile_y, metadata) AS rast
FROM RT_Read('elevation.tif', blocksize_x := 2, blocksize_y := 2);
-- MobilityDB's raster functions over them
SELECT count(*), summarystatsMean(summaryStatsAgg(rast, true)) FROM elevation;
-- 4 | 57.06666666666667
-- A MobilityDB raster handed to a function of the extension
SELECT tile_x, tile_y, RT_CubeStats(CAST(rast AS RT_DATACUBE), 0).mean
FROM elevation ORDER BY tile_y, tile_x;
-- 0 | 0 | 53.5
-- 1 | 0 | 68.5
-- 0 | 1 | 46.0
-- 1 | 1 | 61.333333333333336
-- A raster converted to the extension's columns and back is the raster it was
SELECT bool_and(raster(CAST(rast AS RT_DATACUBE), 0, 0, rasterMetadata(rast)) = rast)
FROM elevation;
-- true