Skip to content

Commit

Permalink
Append the spatial condition to the user provided query
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Jan 8, 2025
1 parent 2742f27 commit 1e7c67c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.sql.*;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.GZIPOutputStream;
Expand Down Expand Up @@ -163,20 +163,25 @@ protected static Query prepareQuery(Tileset tileset, int zoom) {
.replace(";", "")
.replace("?", "??")
.replace("$zoom", String.valueOf(zoom));

// Append a new condition or a where clause
if (querySql.toLowerCase().contains("where")) {
querySql += " AND ";
} else {
querySql += " WHERE ";
}

// Append the condition to the query sql
querySql +=
"geom IS NOT NULL AND geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))";

var querySqlWithParams = String.format(
"""
SELECT
tile.id AS id,
tile.tags - 'id' AS tags,
ST_AsMVTGeom(tile.geom, ST_TileEnvelope(?, ?, ?)) AS geom
FROM (
SELECT
data.id AS id,
data.tags AS tags,
data.geom AS geom
FROM (%s) AS data
WHERE data.geom IS NOT NULL
AND data.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) AS tile
FROM (%s) as tile
""",
querySql);
layerSql.append(querySqlWithParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void prepareQuery() {
List.of(new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table")))));
var query = PostgresTileStore.prepareQuery(tileset, 10);
assertEquals(
"SELECT (SELECT ST_AsMVT(mvtGeom.*, 'a') FROM (SELECT tile.id AS id, tile.tags - 'id' AS tags, ST_AsMVTGeom(tile.geom, ST_TileEnvelope(?, ?, ?)) AS geom FROM ( SELECT data.id AS id, data.tags AS tags, data.geom AS geom FROM (SELECT id, tags, geom FROM table) AS data WHERE data.geom IS NOT NULL AND data.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) AS tile ) AS mvtGeom) || (SELECT ST_AsMVT(mvtGeom.*, 'b') FROM (SELECT tile.id AS id, tile.tags - 'id' AS tags, ST_AsMVTGeom(tile.geom, ST_TileEnvelope(?, ?, ?)) AS geom FROM ( SELECT data.id AS id, data.tags AS tags, data.geom AS geom FROM (SELECT id, tags, geom FROM table) AS data WHERE data.geom IS NOT NULL AND data.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) AS tile ) AS mvtGeom) AS mvtTile",
"SELECT (SELECT ST_AsMVT(mvtGeom.*, 'a') FROM (SELECT tile.id AS id, tile.tags - 'id' AS tags, ST_AsMVTGeom(tile.geom, ST_TileEnvelope(?, ?, ?)) AS geom FROM (SELECT id, tags, geom FROM table WHERE geom IS NOT NULL AND geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) as tile ) AS mvtGeom) || (SELECT ST_AsMVT(mvtGeom.*, 'b') FROM (SELECT tile.id AS id, tile.tags - 'id' AS tags, ST_AsMVTGeom(tile.geom, ST_TileEnvelope(?, ?, ?)) AS geom FROM (SELECT id, tags, geom FROM table WHERE geom IS NOT NULL AND geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) as tile ) AS mvtGeom) AS mvtTile",
query.sql());
}
}

0 comments on commit 1e7c67c

Please sign in to comment.