Skip to content

Commit c75d01a

Browse files
committed
Open writer in getid command earlier
This way we'll get an error message earlier if there is a problem.
1 parent 5fdc7e5 commit c75d01a

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/command_getid.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
4242
#include <cstddef>
4343
#include <fstream>
4444
#include <iostream>
45+
#include <memory>
4546
#include <string>
4647
#include <vector>
4748

@@ -245,6 +246,7 @@ bool CommandGetId::find_relations_in_relations() {
245246
osmium::index::RelationsMapStash stash;
246247

247248
osmium::io::Reader reader{m_input_file, osmium::osm_entity_bits::relation, osmium::io::read_meta::no};
249+
open_writer(reader);
248250
while (osmium::memory::Buffer buffer = reader.read()) {
249251
for (const auto& relation : buffer.select<osmium::Relation>()) {
250252
for (const auto& member : relation.members()) {
@@ -278,6 +280,7 @@ void CommandGetId::find_nodes_and_ways_in_relations() {
278280
m_vout << " Reading input file to find nodes/ways in relations...\n";
279281

280282
osmium::io::Reader reader{m_input_file, osmium::osm_entity_bits::relation, osmium::io::read_meta::no};
283+
open_writer(reader);
281284
while (osmium::memory::Buffer buffer = reader.read()) {
282285
for (const auto& relation : buffer.select<osmium::Relation>()) {
283286
if (m_ids(osmium::item_type::relation).get(relation.positive_id())) {
@@ -298,6 +301,7 @@ void CommandGetId::find_nodes_in_ways() {
298301
m_vout << " Reading input file to find nodes in ways...\n";
299302

300303
osmium::io::Reader reader{m_input_file, osmium::osm_entity_bits::way, osmium::io::read_meta::no};
304+
open_writer(reader);
301305
while (osmium::memory::Buffer buffer = reader.read()) {
302306
for (const auto& way : buffer.select<osmium::Way>()) {
303307
if (m_ids(osmium::item_type::way).get(way.positive_id())) {
@@ -332,19 +336,25 @@ void CommandGetId::find_referenced_objects() {
332336
m_vout << "Done following references.\n";
333337
}
334338

339+
void CommandGetId::open_writer(osmium::io::Reader& reader) {
340+
if (m_writer) {
341+
return;
342+
}
343+
344+
m_vout << "Opening output file...\n";
345+
osmium::io::Header header{reader.header()};
346+
setup_header(header);
347+
m_writer = std::make_unique<osmium::io::Writer>(m_output_file, header, m_output_overwrite, m_fsync);
348+
}
349+
335350
bool CommandGetId::run() {
336351
if (m_add_referenced_objects) {
337352
find_referenced_objects();
338353
}
339354

340355
m_vout << "Opening input file...\n";
341356
osmium::io::Reader reader{m_input_file, get_needed_types()};
342-
343-
m_vout << "Opening output file...\n";
344-
osmium::io::Header header{reader.header()};
345-
setup_header(header);
346-
347-
osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
357+
open_writer(reader);
348358

349359
m_vout << "Copying matching objects to output file...\n";
350360
osmium::ProgressBar progress_bar{reader.file_size(), display_progress()};
@@ -355,22 +365,22 @@ bool CommandGetId::run() {
355365
if (!m_work_with_history) {
356366
m_ids(object.type()).unset(object.positive_id());
357367
}
358-
writer(object);
368+
writer()(object);
359369
} else if (m_ids(object.type()).get(object.positive_id())) {
360370
if (!m_work_with_history) {
361371
m_ids(object.type()).unset(object.positive_id());
362372
}
363373
if (m_remove_tags) {
364374
object.remove_tags();
365375
}
366-
writer(object);
376+
writer()(object);
367377
}
368378
}
369379
}
370380
progress_bar.done();
371381

372382
m_vout << "Closing output file...\n";
373-
writer.close();
383+
writer().close();
374384

375385
m_vout << "Closing input file...\n";
376386
reader.close();

src/command_getid.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,22 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2929
#include <osmium/index/id_set.hpp>
3030
#include <osmium/index/nwr_array.hpp>
3131
#include <osmium/index/relations_map.hpp>
32+
#include <osmium/io/reader.hpp>
33+
#include <osmium/io/writer.hpp>
3234
#include <osmium/osm/entity_bits.hpp>
3335
#include <osmium/osm/item_type.hpp>
3436
#include <osmium/osm/types.hpp>
3537

3638
#include <cstddef>
3739
#include <iosfwd>
40+
#include <memory>
3841
#include <string>
3942
#include <vector>
4043

4144
class CommandGetId : public CommandWithSingleOSMInput, public with_osm_output {
4245

46+
std::unique_ptr<osmium::io::Writer> m_writer;
47+
4348
osmium::nwr_array<osmium::index::IdSetDense<osmium::unsigned_object_id_type>> m_matching_ids;
4449
osmium::nwr_array<osmium::index::IdSetDense<osmium::unsigned_object_id_type>> m_ids;
4550

@@ -53,6 +58,12 @@ class CommandGetId : public CommandWithSingleOSMInput, public with_osm_output {
5358
osmium::osm_entity_bits::type get_needed_types() const;
5459
std::size_t count_ids() const noexcept;
5560

61+
void open_writer(osmium::io::Reader& reader);
62+
63+
osmium::io::Writer& writer() {
64+
return *m_writer;
65+
}
66+
5667
void find_referenced_objects();
5768

5869
void mark_rel_ids(const osmium::index::RelationsMapIndex& rel_in_rel, osmium::unsigned_object_id_type parent_id);

0 commit comments

Comments
 (0)