Skip to content

Measurement (native) 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

Measurement (native)

3 functions

ST_Area

Scalar function Measurement (native)
Signature
ST_Area(geom: GEOSILO) → DOUBLE
Arguments (Positional)
Argument geom Type GEOSILO Mode Positional Description
Description
1 Polygon area
SELECT ST_Area(ST_GeomFromText('POLYGON((0 0, 4 0, 4 3, 0 3, 0 0))')::GEOSILO);
-- 12.0
2 A hole is subtracted whichever way it is wound
SELECT ST_Area(ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 8 2, 8 8, 2 8, 2 2))')::GEOSILO);
-- 64.0

ST_Length

Scalar function Measurement (native)
Signature
ST_Length(geom: GEOSILO) → DOUBLE
Arguments (Positional)
Argument geom Type GEOSILO Mode Positional Description
Description
1 Line length
SELECT ST_Length(ST_GeomFromText('LINESTRING(0 0, 3 4)')::GEOSILO);
-- 5.0
2 Segments spanning the full longitude range
SELECT ST_Length(ST_GeomFromText('LINESTRING(-179 0, 179 0)')::GEOSILO);
-- 358.0

ST_Perimeter

Scalar function Measurement (native)
Signature
ST_Perimeter(geom: GEOSILO) → DOUBLE
Arguments (Positional)
Argument geom Type GEOSILO Mode Positional Description
Description
1 Polygon perimeter
SELECT ST_Perimeter(ST_GeomFromText('POLYGON((0 0, 4 0, 4 3, 0 3, 0 0))')::GEOSILO);
-- 14.0