-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathflex-table-column.cpp
More file actions
358 lines (313 loc) · 11.4 KB
/
Copy pathflex-table-column.cpp
File metadata and controls
358 lines (313 loc) · 11.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "flex-table-column.hpp"
#include "format.hpp"
#include "geom-boost-adaptor.hpp"
#include "overloaded.hpp"
#include "pgsql-capabilities.hpp"
#include "projection.hpp"
#include "util.hpp"
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <stdexcept>
#include <utility>
#include <vector>
namespace {
struct column_type_lookup
{
char const *m_name;
table_column_type m_type;
char const *name() const noexcept { return m_name; }
};
std::vector<column_type_lookup> const COLUMN_TYPES = {
{{"text", table_column_type::text},
{"boolean", table_column_type::boolean},
{"bool", table_column_type::boolean},
{"int2", table_column_type::int2},
{"smallint", table_column_type::int2},
{"int4", table_column_type::int4},
{"int", table_column_type::int4},
{"integer", table_column_type::int4},
{"int8", table_column_type::int8},
{"bigint", table_column_type::int8},
{"real", table_column_type::real},
{"double", table_column_type::double_precision},
{"timestamp", table_column_type::timestamp},
{"timestamptz", table_column_type::timestamptz},
{"hstore", table_column_type::hstore},
{"json", table_column_type::json},
{"jsonb", table_column_type::jsonb},
{"direction", table_column_type::direction},
{"geometry", table_column_type::geometry},
{"point", table_column_type::point},
{"linestring", table_column_type::linestring},
{"polygon", table_column_type::polygon},
{"multipoint", table_column_type::multipoint},
{"multilinestring", table_column_type::multilinestring},
{"multipolygon", table_column_type::multipolygon},
{"geometrycollection", table_column_type::geometrycollection},
{"id_type", table_column_type::id_type},
{"id_num", table_column_type::id_num}}};
table_column_type get_column_type_from_string(std::string const &type)
{
auto const *column_type = util::find_by_name(COLUMN_TYPES, type);
if (!column_type) {
throw fmt_error("Unknown column type '{}'.", type);
}
return column_type->m_type;
}
std::string lowercase(std::string const &str)
{
std::string result;
for (char const c : str) {
result +=
static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return result;
}
} // anonymous namespace
flex_table_column_t::flex_table_column_t(std::string name,
std::string const &type,
std::string sql_type)
: m_name(std::move(name)), m_type_name(lowercase(type)),
m_sql_type(std::move(sql_type)),
m_type(get_column_type_from_string(m_type_name))
{
if (m_type == table_column_type::hstore) {
if (!has_extension("hstore")) {
throw std::runtime_error{"Extension 'hstore' not available. Use "
"'CREATE EXTENSION hstore;' to load it."};
}
}
}
void flex_table_column_t::set_projection(char const *projection)
{
if (!projection || *projection == '\0') {
return;
}
auto const proj = lowercase(projection);
if (proj == "merc" || proj == "mercator") {
m_srid = PROJ_SPHERE_MERC;
return;
}
if (proj == "latlong" || proj == "latlon" || proj == "wgs84") {
m_srid = PROJ_LATLONG;
return;
}
char *end = nullptr;
m_srid = static_cast<int>(std::strtoul(projection, &end, 10));
if (*end != '\0') {
throw fmt_error("Unknown projection: '{}'.", projection);
}
}
std::string flex_table_column_t::sql_type_name() const
{
if (!m_sql_type.empty()) {
return m_sql_type;
}
switch (m_type) {
case table_column_type::text:
return "text";
case table_column_type::boolean:
return "boolean";
case table_column_type::int2:
return "int2";
case table_column_type::int4:
return "int4";
case table_column_type::int8:
return "int8";
case table_column_type::real:
return "real";
case table_column_type::double_precision:
return "double precision";
case table_column_type::timestamp:
return "timestamp";
case table_column_type::timestamptz:
return "timestamptz";
case table_column_type::hstore:
return "hstore";
case table_column_type::json:
return "json";
case table_column_type::jsonb:
return "jsonb";
case table_column_type::direction:
return "int2";
case table_column_type::geometry:
return fmt::format("Geometry(GEOMETRY, {})", m_srid);
case table_column_type::point:
return fmt::format("Geometry(POINT, {})", m_srid);
case table_column_type::linestring:
return fmt::format("Geometry(LINESTRING, {})", m_srid);
case table_column_type::polygon:
return fmt::format("Geometry(POLYGON, {})", m_srid);
case table_column_type::multipoint:
return fmt::format("Geometry(MULTIPOINT, {})", m_srid);
case table_column_type::multilinestring:
return fmt::format("Geometry(MULTILINESTRING, {})", m_srid);
case table_column_type::multipolygon:
return fmt::format("Geometry(MULTIPOLYGON, {})", m_srid);
case table_column_type::geometrycollection:
return fmt::format("Geometry(GEOMETRYCOLLECTION, {})", m_srid);
case table_column_type::id_type:
return "char(1)";
case table_column_type::id_num:
return "int8";
}
throw std::runtime_error{"Unknown column type."};
}
std::string flex_table_column_t::sql_modifiers() const
{
std::string modifiers;
if (m_not_null) {
modifiers += "NOT NULL ";
}
if (!modifiers.empty()) {
modifiers.resize(modifiers.size() - 1);
}
return modifiers;
}
std::string flex_table_column_t::sql_create() const
{
return fmt::format(R"("{}" {} {})", m_name, sql_type_name(),
sql_modifiers());
}
void flex_table_column_t::add_expire(expire_config_t const &config)
{
assert(is_geometry_column());
assert(srid() == PROJ_SPHERE_MERC);
m_expires.push_back(config);
}
namespace {
/**
* This expires all geometries in "geoms" by themselves. Used when we don't
* need diff expire.
*/
void separate_expire(std::vector<geom::geometry_t> const &geoms,
expire_config_t const &expire_config,
expire_tiles_t &expire_tiles,
std::vector<expire_output_t> *expire_outputs)
{
assert(expire_outputs);
for (auto const &geom : geoms) {
expire_tiles.from_geometry(geom, expire_config);
}
expire_tiles.commit_tiles(&expire_outputs->at(expire_config.expire_output));
}
/**
* When doing diff expire, we need to calculate the symmetric difference
* between old and new geometries. The difference is done by type, so points
* are compared with points, linestrings with linestrings, etc. This function
* separates out the input geometries by the three fundamental types.
*/
// NOLINTBEGIN(cppcoreguidelines-rvalue-reference-param-not-moved)
template <typename T>
void classify_geometries(T input_geoms, geom::multipoint_t *points,
geom::multilinestring_t *linestrings,
geom::multipolygon_t *polygons)
{
assert(points);
assert(linestrings);
assert(polygons);
for (auto &&geom : *input_geoms) {
visit(overloaded{
[&](geom::nullgeom_t && /*input*/) {},
[&](geom::point_t &&input) { points->add_geometry(input); },
[&](geom::linestring_t &&input) {
linestrings->add_geometry(std::move(input));
},
[&](geom::polygon_t &&input) {
polygons->add_geometry(std::move(input));
},
[&](geom::multipoint_t &&input) {
for (auto &&point : input) {
points->add_geometry(point);
}
},
[&](geom::multilinestring_t &&input) {
for (auto &&linestring : input) {
linestrings->add_geometry(std::move(linestring));
}
},
[&](geom::multipolygon_t &&input) {
for (auto &&polygon : input) {
polygons->add_geometry(std::move(polygon));
}
},
[&](geom::collection_t &&input) {
classify_geometries(&input, points, linestrings,
polygons);
}},
std::move(geom));
}
}
// NOLINTEND(cppcoreguidelines-rvalue-reference-param-not-moved)
template <typename T>
void diff_and_expire(geom::multigeometry_t<T> const &old_geoms,
geom::multigeometry_t<T> const &new_geoms,
expire_config_t const &expire_config,
expire_tiles_t &expire_tiles)
{
std::vector<T> diffs;
boost::geometry::sym_difference(old_geoms, new_geoms, diffs);
for (auto const &geom : diffs) {
expire_tiles.from_geometry(geom, expire_config);
}
}
void diff_expire(std::vector<geom::geometry_t> *geoms_old,
std::vector<geom::geometry_t> *geoms_new,
expire_config_t const &expire_config,
expire_tiles_t &expire_tiles,
std::vector<expire_output_t> *expire_outputs)
{
assert(geoms_old);
assert(geoms_new);
assert(expire_outputs);
geom::multipoint_t old_points;
geom::multilinestring_t old_linestrings;
geom::multipolygon_t old_polygons;
classify_geometries(geoms_old, &old_points, &old_linestrings,
&old_polygons);
geom::multipoint_t new_points;
geom::multilinestring_t new_linestrings;
geom::multipolygon_t new_polygons;
classify_geometries(geoms_new, &new_points, &new_linestrings,
&new_polygons);
diff_and_expire(old_points, new_points, expire_config, expire_tiles);
diff_and_expire(old_linestrings, new_linestrings, expire_config,
expire_tiles);
diff_and_expire(old_polygons, new_polygons, expire_config, expire_tiles);
expire_tiles.commit_tiles(&expire_outputs->at(expire_config.expire_output));
}
} // anonymous namespace
void flex_table_column_t::do_expire(
std::vector<geom::geometry_t> *geoms_old,
std::vector<geom::geometry_t> *geoms_new,
std::vector<expire_tiles_t> *expire,
std::vector<expire_output_t> *expire_outputs, bool enable_diff_expire) const
{
assert(geoms_old);
assert(geoms_new);
assert(expire);
assert(expire_outputs);
for (auto const &expire_config : m_expires) {
assert(expire_config.expire_output < expire->size());
auto &expire_tiles = expire->at(expire_config.expire_output);
if (!expire_config.diff_expire || !enable_diff_expire ||
geoms_old->empty() || geoms_new->empty()) {
separate_expire(*geoms_old, expire_config, expire_tiles,
expire_outputs);
separate_expire(*geoms_new, expire_config, expire_tiles,
expire_outputs);
} else {
diff_expire(geoms_old, geoms_new, expire_config, expire_tiles,
expire_outputs);
}
}
}