From 354ea659e8af559aac58f54f7b4e00751bc0f72d Mon Sep 17 00:00:00 2001 From: Bertil Chapuis Date: Tue, 7 Jan 2025 21:12:55 +0100 Subject: [PATCH] Fix the query for the basemap --- .../tilestore/postgres/PostgresTileStore.java | 36 +++++++++++-------- .../postgres/PostgresTileStoreTest.java | 2 +- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java index b0a244b53..b040e04f4 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/postgres/PostgresTileStore.java @@ -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 */ @@ -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) { @@ -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) @@ -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 @@ -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); } diff --git a/baremaps-core/src/test/java/org/apache/baremaps/tilestore/postgres/PostgresTileStoreTest.java b/baremaps-core/src/test/java/org/apache/baremaps/tilestore/postgres/PostgresTileStoreTest.java index 3061e762e..03142e10a 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/tilestore/postgres/PostgresTileStoreTest.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/tilestore/postgres/PostgresTileStoreTest.java @@ -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()); } }