forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagtransform-c.cpp
More file actions
341 lines (296 loc) · 11 KB
/
tagtransform-c.cpp
File metadata and controls
341 lines (296 loc) · 11 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
/**
* 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 <array>
#include <cstdlib>
#include <cstring>
#include "options.hpp"
#include "taginfo-impl.hpp"
#include "tagtransform-c.hpp"
#include "util.hpp"
#include "wildcmp.hpp"
namespace {
struct layers_type
{
char const *highway;
int offset;
bool roads;
};
constexpr std::array<layers_type, 25> const LAYERS = {
{{"proposed", 1, false}, {"construction", 2, false},
{"steps", 10, false}, {"cycleway", 10, false},
{"bridleway", 10, false}, {"footway", 10, false},
{"path", 10, false}, {"track", 11, false},
{"service", 15, false},
{"tertiary_link", 24, false}, {"secondary_link", 25, true},
{"primary_link", 27, true}, {"trunk_link", 28, true},
{"motorway_link", 29, true},
{"raceway", 30, false}, {"pedestrian", 31, false},
{"living_street", 32, false}, {"road", 33, false},
{"unclassified", 33, false}, {"residential", 33, false},
{"tertiary", 34, false}, {"secondary", 36, true},
{"primary", 37, true}, {"trunk", 38, true},
{"motorway", 39, true}}};
void add_z_order(taglist_t *tags, bool *roads)
{
std::string const *const layer = tags->get("layer");
std::string const *const highway = tags->get("highway");
bool const bridge = tags->get_bool("bridge", false);
bool const tunnel = tags->get_bool("tunnel", false);
std::string const *const railway = tags->get("railway");
std::string const *const boundary = tags->get("boundary");
int z_order = 0;
int const l = layer ? (int)std::strtol(layer->c_str(), nullptr, 10) : 0;
z_order = 100 * l;
*roads = false;
if (highway) {
for (auto const &layer : LAYERS) {
if (*highway == layer.highway) {
z_order += layer.offset;
*roads = layer.roads;
break;
}
}
}
if (railway && !railway->empty()) {
z_order += 35;
*roads = true;
}
/* Administrative boundaries are rendered at low zooms so we prefer to use the roads table */
if (boundary && *boundary == "administrative") {
*roads = true;
}
if (bridge) {
z_order += 100;
}
if (tunnel) {
z_order -= 100;
}
tags->add_tag("z_order", fmt::to_string(z_order));
}
bool starts_with(char const *input, std::string const &test) noexcept
{
return std::strncmp(input, test.c_str(), test.size()) == 0;
}
} // anonymous namespace
c_tagtransform_t::c_tagtransform_t(options_t const *options, export_list exlist)
: m_options(options), m_export_list(std::move(exlist))
{}
std::unique_ptr<tagtransform_t> c_tagtransform_t::clone() const
{
return std::make_unique<c_tagtransform_t>(m_options, m_export_list);
}
bool c_tagtransform_t::check_key(std::vector<taginfo> const &infos,
char const *k, bool *filter,
unsigned int *flags)
{
//go through the actual tags found on the item and keep the ones in the export list
for (auto const &info : infos) {
if (info.flags & FLAG_DELETE) {
if (wild_match(info.name.c_str(), k)) {
return false;
}
} else if (std::strcmp(info.name.c_str(), k) == 0) {
*filter = false;
*flags |= info.flags;
return true;
}
}
// if we didn't find any tags that we wanted to export
if (m_options->hstore_mode != hstore_column::none) {
/* ... but if hstore_match_only is set then don't take this
as a reason for keeping the object */
if (!m_options->hstore_match_only) {
*filter = false;
}
/* with hstore, copy all tags... */
return true;
}
if (!m_options->hstore_columns.empty()) {
/* does this column match any of the hstore column prefixes? */
for (auto const &column : m_options->hstore_columns) {
if (starts_with(k, column)) {
/* ... but if hstore_match_only is set then don't take this
as a reason for keeping the object */
if (!m_options->hstore_match_only) {
*filter = false;
}
return true;
}
}
}
return false;
}
bool c_tagtransform_t::filter_tags(osmium::OSMObject const &o, bool *polygon,
bool *roads, taglist_t *out_tags)
{
//assume we dont like this set of tags
bool filter = true;
unsigned int flags = 0;
int add_area_tag = 0;
auto export_type = o.type();
if (o.type() == osmium::item_type::relation) {
export_type = osmium::item_type::way;
}
auto const &infos = m_export_list.get(export_type);
/* We used to only go far enough to determine if it's a polygon or not,
but now we go through and filter stuff we don't need
pop each tag off and keep it in the temp list if we like it */
for (auto const &item : o.tags()) {
char const *const k = item.key();
char const *const v = item.value();
if (o.type() == osmium::item_type::relation &&
std::strcmp("type", k) == 0) {
out_tags->add_tag(k, v);
continue;
}
/* Allow named islands to appear as polygons */
if (std::strcmp("natural", k) == 0 &&
std::strcmp("coastline", v) == 0) {
add_area_tag = 1;
/* Discard natural=coastline tags (we render these from a shapefile instead) */
if (!m_options->keep_coastlines) {
continue;
}
}
//go through the actual tags found on the item and keep the ones in the export list
if (check_key(infos, k, &filter, &flags)) {
out_tags->add_tag(k, v);
}
}
if (m_options->extra_attributes && o.version() > 0) {
out_tags->add_attributes(o);
}
if (polygon) {
if (add_area_tag) {
/* If we need to force this as a polygon, append an area tag */
out_tags->add_tag_if_not_exists("area", "yes");
*polygon = true;
} else {
auto const *area = o.tags()["area"];
if (area) {
*polygon = taglist_t::value_to_bool(area, flags & FLAG_POLYGON);
} else {
*polygon = flags & FLAG_POLYGON;
}
}
}
if (roads && !filter && (o.type() == osmium::item_type::way)) {
add_z_order(out_tags, roads);
}
return filter;
}
bool c_tagtransform_t::filter_rel_member_tags(
taglist_t const &rel_tags, osmium::memory::Buffer const &,
rolelist_t const &, bool *make_boundary, bool *make_polygon, bool *roads,
taglist_t *out_tags)
{
std::string const *type = rel_tags.get("type");
if (!type) {
return true;
}
bool is_route = false;
bool is_boundary = false;
bool is_multipolygon = false;
if (*type == "route") {
is_route = true;
} else if (*type == "boundary") {
is_boundary = true;
} else if (*type == "multipolygon") {
is_multipolygon = true;
} else {
return true;
}
/* Clone tags from relation */
for (auto const &rel_tag : rel_tags) {
//copy the name tag as "route_name"
if (is_route && (rel_tag.key == "name")) {
out_tags->add_tag_if_not_exists("route_name", rel_tag.value);
}
//copy all other tags except for "type"
if (rel_tag.key != "type") {
out_tags->add_tag_if_not_exists(rel_tag);
}
}
if (out_tags->empty()) {
return true;
}
if (is_route) {
std::string const *netw = rel_tags.get("network");
int networknr = -1;
if (netw != nullptr) {
std::string const *state = rel_tags.get("state");
std::string statetype("yes");
if (state) {
if (*state == "alternate") {
statetype = "alternate";
} else if (*state == "connection") {
statetype = "connection";
}
}
if (*netw == "lcn") {
networknr = 10;
out_tags->add_tag_if_not_exists("lcn", statetype);
} else if (*netw == "rcn") {
networknr = 11;
out_tags->add_tag_if_not_exists("rcn", statetype);
} else if (*netw == "ncn") {
networknr = 12;
out_tags->add_tag_if_not_exists("ncn", statetype);
} else if (*netw == "lwn") {
networknr = 20;
out_tags->add_tag_if_not_exists("lwn", statetype);
} else if (*netw == "rwn") {
networknr = 21;
out_tags->add_tag_if_not_exists("rwn", statetype);
} else if (*netw == "nwn") {
networknr = 22;
out_tags->add_tag_if_not_exists("nwn", statetype);
}
}
std::string const *prefcol = rel_tags.get("preferred_color");
if (prefcol != nullptr && prefcol->size() == 1) {
if ((*prefcol)[0] == '0' || (*prefcol)[0] == '1' ||
(*prefcol)[0] == '2' || (*prefcol)[0] == '3' ||
(*prefcol)[0] == '4') {
out_tags->add_tag_if_not_exists("route_pref_color", *prefcol);
} else {
out_tags->add_tag_if_not_exists("route_pref_color", "0");
}
} else {
out_tags->add_tag_if_not_exists("route_pref_color", "0");
}
std::string const *relref = rel_tags.get("ref");
if (relref != nullptr) {
if (networknr == 10) {
out_tags->add_tag_if_not_exists("lcn_ref", *relref);
} else if (networknr == 11) {
out_tags->add_tag_if_not_exists("rcn_ref", *relref);
} else if (networknr == 12) {
out_tags->add_tag_if_not_exists("ncn_ref", *relref);
} else if (networknr == 20) {
out_tags->add_tag_if_not_exists("lwn_ref", *relref);
} else if (networknr == 21) {
out_tags->add_tag_if_not_exists("rwn_ref", *relref);
} else if (networknr == 22) {
out_tags->add_tag_if_not_exists("nwn_ref", *relref);
}
}
} else if (is_boundary ||
(is_multipolygon && out_tags->contains("boundary"))) {
/* Boundaries will get converted into multiple geometries:
- Linear features will end up in the line and roads tables (useful for admin boundaries)
- Polygon features also go into the polygon table (useful for national_forests)
The edges of the polygon also get treated as linear fetaures allowing these to be rendered seperately. */
*make_boundary = true;
} else if (is_multipolygon) {
*make_polygon = true;
}
add_z_order(out_tags, roads);
return false;
}