Encoding functions in the GeoSilo DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- check-fixtures.sql for geosilo
-- spatial is needed for ST_GeomFromText, ST_AsText, etc.
INSTALL spatial;
LOAD spatial;
-- raw GEOMETRY source for INSERT INTO parcels SELECT ... FROM raw_parcels;
CREATE OR REPLACE TABLE raw_parcels AS
SELECT * FROM (VALUES
(1, 'a', ST_GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))')),
(2, 'b', ST_GeomFromText('POLYGON((2 2,3 2,3 3,2 3,2 2))'))
) t(id, name, geom);
-- roads(geom) for ST_Length aggregate
CREATE OR REPLACE TABLE roads AS
SELECT * FROM (VALUES
(ST_GeomFromText('LINESTRING(0 0, 1 1, 2 2)')),
(ST_GeomFromText('LINESTRING(0 0, 0 1, 0 2)'))
) t(geom);
-- stations(id, geom) — point geometries for ST_X/ST_Y
CREATE OR REPLACE TABLE stations AS
SELECT * FROM (VALUES
(1, ST_GeomFromText('POINT(-77.0 39.0)')),
(2, ST_GeomFromText('POINT(-76.5 40.5)'))
) t(id, geom);
-- mixed_geom_table(geom) — mixed geometry types
CREATE OR REPLACE TABLE mixed_geom_table AS
SELECT * FROM (VALUES
(ST_GeomFromText('POINT(0 0)')),
(ST_GeomFromText('LINESTRING(0 0, 1 1)')),
(ST_GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))'))
) t(geom);
-- compact_table(silo_blob) — geosilo blobs for geosilo_metadata example
CREATE OR REPLACE TABLE compact_table AS
SELECT geosilo_encode(ST_GeomFromText('POLYGON((0 0,1 0,1 1,0 1,0 0))')) AS silo_blob
UNION ALL
SELECT geosilo_encode(ST_GeomFromText('POINT(1 2)')) AS silo_blob;
-- utm_data(geom) — projected meters geometry source
CREATE OR REPLACE TABLE utm_data AS
SELECT ST_GeomFromText('POINT(500000 4500000)') AS geom;
-- mm_precision_data(geom) and millimeter_data(geom)
CREATE OR REPLACE TABLE mm_precision_data AS
SELECT ST_GeomFromText('POINT(0.001 0.002)') AS geom;
CREATE OR REPLACE TABLE millimeter_data AS
SELECT ST_GeomFromText('POINT(0.001 0.002)') AS geom;
-- parcels_geometry(id, geom) — GEOMETRY-typed parcels for export example
CREATE OR REPLACE TABLE parcels_geometry AS
SELECT id, geom FROM raw_parcels;
-- parcels(id, name, geom GEOSILO) — the table the cookbook defines and fills
-- in earlier blocks. Each in-browser example runs on its own, so it needs to
-- exist up front. The polygons sit inside the cookbook's -78..-76 longitude,
-- 39..41 latitude band, so its bounding-box filters return rows.
CREATE OR REPLACE TABLE parcels (
id INTEGER,
name VARCHAR,
geom GEOSILO('EPSG:4326') USING COMPRESSION zstd
);
INSERT INTO parcels
SELECT * FROM (VALUES
(1, 'north lot', ST_GeomFromText('POLYGON((-77.2 39.8,-77.1 39.8,-77.1 39.9,-77.2 39.9,-77.2 39.8))')),
(2, 'river parcel', ST_GeomFromText('POLYGON((-76.9 40.1,-76.8 40.1,-76.8 40.2,-76.9 40.2,-76.9 40.1))')),
(3, 'west field', ST_GeomFromText('POLYGON((-79.5 38.0,-79.4 38.0,-79.4 38.1,-79.5 38.1,-79.5 38.0))'))
) t(id, name, geom);
-- Source table and input files the cookbook's load examples read.
CREATE OR REPLACE TABLE raw_data AS SELECT id, geom FROM raw_parcels;
COPY (SELECT id, name, ST_AsText(geom) AS wkt FROM raw_parcels)
TO 'parcels.csv' (HEADER true);
COPY (SELECT id, name, ST_AsWKB(geom) AS geom_wkb FROM raw_parcels)
TO 'parcels.parquet' (FORMAT PARQUET);
Function category
Encoding
3 functionsEncode/decode between GEOMETRY and the compact GEOSILO BLOB format, plus header inspection without a full decode. The GEOSILO column type drives most of this transparently — these functions are for explicit BLOB pipelines and Arrow IPC interchange.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
blob
|
Type
BLOB
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geom
|
Type
GEOSILO
|
Mode Positional | Description |
Description
Decode a GEOSILO blob back into a standard GEOMETRY. Most callers don't need to call this directly — the implicit GEOSILO → GEOMETRY cast runs it on demand for any ST_* function that doesn't have a native GEOSILO overload (e.g. ST_Buffer, ST_Union, ST_AsText).
DuckDB 2.0 build: Every read is bounds-checked: a truncated or corrupt blob raises an Invalid Input Error instead of crashing the process, and nesting deeper than 16 levels is rejected. Use try_geosilo_decode to get NULL instead of an error, or geosilo_is_valid to filter untrusted blobs first.
SELECT ST_AsText(
geosilo_decode(geosilo_encode(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0))')))
);
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geometry
|
Type
GEOMETRY
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geometry
|
Type
GEOMETRY
|
Mode Positional | Description |
Argument
scale
|
Type
BIGINT
|
Mode Positional | Description |
Description
Encode a GEOMETRY into the compact GEOSILO format and return a BLOB. With one argument, the integer scale is derived from the geometry's CRS — degrees default to 1e7 (≈1 cm), projected meters to 100 (1 cm). With two arguments, the second explicitly sets the scale. To use the result with the native GEOSILO ST_* overloads, cast it with ::GEOSILO: the explicit BLOB → GEOSILO cast validates the silo header and fails on a bad one (TRY_CAST returns NULL instead). There is no cast from GEOSILO back to BLOB. geosilo_decode and geosilo_metadata accept the BLOB directly.
DuckDB 2.0 build: scale must be a positive integer — 0 or a negative scale raises an error (the Haybarn 1.5 build silently returned POINT EMPTY for 0). Scaled coordinates must fit a 32-bit integer (about ±214 at 1e7, ±21,474,836 at 100), otherwise encoding fails with an overflow error. Z, M and ZM geometries encode and decode correctly, and the BLOB → GEOSILO cast validates the whole blob rather than only its header.
SELECT geosilo_encode(ST_GeomFromText('POINT(1 2)'));
SELECT geosilo_encode(geom, 100) FROM utm_data;
SELECT ST_Area(geosilo_encode(ST_GeomFromText('POLYGON((0 0, 4 0, 4 3, 0 3, 0 0))'))::GEOSILO);
-- 12.0
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
blob
|
Type
BLOB
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
geom
|
Type
GEOSILO
|
Mode Positional | Description |
Description
Read a GEOSILO blob's header without decoding the geometry. Returns STRUCT(geometry_type VARCHAR, vertex_type VARCHAR, scale BIGINT). Useful for filtering by geometry type or auditing the integer scale of a stored column before any heavy spatial work.
DuckDB 2.0 build: The header is fully validated (magic byte, version, geometry and vertex type, positive scale); an empty or malformed blob raises an error, and a NULL input gives a NULL struct whose fields are also NULL.
SELECT geosilo_metadata(silo_blob) AS meta
FROM compact_table
LIMIT 5;
-- {'geometry_type':'POLYGON','vertex_type':'XY','scale':10000000}