This topic describes the ST_AsMVTEx function. This function serves the same as the ST_AsMVT function. Compared with the ST_AsMVT function, the ST_AsMVTEx function filters out the vector elements that have little effect on the display based on the relationships among different vector elements. This helps reduce the size of mapbox vector tiles (MVTs) and improve the visualization efficiency. The scale_factor parameter is added to control the filtering effect. The mvt_size_limit parameter is added to limit the maximum number of vector elements that an MVT can contain.
Syntax
bytea ST_AsMVTEx(anyelement row);
bytea ST_AsMVTEx(anyelement row, int4 scale_factor);
bytea ST_AsMVTEx(anyelement row, int4 scale_factor, int4 mvt_size_limit);
bytea ST_AsMVTEx(anyelement row, int4 scale_factor, int4 mvt_size_limit, text name);
bytea ST_AsMVTEx(anyelement row, int4 scale_factor, int4 mvt_size_limit, text name, int4 extent);
bytea ST_AsMVTEx(anyelement row, int4 scale_factor, int4 mvt_size_limit, text name, int4 extent, text geom_name);
bytea ST_AsMVTEx(anyelement row, int4 scale_factor, int4 mvt_size_limit, text name, int4 extent, text geom_name, text feature_id_name);
Return value
Returns a binary MVT representation that corresponds to a tile layer. The tile content is determined by the set of rows.
Parameters
Parameter | Description |
row | The row data with at least one geometry column. |
scale_factor | The filtering effect. The larger the value is, the more vector elements are filtered, and the smaller the MVT is. Valid values: 1 to extent-1. Default value: 1. |
mvt_size_limit | The maximum number of vector elements that an MVT can contain. Vector elements that exceed the limit are discarded. The default value is the maximum INTEGER value, which is 2147483647. |
name | The name of the layer. The default value is the string |
extent | The tile extent in screen space as defined by the specification. Default value: 4096. |
geom_name | The name of the geometry column in the row data. The default value is the first geometry column. |
feature_id_name | The name of the Feature ID column in the row data. The Feature ID is not set for NULL or negative values. The first column that matches name and valid type including smallint, integer, and bigint is used as Feature ID. Any subsequent column is added as a property. JSON properties are not supported. |
Description
This function serves the same as the ST_AsMVT function. Compared with the ST_AsMVT function, the ST_AsMVTEx function filters out the vector elements that have little effect on the display to reduce the size of MVTs and improve the visualization efficiency. The
scale_factor
parameter controls the filtering effect. The value ofscale_factor
ranges from 1 to extent-1. The larger the value is, the more vector elements are filtered.The
mvt_size_limit
parameter specifies the maximum number of vector elements that an MVT can contain. If the number of vector elements is greater than themvt_size_limit
value after the filtering, the system randomly selects and discards excess vector elements.If the value of the
extent
parameter is very large, system performance is affected.This function is not suitable for datasets that consist of large planes.
Example
-- Create a vector data table and insert one plane, one line, and 9998 points.
CREATE TABLE example_table(id integer, geom Geometry);
INSERT INTO example_table(id, geom) VALUES
(1, ST_MakeEnvelope(150, 75, 170, 80, 4326));
INSERT INTO example_table(id, geom) VALUES
(2, ST_GeomFromText('LINESTRING(-160 -70, 160 -70)', 4326));
INSERT INTO example_table(id, geom)
SELECT i,
ST_SetSRID(ST_MakePoint((random() * 20) + 150, (random() * 10) + 70), 4326)
FROM generate_series(3, 10000) i;
-- Set scale_factor to 1.
WITH mvtgeom AS
(
SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)) FROM example_table
WHERE geom && ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)
)
SELECT length(ST_AsMVTEx(mvtgeom.*, 1)) FROM mvtgeom;
length
-------
54237
(1 row)
-- Set scale_factor to 4.
WITH mvtgeom AS
(
SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)) FROM example_table
WHERE geom && ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)
)
SELECT length(ST_AsMVTEx(mvtgeom.*, 4)) FROM mvtgeom;
length
-------
39211
(1 row)
-- Set scale_factor to 4, and the maximum number of vector elements that the MVT can contain to 100.
WITH mvtgeom AS
(
SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)) FROM example_table
WHERE geom && ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)
)
SELECT length(ST_AsMVTEx(mvtgeom.*, 4, 100)) FROM mvtgeom;
length
-------
1117
(1 row)
-- Set scale_factor to 4, set the maximum number of vector elements that the MVT can contain to 100, set the tile resolution to 10000 × 10000, and set the layer name to layer_name.
WITH mvtgeom AS
(
SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(1, 1, 0), 4326), 10000) FROM example_table
WHERE geom && ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)
)
SELECT length(ST_AsMVTEx(mvtgeom.*, 4, 100, 'layer_name', 10000)) FROM mvtgeom;
length
-------
1220
(1 row)
-- Set scale_factor to 4, set the maximum number of vector elements that the MVT can contain to 100, set the tile resolution to 10000 × 10000, and set the layer name to layer_name.
-- Set the geometry column name to geom_field and the ID column name to id_field.
WITH mvtgeom AS
(
SELECT ST_AsMVTGeom(geom, ST_Transform(ST_TileEnvelope(1, 1, 0), 4326), 10000) AS geom_field, id AS id_field FROM example_table
WHERE geom && ST_Transform(ST_TileEnvelope(1, 1, 0), 4326)
)
SELECT length(ST_AsMVTEx(mvtgeom.*, 4, 100, 'layer_name', 10000, 'geom_field', 'id_field')) FROM mvtgeom;
length
-------
1520
(1 row)