Skip to content

Commit 6ac5340

Browse files
committed
Split parse-osmium into input-handler and progress-display
Rename classes and put them in separate files. parse_osmium_t gets the new name input_handler_t and parse_stats_t gets the new name progress_display_t.
1 parent 8702ad6 commit 6ac5340

File tree

9 files changed

+247
-219
lines changed

9 files changed

+247
-219
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(osm2pgsql_lib_SOURCES
77
geometry-processor.cpp
88
geom-transform.cpp
99
id-tracker.cpp
10+
input-handler.cpp
1011
middle-pgsql.cpp
1112
middle-ram.cpp
1213
node-persistent-cache.cpp
@@ -19,11 +20,11 @@ set(osm2pgsql_lib_SOURCES
1920
output-null.cpp
2021
output-pgsql.cpp
2122
output.cpp
22-
parse-osmium.cpp
2323
pgsql.cpp
2424
processor-line.cpp
2525
processor-point.cpp
2626
processor-polygon.cpp
27+
progress-display.cpp
2728
reprojection.cpp
2829
sprompt.cpp
2930
table.cpp
@@ -39,6 +40,7 @@ set(osm2pgsql_lib_SOURCES
3940
geometry-processor.hpp
4041
geom-transform.hpp
4142
id-tracker.hpp
43+
input-handler.hpp
4244
middle-pgsql.hpp
4345
middle-ram.hpp
4446
middle.hpp
@@ -53,11 +55,11 @@ set(osm2pgsql_lib_SOURCES
5355
output-null.hpp
5456
output-pgsql.hpp
5557
output.hpp
56-
parse-osmium.hpp
5758
pgsql.hpp
5859
processor-line.hpp
5960
processor-point.hpp
6061
processor-polygon.hpp
62+
progress-display.hpp
6163
reprojection.hpp
6264
sprompt.hpp
6365
table.hpp

src/input-handler.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <osmium/osm.hpp>
2+
3+
#include "format.hpp"
4+
#include "input-handler.hpp"
5+
#include "osmdata.hpp"
6+
7+
input_handler_t::input_handler_t(osmium::Box const &bbox, bool append,
8+
osmdata_t const *osmdata)
9+
: m_data(osmdata), m_bbox(bbox), m_append(append)
10+
{}
11+
12+
void input_handler_t::node(osmium::Node const &node)
13+
{
14+
if (m_type != osmium::item_type::node) {
15+
m_type = osmium::item_type::node;
16+
m_data->flush();
17+
}
18+
19+
if (node.deleted()) {
20+
if (!m_append) {
21+
throw std::runtime_error{"Input file contains deleted objects but "
22+
"you are not in append mode."};
23+
}
24+
m_data->node_delete(node.id());
25+
} else {
26+
// if the node is not valid, then node.location.lat/lon() can throw.
27+
// we probably ought to treat invalid locations as if they were
28+
// deleted and ignore them.
29+
if (!node.location().valid()) {
30+
fmt::print(
31+
stderr,
32+
"WARNING: Node {} (version {}) has an invalid location and has "
33+
"been ignored. This is not expected to happen with recent "
34+
"planet files, so please check that your input is correct.\n",
35+
node.id(), node.version());
36+
37+
return;
38+
}
39+
40+
if (!m_bbox.valid() || m_bbox.contains(node.location())) {
41+
if (m_append) {
42+
m_data->node_modify(node);
43+
} else {
44+
m_data->node_add(node);
45+
}
46+
m_progress.add_node(node.id());
47+
}
48+
}
49+
}
50+
51+
void input_handler_t::way(osmium::Way &way)
52+
{
53+
if (m_type != osmium::item_type::way) {
54+
m_type = osmium::item_type::way;
55+
m_data->flush();
56+
}
57+
58+
if (way.deleted()) {
59+
if (!m_append) {
60+
throw std::runtime_error{"Input file contains deleted objects but "
61+
"you are not in append mode."};
62+
}
63+
m_data->way_delete(way.id());
64+
} else {
65+
if (m_append) {
66+
m_data->way_modify(&way);
67+
} else {
68+
m_data->way_add(&way);
69+
}
70+
}
71+
m_progress.add_way(way.id());
72+
}
73+
74+
void input_handler_t::relation(osmium::Relation const &rel)
75+
{
76+
if (m_type != osmium::item_type::relation) {
77+
m_type = osmium::item_type::relation;
78+
m_data->flush();
79+
}
80+
81+
if (rel.deleted()) {
82+
if (!m_append) {
83+
throw std::runtime_error{"Input file contains deleted objects but "
84+
"you are not in append mode."};
85+
}
86+
m_data->relation_delete(rel.id());
87+
} else {
88+
if (rel.members().size() > 32767) {
89+
return;
90+
}
91+
if (m_append) {
92+
m_data->relation_modify(rel);
93+
} else {
94+
m_data->relation_add(rel);
95+
}
96+
}
97+
m_progress.add_rel(rel.id());
98+
}

src/input-handler.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef OSM2PGSQL_INPUT_HANDLER_HPP
2+
#define OSM2PGSQL_INPUT_HANDLER_HPP
3+
4+
/**
5+
* \file
6+
*
7+
* This file is part of osm2pgsql (https://github.com/openstreetmap/osm2pgsql).
8+
*
9+
* It contains the input_handler_t class.
10+
*/
11+
12+
#include <osmium/fwd.hpp>
13+
#include <osmium/handler.hpp>
14+
#include <osmium/osm/box.hpp>
15+
16+
#include "progress-display.hpp"
17+
18+
class osmdata_t;
19+
20+
/**
21+
* When an OSM file is read, this handler is called for each node, way, and
22+
* relation. Depending on the processing mode (create or append), the type
23+
* of object and whether an object is added or deleted, the right functions
24+
* of the osmdata_t class are called.
25+
*/
26+
class input_handler_t : public osmium::handler::Handler
27+
{
28+
public:
29+
input_handler_t(osmium::Box const &bbox, bool append,
30+
osmdata_t const *osmdata);
31+
32+
void node(osmium::Node const &node);
33+
void way(osmium::Way &way);
34+
void relation(osmium::Relation const &rel);
35+
36+
progress_display_t const &progress() const noexcept { return m_progress; }
37+
38+
private:
39+
osmdata_t const *m_data;
40+
41+
// Bounding box for node import (or invalid Box if everything should be
42+
// imported).
43+
osmium::Box m_bbox;
44+
45+
// The progress meter will be updated as we go.
46+
progress_display_t m_progress;
47+
48+
// Current type being parsed.
49+
osmium::item_type m_type = osmium::item_type::node;
50+
51+
// Are we running in append mode?
52+
bool m_append;
53+
};
54+
55+
#endif // OSM2PGSQL_INPUT_HANDLER_HPP

src/osm2pgsql.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "options.hpp"
3030
#include "osmdata.hpp"
3131
#include "output.hpp"
32+
#include "progress-display.hpp"
3233
#include "reprojection.hpp"
3334
#include "util.hpp"
3435
#include "version.hpp"
@@ -84,7 +85,7 @@ int main(int argc, char *argv[])
8485

8586
// Processing: In this phase the input file(s) are read and parsed,
8687
// populating some of the tables.
87-
parse_stats_t stats;
88+
progress_display_t progress;
8889
for (auto const &filename : options.input_files) {
8990
fmt::print(stderr, "\nReading in file: {}\n", filename);
9091
util::timer_t timer_parse;
@@ -99,13 +100,13 @@ int main(int argc, char *argv[])
99100
"Unknown file format '{}'."_format(options.input_format)};
100101
}
101102

102-
stats.update(osmdata.process_file(file, options.bbox));
103+
progress.update(osmdata.process_file(file, options.bbox));
103104

104-
stats.print_status(std::time(nullptr));
105+
progress.print_status(std::time(nullptr));
105106
fmt::print(stderr, " parse time: {}s\n", timer_parse.stop());
106107
}
107108

108-
stats.print_summary();
109+
progress.print_summary();
109110

110111
// Process pending ways and relations. Cluster database tables and
111112
// create indexes.

src/osmdata.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "db-copy.hpp"
1616
#include "format.hpp"
17+
#include "input-handler.hpp"
1718
#include "middle.hpp"
1819
#include "options.hpp"
1920
#include "osmdata.hpp"
@@ -369,8 +370,8 @@ class multithreaded_processor
369370

370371
} // anonymous namespace
371372

372-
parse_stats_t osmdata_t::process_file(osmium::io::File const &file,
373-
osmium::Box const &bbox) const
373+
progress_display_t osmdata_t::process_file(osmium::io::File const &file,
374+
osmium::Box const &bbox) const
374375
{
375376
if (!m_append && file.has_multiple_object_versions()) {
376377
throw std::runtime_error{
@@ -380,12 +381,12 @@ parse_stats_t osmdata_t::process_file(osmium::io::File const &file,
380381
fmt::print(stderr, "Using {} parser.\n",
381382
osmium::io::as_string(file.format()));
382383

383-
parse_osmium_t handler{bbox, m_append, this};
384+
input_handler_t handler{bbox, m_append, this};
384385
osmium::io::Reader reader{file};
385386
osmium::apply(reader, handler);
386387
reader.close();
387388

388-
return handler.stats();
389+
return handler.progress();
389390
}
390391

391392
void osmdata_t::process_stage1b() const

src/osmdata.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#ifndef OSM2PGSQL_OSMDATA_HPP
22
#define OSM2PGSQL_OSMDATA_HPP
33

4+
/**
5+
* \file
6+
*
7+
* This file is part of osm2pgsql (https://github.com/openstreetmap/osm2pgsql).
8+
*
9+
* It contains the osmdata_t class.
10+
*/
11+
412
#include <memory>
513
#include <vector>
614

@@ -9,13 +17,18 @@
917

1018
#include "dependency-manager.hpp"
1119
#include "osmtypes.hpp"
12-
#include "parse-osmium.hpp"
20+
#include "progress-display.hpp"
1321

1422
class options_t;
1523
class output_t;
1624
struct middle_t;
1725
struct slim_middle_t;
1826

27+
/**
28+
* This class guides the processing of the OSM data through its multiple
29+
* stages. It calls upon the major compontents of osm2pgsql, the dependency
30+
* manager, the middle, and the outputs to do their work.
31+
*/
1932
class osmdata_t
2033
{
2134
public:
@@ -31,8 +44,8 @@ class osmdata_t
3144
* Process the specified OSM file (stage 1a). This is called once for
3245
* each input file.
3346
*/
34-
parse_stats_t process_file(osmium::io::File const &file,
35-
osmium::Box const &bbox) const;
47+
progress_display_t process_file(osmium::io::File const &file,
48+
osmium::Box const &bbox) const;
3649

3750
/**
3851
* Rest of the processing (stages 1b, 2, and 3). This is called once

0 commit comments

Comments
 (0)