Skip to content

Commit

Permalink
Open output files earlier in several commands
Browse files Browse the repository at this point in the history
So we get error message earlier if there are any problems.

Needs libosmium version 2.20.0.
  • Loading branch information
joto committed Jan 12, 2025
1 parent 5543e7c commit 209db39
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 57 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Changed

- Needs at least libosmium version 2.20.0.
- Switch from RapidJSON to NLohman JSON. RapidJSON hasn't seen an update in
a long time, so we are using a different JSON library.
- Removed "spaten" output format.
- Open writer in getid command earlier, so see potential errors earlier.
- Open writer in some commands earlier, so we see potential errors earlier.
- Lots of small code cleanups.

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ option(RUN_TESTS_WITH_BINARY_COMPARE "Run tests that do binary comparisons" ON)
find_package(Boost 1.55.0 REQUIRED COMPONENTS program_options)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})

find_package(Osmium 2.17.0 REQUIRED COMPONENTS io)
find_package(Osmium 2.20.0 REQUIRED COMPONENTS io)
include_directories(SYSTEM ${OSMIUM_INCLUDE_DIRS})

find_package(nlohmann_json)
Expand Down
9 changes: 5 additions & 4 deletions src/command_add_locations_to_ways.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ void CommandAddLocationsToWays::find_member_nodes() {
}

