Skip to content

Instantly share code, notes, and snippets.

@bitner
Last active October 13, 2021 14:01
Show Gist options
  • Save bitner/b37852bc4945e18a3a98a8ff7edff40f to your computer and use it in GitHub Desktop.
Save bitner/b37852bc4945e18a3a98a8ff7edff40f to your computer and use it in GitHub Desktop.
landsat_centroid_poly.sql
CREATE OR REPLACE FUNCTION landsat(
-- mandatory parameters
xmin float,
ymin float,
xmax float,
ymax float,
epsg integer
)
RETURNS bytea AS $$
WITH bounds AS (
SELECT ST_Transform(ST_Segmentize(
ST_MakeEnvelope(xmin, ymin, xmax, ymax, epsg), xmax - xmin
), 4326) as bounds
), t AS (
SELECT
ST_AsMVTGeom(st_transform(geom, epsg), bounds) as polymvt,
ST_AsMVTGeom(st_centroid(st_transform(geom, epsg), bounds)) as pointmvt,
path,
"row"
FROM public.landsat_wrs, bounds
WHERE ST_Intersects(geom, bounds)
)
SELECT
ST_AsMVT(ROW(t.polymvt, path, "row"), 'polygon')
||
ST_AsMVT(ROW(t.pointmvt), 'point')
FROM t;
$$
LANGUAGE SQL
IMMUTABLE -- Same inputs always give same outputs
STRICT -- Null input gets null output
PARALLEL SAFE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment