forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen-tile-raster.cpp
More file actions
266 lines (223 loc) · 8.53 KB
/
gen-tile-raster.cpp
File metadata and controls
266 lines (223 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "gen-tile-raster.hpp"
#include "canvas.hpp"
#include "hex.hpp"
#include "logging.hpp"
#include "params.hpp"
#include "pgsql.hpp"
#include "raster.hpp"
#include "tile.hpp"
#include "tracer.hpp"
#include "wkb.hpp"
#include <unordered_map>
namespace {
std::size_t round_up(std::size_t value, std::size_t multiple) noexcept
{
return ((value + multiple - 1U) / multiple) * multiple;
}
struct param_canvas_t
{
canvas_t canvas;
std::size_t points = 0;
param_canvas_t(unsigned int image_extent, unsigned int image_buffer)
: canvas(image_extent, image_buffer)
{}
};
using canvas_list_t = std::unordered_map<std::string, param_canvas_t>;
void draw_from_db(double margin, unsigned int image_extent,
unsigned int image_buffer, canvas_list_t *canvas_list,
pg_conn_t *conn, tile_t const &tile)
{
auto const box = tile.box(margin);
auto const result = conn->exec_prepared(
"get_geoms", box.min_x(), box.min_y(), box.max_x(), box.max_y());
for (int n = 0; n < result.num_tuples(); ++n) {
std::string param = result.get_value(n, 1);
auto const geom = ewkb_to_geom(util::decode_hex(result.get(n, 0)));
auto const [it, success] = canvas_list->try_emplace(
std::move(param), image_extent, image_buffer);
it->second.points += it->second.canvas.draw(geom, tile);
}
}
void save_image_to_table(pg_conn_t *connection, canvas_t const &canvas,
tile_t const &tile, double margin,
std::string const ¶m, char const *variant,
std::string const &table_prefix)
{
auto const wkb = util::encode_hex(canvas.to_wkb(tile, margin));
connection->exec("INSERT INTO \"{}_{}\" (type, zoom, x, y, rast)"
" VALUES ('{}', {}, {}, {}, '{}')",
table_prefix, variant, param, tile.zoom(), tile.x(),
tile.y(), wkb);
}
} // anonymous namespace
gen_tile_raster_union_t::gen_tile_raster_union_t(pg_conn_t *connection,
bool append, params_t *params)
: gen_tile_t(connection, append, params), m_timer_draw(add_timer("draw")),
m_timer_simplify(add_timer("simplify")),
m_timer_vectorize(add_timer("vectorize")), m_timer_write(add_timer("write"))
{
check_src_dest_table_params_exist();
m_margin = get_params().get_double("margin");
m_image_extent = uint_in_range(*params, "image_extent", 1024, 65536, 2048);
m_image_buffer =
uint_in_range(*params, "image_buffer", 0, m_image_extent, 0);
m_buffer_size = uint_in_range(*params, "buffer_size", 1, 65536, 10);
m_turdsize = static_cast<int>(
uint_in_range(*params, "turdsize", 0, 65536, m_turdsize));
std::string const where_condition = get_params().get_string("where", "");
if (get_params().has("img_path")) {
m_image_path = get_params().get_string("img_path");
}
if (get_params().has("img_table")) {
m_image_table = get_params().get_string("img_table");
for (char const variant : {'i', 'o'}) {
auto const table_name =
fmt::format("{}_{}", m_image_table, variant);
connection->exec(R"(
CREATE TABLE IF NOT EXISTS "{}" (
type TEXT,
zoom INT4,
x INT4,
y INT4,
rast RASTER
)
)",
table_name);
raster_table_preprocess(table_name);
}
}
if (get_params().get_bool("make_valid")) {
params->set("geom_sql", "(ST_Dump(ST_CollectionExtract("
" ST_MakeValid($1::geometry), 3))).geom");
} else {
params->set("geom_sql", "$1::geometry");
}
if ((m_image_extent & (m_image_extent - 1)) != 0) {
throw fmt_error(
"The 'image_extent' parameter on generalizer{} must be power of 2.",
context());
}
m_image_buffer =
round_up(static_cast<std::size_t>(m_margin *
static_cast<double>(m_image_extent)),
64U);
m_margin = static_cast<double>(m_image_buffer) /
static_cast<double>(m_image_extent);
log_gen("Image extent: {}px, buffer: {}px, margin: {}", m_image_extent,
m_image_buffer, m_margin);
std::string prepare;
if (with_group_by()) {
prepare = R"(
SELECT "{geom_column}", "{group_by_column}"
FROM {src}
WHERE "{geom_column}" &&
ST_MakeEnvelope($1::real, $2::real, $3::real, $4::real, 3857)
)";
dbprepare("insert_geoms", R"(
INSERT INTO {dest} ("{geom_column}", x, y, "{group_by_column}")
VALUES ({geom_sql}, $2::int, $3::int, $4::text)
)");
} else {
prepare = R"(
SELECT "{geom_column}", NULL AS param
FROM {src}
WHERE "{geom_column}" &&
ST_MakeEnvelope($1::real, $2::real, $3::real, $4::real, 3857)
)";
dbprepare("insert_geoms", R"(
INSERT INTO {dest} ("{geom_column}", x, y) VALUES ({geom_sql}, $2::int, $3::int)
)");
}
if (!where_condition.empty()) {
prepare.append(fmt::format(" AND ({})", where_condition));
}
dbprepare("get_geoms", prepare);
}
void gen_tile_raster_union_t::process(tile_t const &tile)
{
connection().exec("BEGIN");
delete_existing(tile);
canvas_list_t canvas_list;
log_gen("Read from database and draw polygons...");
timer(m_timer_draw).start();
draw_from_db(m_margin, m_image_extent, m_image_buffer, &canvas_list,
&connection(), tile);
timer(m_timer_draw).stop();
for (auto &cp : canvas_list) {
auto const ¶m = cp.first;
auto &[canvas, points] = cp.second;
log_gen("Handling param='{}'", param);
if (!m_image_path.empty()) {
// Save input image for debugging
save_image_to_file(canvas, tile, m_image_path, param, "i",
m_image_extent, m_margin);
}
if (!m_image_table.empty()) {
// Store input image in database for debugging
save_image_to_table(&connection(), canvas, tile, m_margin, param,
"i", m_image_table);
}
if (m_buffer_size > 0) {
log_gen("Generalize (buffer={} Mercator units)...",
m_buffer_size * tile.extent() /
static_cast<double>(m_image_extent));
timer(m_timer_simplify).start();
canvas.open_close(m_buffer_size);
timer(m_timer_simplify).stop();
}
if (!m_image_path.empty()) {
// Save output image for debugging
save_image_to_file(canvas, tile, m_image_path, param, "o",
m_image_extent, m_margin);
}
if (!m_image_table.empty()) {
// Store output image in database for debugging
save_image_to_table(&connection(), canvas, tile, m_margin, param,
"o", m_image_table);
}
tracer_t tracer{m_image_extent, m_image_buffer, m_turdsize};
log_gen("Vectorize...");
timer(m_timer_vectorize).start();
auto const geometries = tracer.trace(canvas, tile);
timer(m_timer_vectorize).stop();
log_gen("Reduced from {} points to {} points ({:.1f} %)", points,
tracer.num_points(),
static_cast<double>(tracer.num_points()) /
static_cast<double>(points) * 100);
log_gen("Write geometries to destination table...");
timer(m_timer_write).start();
for (auto const &geom : geometries) {
auto const wkb = geom_to_ewkb(geom);
if (with_group_by()) {
connection().exec_prepared("insert_geoms", binary_param_t{wkb},
tile.x(), tile.y(), param);
} else {
connection().exec_prepared("insert_geoms", binary_param_t{wkb},
tile.x(), tile.y());
}
}
timer(m_timer_write).stop();
log_gen("Inserted {} generalized polygons", geometries.size());
}
connection().exec("COMMIT");
}
void gen_tile_raster_union_t::post()
{
if (!m_image_table.empty()) {
for (char const variant : {'i', 'o'}) {
raster_table_postprocess(
fmt::format("{}_{}", m_image_table, variant));
}
}
if (!append_mode()) {
dbexec("ANALYZE {dest}");
}
}