Skip to content

Commit 8702ad6

Browse files
authored
Merge pull request #1205 from joto/move-file-read-into-osmdata
Move file reading functionality from parse_osmium_t to osmdata_t
2 parents 925cdc0 + 19112f0 commit 8702ad6

File tree

6 files changed

+65
-66
lines changed

6 files changed

+65
-66
lines changed

src/osm2pgsql.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "options.hpp"
3030
#include "osmdata.hpp"
3131
#include "output.hpp"
32-
#include "parse-osmium.hpp"
3332
#include "reprojection.hpp"
3433
#include "util.hpp"
3534
#include "version.hpp"
@@ -90,10 +89,17 @@ int main(int argc, char *argv[])
9089
fmt::print(stderr, "\nReading in file: {}\n", filename);
9190
util::timer_t timer_parse;
9291

93-
parse_osmium_t parser{options.bbox, options.append, &osmdata};
94-
parser.stream_file(filename, options.input_format);
95-
96-
stats.update(parser.stats());
92+
osmium::io::File file{filename, options.input_format};
93+
if (file.format() == osmium::io::file_format::unknown) {
94+
if (options.input_format.empty()) {
95+
throw std::runtime_error{
96+
"Cannot detect file format. Try using -r."};
97+
}
98+
throw std::runtime_error{
99+
"Unknown file format '{}'."_format(options.input_format)};
100+
}
101+
102+
stats.update(osmdata.process_file(file, options.bbox));
97103

98104
stats.print_status(std::time(nullptr));
99105
fmt::print(stderr, " parse time: {}s\n", timer_parse.stop());

src/osmdata.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include <utility>
99
#include <vector>
1010

11+
#include <osmium/io/any_input.hpp>
1112
#include <osmium/thread/pool.hpp>
13+
#include <osmium/visitor.hpp>
1214

1315
#include "db-copy.hpp"
1416
#include "format.hpp"
@@ -367,6 +369,25 @@ class multithreaded_processor
367369

368370
} // anonymous namespace
369371

