Skip to content

Commit 0f5b7b6

Browse files
authored
Merge pull request #1163 from joto/cleanup-gazetteer
Various cleanups in gazetteer code
2 parents 487bac7 + 4eaf04b commit 0f5b7b6

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
lines changed

src/gazetteer-style.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <algorithm>
22
#include <cstring>
3+
#include <stdexcept>
34

45
#include <boost/property_tree/json_parser.hpp>
56
#include <osmium/osm.hpp>
@@ -85,9 +86,9 @@ void gazetteer_style_t::load_style(std::string const &filename)
8586

8687
pt::read_json(filename, root);
8788

88-
for (auto &entry : root) {
89-
for (auto &tag : entry.second.get_child("keys")) {
90-
for (auto &value : entry.second.get_child("values")) {
89+
for (auto const &entry : root) {
90+
for (auto const &tag : entry.second.get_child("keys")) {
91+
for (auto const &value : entry.second.get_child("values")) {
9192
add_style_entry(tag.second.data(), value.first,
9293
parse_flags(value.second.data()));
9394
}
@@ -102,7 +103,7 @@ gazetteer_style_t::flag_t gazetteer_style_t::parse_flags(std::string const &str)
102103
std::string::size_type start = 0;
103104

104105
while (start != std::string::npos) {
105-
auto end = str.find(',', start);
106+
auto const end = str.find(',', start);
106107

107108
std::string item;
108109

@@ -236,40 +237,42 @@ void gazetteer_style_t::add_style_entry(std::string const &key,
236237
gazetteer_style_t::flag_t gazetteer_style_t::find_flag(char const *k,
237238
char const *v) const
238239
{
239-
auto klen = std::strlen(k);
240-
auto vlen = std::strlen(v);
240+
auto const klen = std::strlen(k);
241+
auto const vlen = std::strlen(v);
241242

242243
// full match
243-
auto fulllen = klen + vlen + 1U;
244+
auto const fulllen = klen + vlen + 1U;
244245
for (auto const &e : m_matcher) {
245246
switch (e.type) {
246247
case matcher_t::MT_FULL:
247248
if (e.name.size() == fulllen &&
248249
std::strcmp(k, e.name.c_str()) == 0 &&
249-
memcmp(v, e.name.data() + klen + 1, vlen) == 0) {
250+
std::memcmp(v, e.name.data() + klen + 1, vlen) == 0) {
250251
return e.flag;
251252
}
252253
break;
253254
case matcher_t::MT_KEY:
254-
if (e.name.size() == klen && memcmp(k, e.name.data(), klen) == 0) {
255+
if (e.name.size() == klen &&
256+
std::memcmp(k, e.name.data(), klen) == 0) {
255257
return e.flag;
256258
}
257259
break;
258260
case matcher_t::MT_PREFIX:
259261
if (e.name.size() < klen &&
260-
memcmp(k, e.name.data(), e.name.size()) == 0) {
262+
std::memcmp(k, e.name.data(), e.name.size()) == 0) {
261263
return e.flag;
262264
}
263265
break;
264266
case matcher_t::MT_SUFFIX:
265267
if (e.name.size() < klen &&
266-
memcmp(k + klen - e.name.size(), e.name.data(),
267-
e.name.size()) == 0) {
268+
std::memcmp(k + klen - e.name.size(), e.name.data(),
269+
e.name.size()) == 0) {
268270
return e.flag;
269271
}
270272
break;
271273
case matcher_t::MT_VALUE:
272-
if (e.name.size() == vlen && memcmp(v, e.name.data(), vlen) == 0) {
274+
if (e.name.size() == vlen &&
275+
std::memcmp(v, e.name.data(), vlen) == 0) {
273276
return e.flag;
274277
}
275278
break;
@@ -294,11 +297,11 @@ void gazetteer_style_t::process_tags(osmium::OSMObject const &o)
294297
bool is_named = false;
295298

296299
for (auto const &item : o.tags()) {
297-
char const *k = item.key();
298-
char const *v = item.value();
300+
char const *const k = item.key();
301+
char const *const v = item.value();
299302

300303
if (std::strcmp(k, "admin_level") == 0) {
301-
m_admin_level = atoi(v);
304+
m_admin_level = std::atoi(v);
302305
if (m_admin_level <= 0 || m_admin_level > MAX_ADMINLEVEL) {
303306
m_admin_level = MAX_ADMINLEVEL;
304307
}
@@ -309,7 +312,7 @@ void gazetteer_style_t::process_tags(osmium::OSMObject const &o)
309312
m_operator = v;
310313
}
311314

312-
flag_t flag = find_flag(k, v);
315+
flag_t const flag = find_flag(k, v);
313316

314317
if (flag == 0) {
315318
continue;
@@ -348,7 +351,7 @@ void gazetteer_style_t::process_tags(osmium::OSMObject const &o)
348351
// country and postcode are handled specially, ignore them here
349352
if (std::strcmp(addr_key, "country") != 0 &&
350353
std::strcmp(addr_key, "postcode") != 0) {
351-
bool first = std::none_of(
354+
bool const first = std::none_of(
352355
m_address.begin(), m_address.end(), [&](ptag_t const &t) {
353356
return std::strcmp(t.first, addr_key) == 0;
354357
});
@@ -413,7 +416,7 @@ void gazetteer_style_t::filter_main_tags(bool is_named,
413416
// first throw away unnamed mains
414417
auto mend =
415418
std::remove_if(m_main.begin(), m_main.end(), [&](pmaintag_t const &t) {
416-
auto flags = std::get<2>(t);
419+
auto const flags = std::get<2>(t);
417420

418421
if (flags & SF_MAIN_NAMED) {
419422
return !is_named;
@@ -468,7 +471,7 @@ void gazetteer_style_t::copy_out(osmium::OSMObject const &o,
468471
DomainMatcher m{std::get<0>(tag)};
469472
buffer.new_hash();
470473
for (auto const &t : o.tags()) {
471-
char const *k = m(t);
474+
char const *const k = m(t);
472475
if (k) {
473476
buffer.add_hash_elem(k, t.value());
474477
}
@@ -507,9 +510,10 @@ void gazetteer_style_t::copy_out(osmium::OSMObject const &o,
507510
for (auto const &a : m_address) {
508511
if (std::strcmp(a.first, "tiger:county") == 0) {
509512
std::string term;
510-
auto *end = strchr(a.second, ',');
513+
auto const *const end = std::strchr(a.second, ',');
511514
if (end) {
512-
auto len = (std::string::size_type)(end - a.second);
515+
auto const len =
516+
(std::string::size_type)(end - a.second);
513517
term = std::string(a.second, len);
514518
} else {
515519
term = a.second;

src/gazetteer-style.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class gazetteer_style_t
139139
copy_mgr_t &buffer) const;
140140
std::string class_list() const;
141141

142-
bool has_data() const { return !m_main.empty(); }
142+
bool has_data() const noexcept { return !m_main.empty(); }
143143

144144
private:
145145
bool add_metadata_style_entry(std::string const &key);

src/output-gazetteer.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <libpq-fe.h>
2-
31
#include "format.hpp"
42
#include "middle.hpp"
53
#include "options.hpp"
@@ -10,8 +8,8 @@
108
#include "wkb.hpp"
119

1210
#include <cstring>
13-
#include <iostream>
1411
#include <memory>
12+
#include <string>
1513

1614
void output_gazetteer_t::delete_unused_classes(char osm_type, osmid_t osm_id)
1715
{
@@ -20,7 +18,7 @@ void output_gazetteer_t::delete_unused_classes(char osm_type, osmid_t osm_id)
2018

2119
assert(m_style.has_data());
2220

23-
std::string cls = m_style.class_list();
21+
std::string const cls = m_style.class_list();
2422
m_copy.delete_object(osm_type, osm_id, cls);
2523
}
2624
}
@@ -35,18 +33,18 @@ void output_gazetteer_t::delete_unused_full(char osm_type, osmid_t osm_id)
3533

3634
void output_gazetteer_t::start()
3735
{
38-
int srid = m_options.projection->target_srs();
39-
4036
/* (Re)create the table unless we are appending */
4137
if (!m_options.append) {
38+
int const srid = m_options.projection->target_srs();
39+
4240
pg_conn_t conn{m_options.database_options.conninfo()};
4341

4442
/* Drop any existing table */
4543
conn.exec("DROP TABLE IF EXISTS place CASCADE");
4644

4745
/* Create the new table */
4846

49-
std::string sql =
47+
std::string const sql =
5048
"CREATE TABLE place ("
5149
" osm_id int8 NOT NULL,"
5250
" osm_type char(1) NOT NULL,"
@@ -56,15 +54,14 @@ void output_gazetteer_t::start()
5654
" admin_level smallint,"
5755
" address hstore,"
5856
" extratags hstore," +
59-
" geometry Geometry(Geometry,{}) NOT NULL"_format(srid) + ")";
60-
61-
sql += tablespace_clause(m_options.tblsmain_data);
57+
" geometry Geometry(Geometry,{}) NOT NULL"_format(srid) + ")" +
58+
tablespace_clause(m_options.tblsmain_data);
6259

6360
conn.exec(sql);
6461

6562
std::string const index_sql =
66-
"CREATE INDEX place_id_idx ON place "
67-
"USING BTREE (osm_type, osm_id)" +
63+
"CREATE INDEX place_id_idx ON place"
64+
" USING BTREE (osm_type, osm_id)" +
6865
tablespace_clause(m_options.tblsmain_index);
6966
conn.exec(index_sql);
7067
}
@@ -95,7 +92,7 @@ bool output_gazetteer_t::process_node(osmium::Node const &node)
9592
return false;
9693
}
9794

98-
auto wkb = m_builder.get_wkb_node(node.location());
95+
auto const wkb = m_builder.get_wkb_node(node.location());
9996
delete_unused_classes('N', node.id());
10097
m_style.copy_out(node, wkb, m_copy);
10198

@@ -133,7 +130,7 @@ bool output_gazetteer_t::process_way(osmium::Way *way)
133130
geom = m_builder.get_wkb_polygon(*way);
134131
}
135132
if (geom.empty()) {
136-
auto wkbs = m_builder.get_wkb_line(way->nodes(), 0.0);
133+
auto const wkbs = m_builder.get_wkb_line(way->nodes(), 0.0);
137134
if (wkbs.empty()) {
138135
return false;
139136
}
@@ -163,8 +160,7 @@ void output_gazetteer_t::relation_modify(osmium::Relation const &rel)
163160

164161
bool output_gazetteer_t::process_relation(osmium::Relation const &rel)
165162
{
166-
auto const &tags = rel.tags();
167-
char const *type = tags["type"];
163+
char const *const type = rel.tags()["type"];
168164
if (!type) {
169165
return false;
170166
}
@@ -185,20 +181,22 @@ bool output_gazetteer_t::process_relation(osmium::Relation const &rel)
185181
}
186182

187183
/* get the boundary path (ways) */
188-
osmium_buffer.clear();
189-
auto num_ways = m_mid->rel_way_members_get(rel, nullptr, osmium_buffer);
184+
m_osmium_buffer.clear();
185+
auto const num_ways =
186+
m_mid->rel_way_members_get(rel, nullptr, m_osmium_buffer);
190187

191188
if (num_ways == 0) {
192189
return false;
193190
}
194191

195-
for (auto &w : osmium_buffer.select<osmium::Way>()) {
192+
for (auto &w : m_osmium_buffer.select<osmium::Way>()) {
196193
m_mid->nodes_get_list(&(w.nodes()));
197194
}
198195

199-
auto geoms = is_waterway
200-
? m_builder.get_wkb_multiline(osmium_buffer, 0.0)
201-
: m_builder.get_wkb_multipolygon(rel, osmium_buffer, true);
196+
auto const geoms =
197+
is_waterway
198+
? m_builder.get_wkb_multiline(m_osmium_buffer, 0.0)
199+
: m_builder.get_wkb_multipolygon(rel, m_osmium_buffer, true);
202200

203201
if (geoms.empty()) {
204202
return false;

src/output-gazetteer.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define OSM2PGSQL_OUTPUT_GAZETTEER_HPP
33

44
#include <memory>
5-
#include <string>
65

76
#include <osmium/memory/buffer.hpp>
87

@@ -20,7 +19,7 @@ class output_gazetteer_t : public output_t
2019
std::shared_ptr<db_copy_thread_t> const &copy_thread)
2120
: output_t(cloned_mid, other->m_options), m_copy(copy_thread),
2221
m_builder(other->m_options.projection),
23-
osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
22+
m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
2423
{}
2524

2625
public:
@@ -29,7 +28,7 @@ class output_gazetteer_t : public output_t
2928
std::shared_ptr<db_copy_thread_t> const &copy_thread)
3029
: output_t(mid, options), m_copy(copy_thread),
3130
m_builder(options.projection),
32-
osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
31+
m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
3332
{
3433
m_style.load_style(options.style);
3534
}
@@ -43,13 +42,13 @@ class output_gazetteer_t : public output_t
4342
}
4443

4544
void start() override;
46-
void stop(osmium::thread::Pool *) override {}
45+
void stop(osmium::thread::Pool *) noexcept override {}
4746
void commit() override;
4847

4948
bool need_forward_dependencies() const noexcept override { return false; }
5049

51-
void pending_way(osmid_t, bool) override {}
52-
void pending_relation(osmid_t, bool) override {}
50+
void pending_way(osmid_t, bool) noexcept override {}
51+
void pending_relation(osmid_t, bool) noexcept override {}
5352

5453
void node_add(osmium::Node const &node) override;
5554
void way_add(osmium::Way *way) override;
@@ -81,7 +80,7 @@ class output_gazetteer_t : public output_t
8180
gazetteer_style_t m_style;
8281

8382
geom::osmium_builder_t m_builder;
84-
osmium::memory::Buffer osmium_buffer;
83+
osmium::memory::Buffer m_osmium_buffer;
8584
};
8685

8786
#endif // OSM2PGSQL_OUTPUT_GAZETTEER_HPP

0 commit comments

Comments
 (0)