Skip to content

Commit

Permalink
Fix the query for the basemap
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Jan 7, 2025
1 parent 0ff60b8 commit 7acde84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PostgresTileStore(DataSource datasource, Tileset tileset) {

/**
* A record that holds the sql of a prepared statement and the number of parameters.
*
*
* @param sql
* @param parameters
*/
Expand Down Expand Up @@ -164,9 +164,17 @@ protected static Query prepareQuery(Tileset tileset, int zoom) {
.replace("?", "??")
.replace("$zoom", String.valueOf(zoom));
var querySqlWithParams = String.format(
"SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags - 'id' AS tags, t.id AS id "
+ "FROM ((%s) AS t1 WHERE t1.geom IS NOT NULL "
+ "AND t1.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) as t",
"""
SELECT
id AS id,
tags - 'id' AS tags,
ST_AsMVTGeom(geom, ST_TileEnvelope(?, ?, ?)) AS geom
FROM (
SELECT id, tags, geom
FROM (%s)
WHERE geom IS NOT NULL
AND geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096)))
""",
querySql);
layerSql.append(querySqlWithParams);

Expand Down Expand Up @@ -201,7 +209,7 @@ protected static Query prepareQuery(Tileset tileset, int zoom) {
tileSql.append(tileQueryTail);

// Format the sql query
var sql = tileSql.toString().replace("\n", " ");
var sql = tileSql.toString().replaceAll("\\s+", " ");

return new Query(sql, paramCount);
}
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 ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags - 'id' AS tags, t.id AS id FROM ((SELECT id, tags, geom FROM table) AS t1 WHERE t1.geom IS NOT NULL AND t1.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) as t) AS mvtGeom) || (SELECT ST_AsMVT(mvtGeom.*, 'b') FROM (SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags - 'id' AS tags, t.id AS id FROM ((SELECT id, tags, geom FROM table) AS t1 WHERE t1.geom IS NOT NULL AND t1.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) as t) AS mvtGeom) AS mvtTile",
"SELECT (SELECT ST_AsMVT(mvtGeom.*, 'a') FROM (SELECT id AS id, tags - 'id' AS tags, ST_AsMVTGeom(geom, ST_TileEnvelope(?, ?, ?)) AS geom FROM ( SELECT id, tags, geom FROM (SELECT id, tags, geom FROM table) WHERE geom IS NOT NULL AND geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) ) AS mvtGeom) || (SELECT ST_AsMVT(mvtGeom.*, 'b') FROM (SELECT id AS id, tags - 'id' AS tags, ST_AsMVTGeom(geom, ST_TileEnvelope(?, ?, ?)) AS geom FROM ( SELECT id, tags, geom FROM (SELECT id, tags, geom FROM table) WHERE geom IS NOT NULL AND geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) ) AS mvtGeom) AS mvtTile",
query.sql());
}
}

0 comments on commit 7acde84

Please sign in to comment.