Skip to content

Commit 4bb963d

Browse files
authored
Merge pull request #1189 from joto/various-code-cleanups
Various code cleanups
2 parents 9d325d3 + 5253461 commit 4bb963d

File tree

6 files changed

+52
-56
lines changed

6 files changed

+52
-56
lines changed

src/options.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,31 @@ options_t::options_t()
304304

305305
options_t::~options_t() {}
306306

307+
static osmium::Box parse_bbox(char const *bbox)
308+
{
309+
double minx, maxx, miny, maxy;
310+
int const n = sscanf(bbox, "%lf,%lf,%lf,%lf", &minx, &miny, &maxx, &maxy);
311+
if (n != 4) {
312+
throw std::runtime_error{"Bounding box must be specified like: "
313+
"minlon,minlat,maxlon,maxlat\n"};
314+
}
315+
316+
if (maxx <= minx) {
317+
throw std::runtime_error{
318+
"Bounding box failed due to maxlon <= minlon\n"};
319+
}
320+
321+
if (maxy <= miny) {
322+
throw std::runtime_error{
323+
"Bounding box failed due to maxlat <= minlat\n"};
324+
}
325+
326+
fmt::print(stderr, "Applying Bounding box: {},{} to {},{}\n", minx, miny,
327+
maxx, maxy);
328+
329+
return osmium::Box{minx, miny, maxx, maxy};
330+
}
331+
307332
options_t::options_t(int argc, char *argv[]) : options_t()
308333
{
309334
int c;
@@ -322,7 +347,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
322347
append = true;
323348
break;
324349
case 'b':
325-
bbox = optarg;
350+
bbox = parse_bbox(optarg);
326351
break;
327352
case 'c':
328353
create = true;

src/options.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "node-ram-cache.hpp"
55
#include "reprojection.hpp"
66

7+
#include <osmium/osm/box.hpp>
8+
79
#include <boost/optional.hpp>
810
#include <memory>
911
#include <string>
@@ -120,7 +122,7 @@ struct options_t
120122
database_options_t database_options;
121123
std::string output_backend{"pgsql"};
122124
std::string input_reader{"auto"};
123-
boost::optional<std::string> bbox{boost::none};
125+
osmium::Box bbox;
124126
bool extra_attributes = false;
125127
bool verbose = false;
126128

src/osm2pgsql.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ static std::shared_ptr<middle_t> create_middle(options_t const &options)
4949

5050
int main(int argc, char *argv[])
5151
{
52-
fmt::print(stderr, "osm2pgsql version {}\n\n", get_osm2pgsql_version());
53-
5452
try {
53+
fmt::print(stderr, "osm2pgsql version {}\n\n", get_osm2pgsql_version());
54+
5555
options_t const options{argc, argv};
5656
if (options.long_usage_bool) {
5757
return 0;

src/osmdata.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,13 @@ class multithreaded_processor
174174
public:
175175
using output_vec_t = std::vector<std::shared_ptr<output_t>>;
176176

177-
multithreaded_processor(std::shared_ptr<middle_t> const &mid,
177+
multithreaded_processor(std::string const &conninfo,
178+
std::shared_ptr<middle_t> const &mid,
178179
output_vec_t outs, size_t thread_count)
179180
: m_outputs(std::move(outs))
180181
{
181182
assert(!m_outputs.empty());
182183

183-
// The database connection info should be the same for all outputs,
184-
// we take it arbitrarily from the first.
185-
std::string const &conninfo =
186-
m_outputs[0]->get_options()->database_options.conninfo();
187-
188184
// For each thread we create clones of all the outputs.
189185
m_clones.resize(thread_count);
190186
for (size_t i = 0; i < thread_count; ++i) {
@@ -387,11 +383,12 @@ void osmdata_t::stop() const
387383
// In append mode there might be dependent objects pending that we
388384
// need to process.
389385
if (opts->append && m_dependency_manager->has_pending()) {
390-
multithreaded_processor proc{m_mid, m_outs,
391-
(std::size_t)opts->num_procs};
386+
multithreaded_processor proc{opts->database_options.conninfo(), m_mid,
387+
m_outs, (std::size_t)opts->num_procs};
392388

393389
proc.process_ways(m_dependency_manager->get_pending_way_ids());
394-
proc.process_relations(m_dependency_manager->get_pending_relation_ids());
390+
proc.process_relations(
391+
m_dependency_manager->get_pending_relation_ids());
395392
proc.merge_expire_trees();
396393
}
397394

src/parse-osmium.cpp

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,10 @@ void parse_stats_t::possibly_print_status()
7272
print_status(now);
7373
}
7474

75-
parse_osmium_t::parse_osmium_t(boost::optional<std::string> const &bbox,
76-
bool do_append, osmdata_t *osmdata)
77-
: m_data(osmdata), m_append(do_append)
78-
{
79-
if (bbox) {
80-
m_bbox = parse_bbox(bbox);
81-
}
82-
}
83-
84-
osmium::Box parse_osmium_t::parse_bbox(boost::optional<std::string> const &bbox)
85-
{
86-
double minx, maxx, miny, maxy;
87-
int const n =
88-
sscanf(bbox->c_str(), "%lf,%lf,%lf,%lf", &minx, &miny, &maxx, &maxy);
89-
if (n != 4) {
90-
throw std::runtime_error{"Bounding box must be specified like: "
91-
"minlon,minlat,maxlon,maxlat\n"};
92-
}
93-
94-
if (maxx <= minx) {
95-
throw std::runtime_error{
96-
"Bounding box failed due to maxlon <= minlon\n"};
97-
}
98-
99-
if (maxy <= miny) {
100-
throw std::runtime_error{
101-
"Bounding box failed due to maxlat <= minlat\n"};
102-
}
103-
104-
fmt::print(stderr, "Applying Bounding box: {},{} to {},{}\n", minx, miny,
105-
maxx, maxy);
106-
107-
return osmium::Box(minx, miny, maxx, maxy);
108-
}
75+
parse_osmium_t::parse_osmium_t(osmium::Box const &bbox, bool append,
76+
osmdata_t *osmdata)
77+
: m_data(osmdata), m_bbox(bbox), m_append(append)
78+
{}
10979

11080
void parse_osmium_t::stream_file(std::string const &filename,
11181
std::string const &fmt)
@@ -162,7 +132,7 @@ void parse_osmium_t::node(osmium::Node const &node)
162132
return;
163133
}
164134

165-
if (!m_bbox || m_bbox->contains(node.location())) {
135+
if (!m_bbox.valid() || m_bbox.contains(node.location())) {
166136
if (m_append) {
167137
m_data->node_modify(node);
168138
} else {

src/parse-osmium.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef OSM2PGSQL_PARSE_OSMIUM_HPP
22
#define OSM2PGSQL_PARSE_OSMIUM_HPP
33

4-
#include <boost/optional.hpp>
54
#include <ctime>
65

76
#include "osmtypes.hpp"
@@ -131,8 +130,7 @@ class parse_stats_t
131130
class parse_osmium_t : public osmium::handler::Handler
132131
{
133132
public:
134-
parse_osmium_t(boost::optional<std::string> const &bbox, bool do_append,
135-
osmdata_t *osmdata);
133+
parse_osmium_t(osmium::Box const &bbox, bool append, osmdata_t *osmdata);
136134

137135
void stream_file(std::string const &filename, std::string const &fmt);
138136

@@ -143,14 +141,18 @@ class parse_osmium_t : public osmium::handler::Handler
143141
parse_stats_t const &stats() const noexcept { return m_stats; }
144142

145143
private:
146-
osmium::Box parse_bbox(boost::optional<std::string> const &bbox);
147-
148144
osmdata_t *m_data;
149-
bool m_append;
150-
boost::optional<osmium::Box> m_bbox;
145+
146+
// Bounding box for node import or invalid Box if everything is imported
147+
osmium::Box m_bbox;
148+
151149
parse_stats_t m_stats;
150+
152151
// Current type being parsed.
153-
osmium::item_type m_type;
152+
osmium::item_type m_type = osmium::item_type::undefined;
153+
154+
// Are we running in append mode?
155+
bool m_append;
154156
};
155157

156158
#endif // OSM2PGSQL_PARSE_OSMIUM_HPP

0 commit comments

Comments
 (0)