bool CommandAddLocationsToWays::run() {
m_output_file.set("locations_on_ways");
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

if (m_keep_member_nodes) {
m_vout << "Getting all nodes referenced from relations...\n";
find_member_nodes();
Expand All @@ -191,14 +194,12 @@ bool CommandAddLocationsToWays::run() {
location_handler.ignore_errors();
}

m_output_file.set("locations_on_ways");

if (m_input_files.size() == 1) { // single input file
m_vout << "Copying input file '" << m_input_files[0].filename() << "'...\n";
osmium::io::Reader reader{m_input_files[0]};
osmium::io::Header header{reader.header()};
setup_header(header);
osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

osmium::ProgressBar progress_bar{reader.file_size(), display_progress()};
copy_data(progress_bar, reader, writer, location_handler);
Expand All @@ -209,7 +210,7 @@ bool CommandAddLocationsToWays::run() {
} else { // multiple input files
osmium::io::Header header;
setup_header(header);
osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

osmium::ProgressBar progress_bar{file_size_sum(m_input_files), display_progress()};
for (const auto& input_file : m_input_files) {
Expand Down
15 changes: 8 additions & 7 deletions src/command_apply_changes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ void CommandApplyChanges::apply_changes_and_write(osmium::ObjectPointerCollectio
}

bool CommandApplyChanges::run() {
if (m_locations_on_ways) {
m_output_file.set("locations_on_ways");
}

m_vout << "Opening output file...\n";
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

std::vector<osmium::memory::Buffer> changes;
osmium::ObjectPointerCollection objects;

Expand Down Expand Up @@ -302,13 +309,7 @@ bool CommandApplyChanges::run() {
if (m_with_history) {
header.set_has_multiple_object_versions(true);
}

if (m_locations_on_ways) {
m_output_file.set("locations_on_ways");
}

m_vout << "Opening output file...\n";
osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

if (m_with_history) {
// For history files this is a straightforward sort of the change
Expand Down
15 changes: 7 additions & 8 deletions src/command_derive_changes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ void CommandDeriveChanges::write_deleted(osmium::io::Writer& writer, osmium::OSM
}

bool CommandDeriveChanges::run() {
m_vout << "Opening output file...\n";
if (m_output_file.format() != osmium::io::file_format::xml || !m_output_file.is_true("xml_change_format")) {
warning("Output format chosen is not the XML change format. Use .osc(.gz|bz2) as suffix or -f option.\n");
}
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

m_vout << "Opening input files...\n";
osmium::io::Reader reader1{m_input_files[0], osmium::osm_entity_bits::object};
osmium::io::ReaderWithProgressBar reader2{display_progress(), m_input_files[1], osmium::osm_entity_bits::object};
Expand All @@ -161,16 +167,9 @@ bool CommandDeriveChanges::run() {
auto end1 = in1.end();
auto end2 = in2.end();

reader2.progress_bar().remove();
m_vout << "Opening output file...\n";
if (m_output_file.format() != osmium::io::file_format::xml || !m_output_file.is_true("xml_change_format")) {
warning("Output format chosen is not the XML change format. Use .osc(.gz|bz2) as suffix or -f option.\n");
}

osmium::io::Header header;
setup_header(header);

osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

reader2.progress_bar().remove();
m_vout << "Deriving changes...\n";
Expand Down
7 changes: 4 additions & 3 deletions src/command_getid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,19 @@ void CommandGetId::find_referenced_objects() {
}

bool CommandGetId::run() {
m_vout << "Opening output file...\n";
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

if (m_add_referenced_objects) {
find_referenced_objects();
}

m_vout << "Opening input file...\n";
osmium::io::Reader reader{m_input_file, get_needed_types()};

m_vout << "Opening output file...\n";
osmium::io::Header header{reader.header()};
setup_header(header);

osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

m_vout << "Copying matching objects to output file...\n";
osmium::ProgressBar progress_bar{reader.file_size(), display_progress()};
Expand Down
10 changes: 6 additions & 4 deletions src/command_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void CommandSort::show_arguments() {
}

bool CommandSort::run_single_pass() {
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

std::vector<osmium::memory::Buffer> data;
osmium::ObjectPointerCollection objects;

Expand Down Expand Up @@ -149,8 +151,7 @@ bool CommandSort::run_single_pass() {
if (bounding_box) {
header.add_box(bounding_box);
}

osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

m_vout << "Sorting data...\n";
objects.sort(osmium::object_order_type_id_version());
Expand All @@ -169,6 +170,8 @@ bool CommandSort::run_single_pass() {
}

bool CommandSort::run_multi_pass() {
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

osmium::Box bounding_box;

m_vout << "Reading input file headers...\n";
Expand All @@ -185,8 +188,7 @@ bool CommandSort::run_multi_pass() {
if (bounding_box) {
header.add_box(bounding_box);
}

osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
writer.set_header(header);

osmium::ProgressBar progress_bar{file_size_sum(m_input_files) * 3, display_progress()};

Expand Down
43 changes: 14 additions & 29 deletions src/command_tags_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,44 +363,29 @@ void CommandTagsFilter::copy_matching_objects(osmium::io::Reader& reader, osmium
}
}
progress_bar.done();

m_vout << "Closing output file...\n";
writer.close();

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

bool CommandTagsFilter::run() {
if (m_add_referenced_objects) {
m_vout << "Opening input file to get header...\n";
osmium::io::Reader reader_only_for_header{m_input_file, osmium::osm_entity_bits::nothing};

m_vout << "Opening output file...\n";
osmium::io::Header header{reader_only_for_header.header()};
setup_header(header);
reader_only_for_header.close();

osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
m_vout << "Opening output file...\n";
osmium::io::Writer writer{m_output_file, m_output_overwrite, m_fsync};

if (m_add_referenced_objects) {
find_referenced_objects();
}

m_vout << "Opening input file...\n";
osmium::io::Reader reader{m_input_file, get_needed_types()};

copy_matching_objects(reader, writer);
} else {
m_vout << "Opening input file...\n";
osmium::io::Reader reader{m_input_file, get_needed_types()};
m_vout << "Opening input file...\n";
osmium::io::Reader reader{m_input_file, get_needed_types()};
osmium::io::Header header{reader.header()};
setup_header(header);
writer.set_header(header);

m_vout << "Opening output file...\n";
osmium::io::Header header{reader.header()};
setup_header(header);
copy_matching_objects(reader, writer);

osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
m_vout << "Closing output file...\n";
writer.close();

copy_matching_objects(reader, writer);
}
m_vout << "Closing input file...\n";
reader.close();

show_memory_used();

Expand Down

0 comments on commit 209db39

Please sign in to comment.