From b99c7d9b6db860f4319669cbb6c1ce2df4c65da0 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 28 Apr 2024 17:14:42 +0200 Subject: [PATCH] Add tile-based generalizer calling SQL commands It is named "tile-sql". Parameters are "name", "src_table", "dest_table", "zoom", and "sql", the command to be called. Use {ZOOM}, {X}, and {Y} in the command to set the zoom level and tile coordinates. --- CMakeLists.txt | 1 + src/gen/gen-create.cpp | 4 ++++ src/gen/gen-tile-sql.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ src/gen/gen-tile-sql.hpp | 35 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 src/gen/gen-tile-sql.cpp create mode 100644 src/gen/gen-tile-sql.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 109778ae0..2fa2491aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,7 @@ else() src/gen/gen-rivers.cpp src/gen/gen-tile-builtup.cpp src/gen/gen-tile-raster.cpp + src/gen/gen-tile-sql.cpp src/gen/gen-tile-vector.cpp src/gen/gen-tile.cpp src/gen/params.cpp diff --git a/src/gen/gen-create.cpp b/src/gen/gen-create.cpp index e26db109c..195f59c16 100644 --- a/src/gen/gen-create.cpp +++ b/src/gen/gen-create.cpp @@ -13,6 +13,7 @@ #include "gen-rivers.hpp" #include "gen-tile-builtup.hpp" #include "gen-tile-raster.hpp" +#include "gen-tile-sql.hpp" #include "gen-tile-vector.hpp" #include "params.hpp" @@ -35,6 +36,9 @@ std::unique_ptr create_generalizer(std::string const &strategy, if (strategy == "rivers") { return std::make_unique(connection, append, params); } + if (strategy == "tile-sql") { + return std::make_unique(connection, append, params); + } if (strategy == "vector-union") { return std::make_unique(connection, append, params); diff --git a/src/gen/gen-tile-sql.cpp b/src/gen/gen-tile-sql.cpp new file mode 100644 index 000000000..0389568cb --- /dev/null +++ b/src/gen/gen-tile-sql.cpp @@ -0,0 +1,47 @@ +/** + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This file is part of osm2pgsql (https://osm2pgsql.org/). + * + * Copyright (C) 2006-2024 by the osm2pgsql developer community. + * For a full list of authors see the git log. + */ + +#include "gen-tile-sql.hpp" + +#include "logging.hpp" +#include "params.hpp" +#include "pgsql.hpp" +#include "tile.hpp" + +gen_tile_sql_t::gen_tile_sql_t(pg_conn_t *connection, bool append, + params_t *params) +: gen_tile_t(connection, append, params), + m_sql_template(get_params().get_string("sql")) +{ + check_src_dest_table_params_exist(); +} + +void gen_tile_sql_t::process(tile_t const &tile) +{ + connection().exec("BEGIN"); + delete_existing(tile); + + log_gen("Run SQL..."); + + params_t tmp_params; + tmp_params.set("ZOOM", tile.zoom()); + tmp_params.set("X", tile.x()); + tmp_params.set("Y", tile.y()); + dbexec(tmp_params, m_sql_template); + + connection().exec("COMMIT"); + log_gen("Done."); +} + +void gen_tile_sql_t::post() +{ + if (!append_mode()) { + dbexec("ANALYZE {dest}"); + } +} diff --git a/src/gen/gen-tile-sql.hpp b/src/gen/gen-tile-sql.hpp new file mode 100644 index 000000000..a19904e87 --- /dev/null +++ b/src/gen/gen-tile-sql.hpp @@ -0,0 +1,35 @@ +#ifndef OSM2PGSQL_GEN_TILE_SQL_HPP +#define OSM2PGSQL_GEN_TILE_SQL_HPP + +/** + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This file is part of osm2pgsql (https://osm2pgsql.org/). + * + * Copyright (C) 2006-2024 by the osm2pgsql developer community. + * For a full list of authors see the git log. + */ + +#include "gen-tile.hpp" + +#include +#include + +class gen_tile_sql_t final : public gen_tile_t +{ +public: + gen_tile_sql_t(pg_conn_t *connection, bool append, params_t *params); + + ~gen_tile_sql_t() override = default; + + void process(tile_t const &tile) override; + + void post() override; + + std::string_view strategy() const noexcept override { return "tile-sql"; } + +private: + std::string m_sql_template; +}; + +#endif // OSM2PGSQL_GEN_TILE_SQL_HPP