I'm trying to develop a plpgsql function that would extract mapbox vector tiles from a postgresql/post gis database. The database has multiple geospatial tables, so I want the function to be able to take a table name as a parameter.
CREATE OR REPLACE
FUNCTION public.region3_sim_storms_pg(z integer, x integer, y integer, stormtable text)
RETURNS bytea
AS $$
DECLARE
result bytea;
BEGIN
WITH
bounds AS (
SELECT ST_TileEnvelope(z, x, y) AS geom
),
mvtgeom AS (
EXECUTE format(
'SELECT ST_AsMVTGeom(ST_Transform(G.geom, 3857), bounds.geom, 4096, 256, true) AS geom, node, bathymetry
FROM %s AS G, bounds
WHERE ST_Intersects(G.geom, ST_Transform(bounds.geom, 4326))', quote_ident(stormtable)
)
)
SELECT ST_AsMVT(mvtgeom, 'public.region3_sim_storms_pg')
INTO result FROM mvtgeom;
RETURN result;
END
$$
LANGUAGE 'plpgsql'
STABLE
PARALLEL SAFE;
COMMENT ON FUNCTION public.region3_sim_storms_pg IS 'Given a tile address, storm name query database.';
Thanks
Jim