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 8f18ee1
Showing 1 changed file with 14 additions and 9 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

0 comments on commit 8f18ee1

Please sign in to comment.