Home > Software engineering >  how to use `ST_AsMVTGeom` and `ST_AsMVT`
how to use `ST_AsMVTGeom` and `ST_AsMVT`

Time:11-12

As shown in the below table grid_cell_data, I have geometryOfCellRepresentativeToTreatment and geometryOfCellRepresentativeToBuffer columns, and they contain geometries.

I want to apply both of ST_AsMVTGeom and ST_AsMVT as stated in the PostGIS documentation, ST_AsMVT takes a geometry column. but when I execute the following code:

SELECT ST_AsMVT(grid_cell_data.geometryOfCellRepresentativeToTreatment) 
AS geom
FROM grid_cell_data

I get the following error:

 pgis_asmvt_transfn: parameter row cannot be other than a rowtype
 

please let me know how to use both of functions ST_AsMVTGeom and ST_AsMVT

image: enter image description here

CodePudding user response:

The function ST_AsMVT expects a row containing a geometry, not simply a geometry. One option to use it is to select the records in a CTE or subquery, so that you can chose which columns of your table are going to be used, e.g. name and geometryOfCellRepresentativeToTreatment:

SELECT ST_AsMVT(g) FROM (
  SELECT name,geometryOfCellRepresentativeToTreatment
  FROM grid_cell_data) g;

Or if you wish to use the whole record:

SELECT ST_AsMVT(grid_cell_data) 
FROM grid_cell_data

Regarding ST_AsMVTGeom you must provide at least a geometry and a the geometric bounds of the tile contents to make it work:

SELECT
 ST_AsText(
   ST_AsMVTGeom(
     geometryOfCellRepresentativeToTreatment,
     -- change to the envelope that suits your data set
     ST_MakeBox2D(ST_Point(0, 0), ST_Point(4096, 4096)))) 
FROM grid_cell_data;

Or combining both functions (see documentation):

WITH j AS (
 SELECT
   ST_AsMVTGeom(
     geometryOfCellRepresentativeToTreatment,
     ST_MakeBox2D(ST_Point(0, 0), ST_Point(4096, 4096)))
 FROM grid_cell_data
)
SELECT ST_AsMVT(j.*) FROM j;

Demo: db<>fiddle

CodePudding user response:

It is an aggregate function like count or sum, and the first argument must be a composite type. (I wonder why PostGIS didn't declare the parameter as record.)

So you are expected call it like this, using a “whole-row reference”:

SELECT ST_AsMVT(grid_cell_data)
FROM grid_cell_data
[WHERE ...]
[GROUP BY ...]
  • Related