372+
parse_stats_t osmdata_t::process_file(osmium::io::File const &file,
373+
osmium::Box const &bbox) const
374+
{
375+
if (!m_append && file.has_multiple_object_versions()) {
376+
throw std::runtime_error{
377+
"Reading an OSM change file only works in append mode."};
378+
}
379+
380+
fmt::print(stderr, "Using {} parser.\n",
381+
osmium::io::as_string(file.format()));
382+
383+
parse_osmium_t handler{bbox, m_append, this};
384+
osmium::io::Reader reader{file};
385+
osmium::apply(reader, handler);
386+
reader.close();
387+
388+
return handler.stats();
389+
}
390+
370391
void osmdata_t::process_stage1b() const
371392
{
372393
if (m_dependency_manager->has_pending()) {

src/osmdata.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include <memory>
55
#include <vector>
66

7+
#include <osmium/fwd.hpp>
8+
#include <osmium/io/file.hpp>
9+
710
#include "dependency-manager.hpp"
811
#include "osmtypes.hpp"
12+
#include "parse-osmium.hpp"
913

1014
class options_t;
1115
class output_t;
@@ -22,6 +26,18 @@ class osmdata_t
2226

2327
void start() const;
2428
void flush() const;
29+
30+
/**
31+
* Process the specified OSM file (stage 1a). This is called once for
32+
* each input file.
33+
*/
34+
parse_stats_t process_file(osmium::io::File const &file,
35+
osmium::Box const &bbox) const;
36+
37+
/**
38+
* Rest of the processing (stages 1b, 2, and 3). This is called once
39+
* after process_file() was called for each input file.
40+
*/
2541
void stop() const;
2642

2743
void node_add(osmium::Node const &node) const;
@@ -37,6 +53,7 @@ class osmdata_t
3753
void relation_delete(osmid_t id) const;
3854

3955
private:
56+
4057
/**
4158
* Run stage 1b processing: Process dependent objects.
4259
* In append mode we need to process dependent objects that were marked

src/parse-osmium.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@
2323
#include "parse-osmium.hpp"
2424
#include "format.hpp"
2525
#include "osmdata.hpp"
26-
#include "reprojection.hpp"
2726

28-
#include <osmium/handler.hpp>
29-
#include <osmium/io/any_input.hpp>
3027
#include <osmium/osm.hpp>
31-
#include <osmium/visitor.hpp>
3228

3329
void parse_stats_t::update(parse_stats_t const &other)
3430
{
@@ -73,36 +69,10 @@ void parse_stats_t::possibly_print_status()
7369
}
7470

7571
parse_osmium_t::parse_osmium_t(osmium::Box const &bbox, bool append,
76-
osmdata_t *osmdata)
72+
osmdata_t const *osmdata)
7773
: m_data(osmdata), m_bbox(bbox), m_append(append)
7874
{}
7975

80-
void parse_osmium_t::stream_file(std::string const &filename,
81-
std::string const &format)
82-
{
83-
osmium::io::File infile{filename, format};
84-
85-
if (!m_append && infile.has_multiple_object_versions()) {
86-
throw std::runtime_error{
87-
"Reading an OSM change file only works in append mode."};
88-
}
89-
90-
if (infile.format() == osmium::io::file_format::unknown) {
91-
throw std::runtime_error{
92-
format.empty()
93-
? std::string{"Cannot detect file format. Try using -r."}
94-
: "Unknown file format '{}'."_format(format)};
95-
}
96-
97-
fmt::print(stderr, "Using {} parser.\n",
98-
osmium::io::as_string(infile.format()));
99-
100-
m_type = osmium::item_type::node;
101-
osmium::io::Reader reader{infile};
102-
osmium::apply(reader, *this);
103-
reader.close();
104-
}
105-
10676
void parse_osmium_t::node(osmium::Node const &node)
10777
{
10878
if (m_type != osmium::item_type::node) {

src/parse-osmium.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ class parse_stats_t
130130
class parse_osmium_t : public osmium::handler::Handler
131131
{
132132
public:
133-
parse_osmium_t(osmium::Box const &bbox, bool append, osmdata_t *osmdata);
134-
135-
void stream_file(std::string const &filename, std::string const &fmt);
133+
parse_osmium_t(osmium::Box const &bbox, bool append,
134+
osmdata_t const *osmdata);
136135

137136
void node(osmium::Node const &node);
138137
void way(osmium::Way &way);
@@ -141,15 +140,15 @@ class parse_osmium_t : public osmium::handler::Handler
141140
parse_stats_t const &stats() const noexcept { return m_stats; }
142141

143142
private:
144-
osmdata_t *m_data;
143+
osmdata_t const *m_data;
145144

146145
// Bounding box for node import or invalid Box if everything is imported
147146
osmium::Box m_bbox;
148147

149148
parse_stats_t m_stats;
150149

151150
// Current type being parsed.
152-
osmium::item_type m_type = osmium::item_type::undefined;
151+
osmium::item_type m_type = osmium::item_type::node;
153152

154153
// Are we running in append mode?
155154
bool m_append;

tests/common-import.hpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "osmdata.hpp"
1515
#include "output-multi.hpp"
1616
#include "output.hpp"
17-
#include "parse-osmium.hpp"
1817
#include "taginfo-impl.hpp"
1918

2019
#include "common-pg.hpp"
@@ -30,31 +29,19 @@ inline void parse_file(options_t const &options,
3029
osmdata_t osmdata{std::move(dependency_manager), mid, outs, options};
3130

3231
osmdata.start();
33-
parse_osmium_t parser{options.bbox, options.append, &osmdata};
3432

35-
std::string filep{TESTDATA_DIR};
36-
filep += filename ? filename : options.input_files[0];
37-
38-
parser.stream_file(filep, "");
33+
std::string filepath{TESTDATA_DIR};
34+
if (filename) {
35+
filepath += filename;
36+
} else {
37+
filepath += options.input_files[0];
38+
}
39+
osmium::io::File file{filepath};
40+
osmdata.process_file(file, options.bbox);
3941

4042
osmdata.stop();
4143
}
4244

43-
class test_parse_t : public parse_osmium_t
44-
{
45-
public:
46-
using parse_osmium_t::parse_osmium_t;
47-
48-
void stream_buffer(char const *buf, std::string const &fmt)
49-
{
50-
osmium::io::File infile{buf, std::strlen(buf), fmt};
51-
52-
osmium::io::Reader reader{infile};
53-
osmium::apply(reader, *this);
54-
reader.close();
55-
}
56-
};
57-
5845
namespace db {
5946

6047
/**
@@ -65,7 +52,7 @@ class import_t
6552
{
6653
public:
6754
void run_import(options_t options, char const *data,
68-
std::string const &fmt = "opl")
55+
std::string const &format = "opl")
6956
{
7057
options.database_options = m_db.db_options();
7158

@@ -96,9 +83,8 @@ class import_t
9683

9784
osmdata.start();
9885

99-
test_parse_t parser(options.bbox, options.append, &osmdata);
100-
101-
parser.stream_buffer(data, fmt);
86+
osmium::io::File file{data, std::strlen(data), format};
87+
osmdata.process_file(file, options.bbox);
10288

10389
osmdata.stop();
10490
}

0 commit comments

Comments
 (0)