Using ST_AsMVT

Erik Wienhold ewie at ewie.name
Sun Jul 6 11:31:55 PDT 2025


Please also CC the list.

On 2025-07-06 18:19 +0200, Matthew Shaw wrote:
> I restructured the function, and it now seems to work.  My next struggle is
> to get the styling to render.  That part doesn't work, and it relies on
> "class".  (I included "id" because the documentation suggests a unique key
> is required.)
> 
> CREATE OR REPLACE
>     FUNCTION f_highways(z integer, x integer, y integer)
>     RETURNS bytea AS $$
> DECLARE
>   mvt bytea;
> BEGIN
> 
> 
> WITH mvtgeom AS
> 
> (
> 
>  SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(z, x, y),4326),
> 4096, 64, true) AS geom, id, class
> 
>  FROM openstreetmap.highway
> 
>  WHERE geom && ST_Transform(ST_TileEnvelope(z, x, y),4326)
> 
> )
> 
> SELECT into mvt ST_AsMVT(mvtgeom.*)
> 
> FROM mvtgeom
> 
> where geom is not null;
> 
> 
>   RETURN mvt;
> END
> $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

Reading the docs at https://postgis.net/docs/ST_AsMVT.html it appears
to me that you have to call it like this:

	ST_AsMVT(mvtgeom.*, "highway", 4096, "geom", "id")

in order to specify "id" as feature_id_name.  (4096 here is the default
extent which also matches the snippet from the Martin docs that you've
cited.)

Column "class" which appears after "id" in your CTE should then be
available as a feature property according to the ST_AsMVT docs:

"The first column matching name and valid type (smallint, integer,
bigint) will be used as Feature ID, and any subsequent column will be
added as a property."

> I'm a 66-year-oldster, by the way.

Ain't nothing wrong with that :)

-- 
Erik Wienhold


More information about the postgis-users mailing list