From 89b76857641f3f5202f638e625e0fb744d1c65ae Mon Sep 17 00:00:00 2001 From: Michael Reichert Date: Wed, 29 Jul 2026 17:01:50 +0200 Subject: [PATCH 1/2] Transform all estimated extents to EPSG:3857 If the database table contains geometries in EPGS:4326, ST_EstimateExtent would return a wrong extent for tile-based processing. --- src/gen/osm2pgsql-gen.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/gen/osm2pgsql-gen.cpp b/src/gen/osm2pgsql-gen.cpp index 4a5bc49af..c90a96067 100644 --- a/src/gen/osm2pgsql-gen.cpp +++ b/src/gen/osm2pgsql-gen.cpp @@ -114,14 +114,21 @@ tile_extent get_extent_from_db(pg_conn_t const &db_connection, result = db_connection.exec( "SELECT ST_XMin(extent), ST_YMin(extent)," " ST_XMax(extent), ST_YMax(extent)" - " FROM raster_columns WHERE r_table_schema='{}'" - " AND r_table_name='{}' AND r_raster_column = '{}'", + " FROM (" + " SELECT ST_Transform(extent, 3857) AS extent" + " FROM raster_columns WHERE r_table_schema='{}'" + " AND r_table_name='{}' AND r_raster_column = '{}'" + " ) a", schema, table, column); } else { result = db_connection.exec( "SELECT ST_XMin(e), ST_YMin(e), ST_XMax(e), ST_YMax(e)" - " FROM ST_EstimatedExtent('{}', '{}', '{}') AS e", - schema, table, column); + " FROM ST_Transform(" + " ST_SetSRID(" + " ST_EstimatedExtent('{}', '{}', '{}'), " + " (SELECT ST_SRID(way) FROM {} LIMIT 1)" + " ), 3857) e", + schema, table, column, qualified_name(schema, table)); } if (result.num_tuples() == 0 || result.is_null(0, 0)) { From aa66da4873cbcd2dfc4e56c2c966dea8ae0bc0f9 Mon Sep 17 00:00:00 2001 From: Michael Reichert Date: Fri, 31 Jul 2026 16:12:14 +0200 Subject: [PATCH 2/2] Remove hardcoded column name, don't return error for srid=0 --- src/gen/osm2pgsql-gen.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/gen/osm2pgsql-gen.cpp b/src/gen/osm2pgsql-gen.cpp index c90a96067..bffb34b48 100644 --- a/src/gen/osm2pgsql-gen.cpp +++ b/src/gen/osm2pgsql-gen.cpp @@ -122,13 +122,25 @@ tile_extent get_extent_from_db(pg_conn_t const &db_connection, schema, table, column); } else { result = db_connection.exec( + "WITH srid AS (" + " SELECT ST_SRID({}) AS srid FROM {} LIMIT 1" + ")" "SELECT ST_XMin(e), ST_YMin(e), ST_XMax(e), ST_YMax(e)" - " FROM ST_Transform(" - " ST_SetSRID(" - " ST_EstimatedExtent('{}', '{}', '{}'), " - " (SELECT ST_SRID(way) FROM {} LIMIT 1)" - " ), 3857) e", - schema, table, column, qualified_name(schema, table)); + " FROM " + " (SELECT CASE WHEN srid.srid = 0" + " THEN ST_EstimatedExtent('{}', '{}', '{}')" + " ELSE" + " ST_Transform(" + " ST_SetSRID(" + " ST_EstimatedExtent('{}', '{}', '{}'), " + " srid.srid" + " )," + " 3857)" + " END AS e" + " FROM srid) e", + column, qualified_name(schema, table), + schema, table, column, + schema, table, column); } if (result.num_tuples() == 0 || result.is_null(0, 0)) {