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 f13e540 commit 354ea65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 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 All @@ -80,8 +80,8 @@ public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {

// Fetch and compress the tile data
try (var connection = datasource.getConnection();
ByteArrayOutputStream data = new ByteArrayOutputStream();
var statement = connection.prepareStatement(query.sql())) {
ByteArrayOutputStream data = new ByteArrayOutputStream();
var statement = connection.prepareStatement(query.sql())) {

// Set the parameters for the tile
for (int i = 0; i < query.parameters(); i += 3) {
Expand All @@ -94,14 +94,14 @@ public ByteBuffer read(TileCoord tileCoord) throws TileStoreException {
logger.debug("Executing sql for tile {}: {}", tileCoord, statement);

try (ResultSet resultSet = statement.executeQuery();
OutputStream gzip = new GZIPOutputStream(data)) {
OutputStream gzip = new GZIPOutputStream(data)) {
while (resultSet.next()) {
byte[] bytes = resultSet.getBytes(1);
gzip.write(bytes);
}
} catch (Exception e) {
throw new TileStoreException(String.format("Failed to execute statement: %s", statement),
e);
e);
}

// Log slow queries (> 10s)
Expand Down Expand Up @@ -159,15 +159,23 @@ protected static Query prepareQuery(Tileset tileset, int zoom) {

// Add the sql to the layer sql
var querySql = query.getSql().trim()
.replaceAll("\\s+", " ")
.replace(";", "")
.replace("?", "??")
.replace("$zoom", String.valueOf(zoom));
.replaceAll("\\s+", " ")
.replace(";", "")
.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",
querySql);
"""
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);

// Increase the parameter count (e.g. ?) and sql count
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 354ea65

Please sign in to comment.