Skip to content

Commit

Permalink
Optimive mvt query: Remove unneeded SRID and extra ST_Transform calls
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed May 12, 2024
1 parent d63a926 commit ddfe73b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions routes/mvt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ const sql = (params, query) => {
${`xform_id=${query.form_id} AND geom is not null AND deleted_at is null`}
), mvtgeom as (
SELECT
ST_AsMVTGeom (
ST_Transform(${process.env.TABLE_COLUMN}, 3857),
ST_TileEnvelope(${params.z}, ${params.x}, ${params.y})) as geom,
ST_AsMVTGeom (geom, ST_TileEnvelope (${params.z}, ${params.x}, ${params.y})) as geom,
id,
json
${query.columns ? `, ${query.columns}` : ''}
${query.id_column ? `, ${query.id_column}` : ''}
FROM
mvtgeom2,
(SELECT ST_SRID(${process.env.TABLE_COLUMN}) AS srid FROM mvtgeom2 LIMIT 1) a
WHERE
ST_Intersects(
${process.env.TABLE_COLUMN},
ST_Transform(
ST_TileEnvelope(${params.z}, ${params.x}, ${params.y}),
srid
)
)
(
SELECT
id,
json,
ST_Transform (geom, 3857) as geom
FROM
mvtgeom2
) transformed_geom
-- Add where clause only when filtering by bounds
${params.z == 0 && params.x == 0 && params.y == 0 ? `
WHERE
ST_Intersects(
${process.env.TABLE_COLUMN},
ST_TileEnvelope(${params.z}, ${params.x}, ${params.y})
)`: ``}
-- Optional Filter
${query.field_name ? `AND json->>'${query.field_name}'='${query.field_value}'` : ``}
Expand Down

0 comments on commit ddfe73b

Please sign in to comment.