Skip to content

Commit b99c7d9

Browse files
committed
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.
1 parent f92d491 commit b99c7d9

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ else()
269269
src/gen/gen-rivers.cpp
270270
src/gen/gen-tile-builtup.cpp
271271
src/gen/gen-tile-raster.cpp
272+
src/gen/gen-tile-sql.cpp
272273
src/gen/gen-tile-vector.cpp
273274
src/gen/gen-tile.cpp
274275
src/gen/params.cpp

src/gen/gen-create.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "gen-rivers.hpp"
1414
#include "gen-tile-builtup.hpp"
1515
#include "gen-tile-raster.hpp"
16+
#include "gen-tile-sql.hpp"
1617
#include "gen-tile-vector.hpp"
1718
#include "params.hpp"
1819

@@ -35,6 +36,9 @@ std::unique_ptr<gen_base_t> create_generalizer(std::string const &strategy,
3536
if (strategy == "rivers") {
3637
return std::make_unique<gen_rivers_t>(connection, append, params);
3738
}
39+
if (strategy == "tile-sql") {
40+
return std::make_unique<gen_tile_sql_t>(connection, append, params);
41+
}
3842
if (strategy == "vector-union") {
3943
return std::make_unique<gen_tile_vector_union_t>(connection, append,
4044
params);

src/gen/gen-tile-sql.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include "gen-tile-sql.hpp"
11+
12+
#include "logging.hpp"
13+
#include "params.hpp"
14+
#include "pgsql.hpp"
15+
#include "tile.hpp"
16+
17+
gen_tile_sql_t::gen_tile_sql_t(pg_conn_t *connection, bool append,
18+
params_t *params)
19+
: gen_tile_t(connection, append, params),
20+
m_sql_template(get_params().get_string("sql"))
21+
{
22+
check_src_dest_table_params_exist();
23+
}
24+
25+
void gen_tile_sql_t::process(tile_t const &tile)
26+
{
27+
connection().exec("BEGIN");
28+
delete_existing(tile);
29+
30+
log_gen("Run SQL...");
31+
32+
params_t tmp_params;
33+
tmp_params.set("ZOOM", tile.zoom());
34+
tmp_params.set("X", tile.x());
35+
tmp_params.set("Y", tile.y());
36+
dbexec(tmp_params, m_sql_template);
37+
38+
connection().exec("COMMIT");
39+
log_gen("Done.");
40+
}
41+
42+
void gen_tile_sql_t::post()
43+
{
44+
if (!append_mode()) {
45+
dbexec("ANALYZE {dest}");
46+
}
47+
}

src/gen/gen-tile-sql.hpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef OSM2PGSQL_GEN_TILE_SQL_HPP
2+
#define OSM2PGSQL_GEN_TILE_SQL_HPP
3+
4+
/**
5+
* SPDX-License-Identifier: GPL-2.0-or-later
6+
*
7+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
8+
*
9+
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
10+
* For a full list of authors see the git log.
11+
*/
12+
13+
#include "gen-tile.hpp"
14+
15+
#include <string>
16+
#include <string_view>
17+
18+
class gen_tile_sql_t final : public gen_tile_t
19+
{
20+
public:
21+
gen_tile_sql_t(pg_conn_t *connection, bool append, params_t *params);
22+
23+
~gen_tile_sql_t() override = default;
24+
25+
void process(tile_t const &tile) override;
26+
27+
void post() override;
28+
29+
std::string_view strategy() const noexcept override { return "tile-sql"; }
30+
31+
private:
32+
std::string m_sql_template;
33+
};
34+
35+
#endif // OSM2PGSQL_GEN_TILE_SQL_HPP

0 commit comments

Comments
 (0)