Skip to content

Commit 6678ebc

Browse files
authored
feat(tools): add --output option to osrm-contract, osrm-customize, osrm-partition (#7646)
1 parent 58a9cbf commit 6678ebc

9 files changed

Lines changed: 122 additions & 16 deletions

File tree

include/contractor/contractor_config.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,18 @@ struct ContractorConfig final : storage::IOConfig
5353

5454
bool IsValid() const { return IOConfig::IsValid() && updater_config.IsValid(); }
5555

56+
std::filesystem::path GetOutputPath(const std::string &ext) const
57+
{
58+
// Validate the extension is configured (throws if not)
59+
GetPath(ext);
60+
if (!output_path.empty())
61+
return {output_path.string() + ext};
62+
return GetPath(ext);
63+
}
64+
5665
updater::UpdaterConfig updater_config;
5766

67+
std::filesystem::path output_path;
5868
unsigned requested_num_threads = 0;
5969
};
6070
} // namespace osrm::contractor

include/customizer/customizer_config.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ struct CustomizationConfig final : storage::IOConfig
3030
updater_config.UseDefaultOutputNames(base);
3131
}
3232

33+
std::filesystem::path GetOutputPath(const std::string &ext) const
34+
{
35+
// Validate the extension is configured (throws if not)
36+
GetPath(ext);
37+
if (!output_path.empty())
38+
return {output_path.string() + ext};
39+
return GetPath(ext);
40+
}
41+
42+
std::filesystem::path output_path;
3343
unsigned requested_num_threads;
3444

3545
updater::UpdaterConfig updater_config;

include/partitioner/partitioner_config.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ struct PartitionerConfig final : storage::IOConfig
3131
IOConfig::UseDefaultOutputNames(base);
3232
}
3333

34+
std::filesystem::path GetOutputPath(const std::string &ext) const
35+
{
36+
// Validate the extension is configured (throws if not)
37+
GetPath(ext);
38+
if (!output_path.empty())
39+
return {output_path.string() + ext};
40+
return GetPath(ext);
41+
}
42+
43+
std::filesystem::path output_path;
3444
unsigned requested_num_threads;
3545

3646
double balance;

src/contractor/contractor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int Contractor::Run()
7272
std::unordered_map<std::string, ContractedMetric> metrics = {
7373
{metric_name, {std::move(query_graph), std::move(edge_filters)}}};
7474

75-
files::writeGraph(config.GetPath(".osrm.hsgr"), metrics, connectivity_checksum);
75+
files::writeGraph(config.GetOutputPath(".osrm.hsgr"), metrics, connectivity_checksum);
7676

7777
TIMER_STOP(preparing);
7878

src/customize/customizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ int Customizer::Run(const CustomizationConfig &config)
166166
std::unordered_map<std::string, std::vector<CellMetric>> metric_exclude_classes = {
167167
{properties.GetWeightName(), std::move(metrics)},
168168
};
169-
files::writeCellMetrics(config.GetPath(".osrm.cell_metrics"), metric_exclude_classes);
169+
files::writeCellMetrics(config.GetOutputPath(".osrm.cell_metrics"), metric_exclude_classes);
170170
TIMER_STOP(writing_mld_data);
171171
util::Log() << "MLD customization writing took " << TIMER_SEC(writing_mld_data) << " seconds";
172172

@@ -176,7 +176,7 @@ int Customizer::Run(const CustomizationConfig &config)
176176
std::move(node_durations),
177177
std::move(node_distances)};
178178
customizer::files::writeGraph(
179-
config.GetPath(".osrm.mldgr"), shaved_graph, connectivity_checksum);
179+
config.GetOutputPath(".osrm.mldgr"), shaved_graph, connectivity_checksum);
180180
TIMER_STOP(writing_graph);
181181
util::Log() << "Graph writing took " << TIMER_SEC(writing_graph) << " seconds";
182182

src/partitioner/partitioner.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,27 @@ int Partitioner::Run(const PartitionerConfig &config)
126126
renumber(partitions, permutation);
127127
{
128128
renumber(mapping, permutation);
129-
extractor::files::writeNBGMapping(config.GetPath(".osrm.cnbg_to_ebg").string(), mapping);
129+
extractor::files::writeNBGMapping(config.GetOutputPath(".osrm.cnbg_to_ebg").string(),
130+
mapping);
130131
}
131132
{
133+
// If writing to a different output path, copy the file first since we mmap in-place
134+
if (!config.output_path.empty())
135+
{
136+
std::filesystem::copy_file(config.GetPath(".osrm.fileIndex"),
137+
config.GetOutputPath(".osrm.fileIndex"),
138+
std::filesystem::copy_options::overwrite_existing);
139+
}
132140
boost::iostreams::mapped_file segment_region;
133141
auto segments = util::mmapFile<extractor::EdgeBasedNodeSegment>(
134-
config.GetPath(".osrm.fileIndex"), segment_region);
142+
config.GetOutputPath(".osrm.fileIndex"), segment_region);
135143
renumber(segments, permutation);
136144
}
137145
{
138146
extractor::EdgeBasedNodeDataContainer node_data;
139147
extractor::files::readNodeData(config.GetPath(".osrm.ebg_nodes"), node_data);
140148
renumber(node_data, permutation);
141-
extractor::files::writeNodeData(config.GetPath(".osrm.ebg_nodes"), node_data);
149+
extractor::files::writeNodeData(config.GetOutputPath(".osrm.ebg_nodes"), node_data);
142150
}
143151
{
144152
std::vector<EdgeWeight> node_weights;
@@ -151,10 +159,11 @@ int Partitioner::Run(const PartitionerConfig &config)
151159
util::inplacePermutation(node_durations.begin(), node_durations.end(), permutation);
152160
util::inplacePermutation(node_distances.begin(), node_distances.end(), permutation);
153161
extractor::files::writeEdgeBasedNodeWeightsDurationsDistances(
154-
config.GetPath(".osrm.enw"), node_weights, node_durations, node_distances);
162+
config.GetOutputPath(".osrm.enw"), node_weights, node_durations, node_distances);
155163
}
156164
{
157165
const auto &filename = config.GetPath(".osrm.maneuver_overrides");
166+
const auto &output_filename = config.GetOutputPath(".osrm.maneuver_overrides");
158167
std::vector<extractor::StorageManeuverOverride> maneuver_overrides;
159168
std::vector<NodeID> node_sequences;
160169
extractor::files::readManeuverOverrides(filename, maneuver_overrides, node_sequences);
@@ -168,13 +177,14 @@ int Partitioner::Run(const PartitionerConfig &config)
168177
maneuver_overrides.end(),
169178
[](const auto &a, const auto &b) { return a.start_node < b.start_node; });
170179

171-
extractor::files::writeManeuverOverrides(filename, maneuver_overrides, node_sequences);
180+
extractor::files::writeManeuverOverrides(
181+
output_filename, maneuver_overrides, node_sequences);
172182
}
173-
if (std::filesystem::exists(config.GetPath(".osrm.hsgr")))
183+
if (std::filesystem::exists(config.GetOutputPath(".osrm.hsgr")))
174184
{
175185
util::Log(logWARNING) << "Found existing .osrm.hsgr file, removing. You need to re-run "
176186
"osrm-contract after osrm-partition.";
177-
std::filesystem::remove(config.GetPath(".osrm.hsgr"));
187+
std::filesystem::remove(config.GetOutputPath(".osrm.hsgr"));
178188
}
179189
TIMER_STOP(renumber);
180190
util::Log() << "Renumbered data in " << TIMER_SEC(renumber) << " seconds";
@@ -190,9 +200,9 @@ int Partitioner::Run(const PartitionerConfig &config)
190200
util::Log() << "CellStorage constructed in " << TIMER_SEC(cell_storage) << " seconds";
191201

192202
TIMER_START(writing_mld_data);
193-
files::writePartition(config.GetPath(".osrm.partition"), mlp);
194-
files::writeCells(config.GetPath(".osrm.cells"), storage);
195-
extractor::files::writeEdgeBasedGraph(config.GetPath(".osrm.ebg"),
203+
files::writePartition(config.GetOutputPath(".osrm.partition"), mlp);
204+
files::writeCells(config.GetOutputPath(".osrm.cells"), storage);
205+
extractor::files::writeEdgeBasedGraph(config.GetOutputPath(".osrm.ebg"),
196206
edge_based_graph.GetNumberOfNodes(),
197207
graphToEdges(edge_based_graph),
198208
edge_based_graph.connectivity_checksum);

src/tools/contract.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <boost/program_options.hpp>
1111
#include <boost/program_options/errors.hpp>
1212

13+
#include <array>
1314
#include <cstdlib>
1415
#include <filesystem>
1516
#include <new>
@@ -72,7 +73,10 @@ return_code parseArguments(int argc,
7273
"time-zone-file",
7374
boost::program_options::value<std::string>(&contractor_config.updater_config.tz_file_path),
7475
"Required for conditional turn restriction parsing, provide a geojson file containing "
75-
"time zone boundaries");
76+
"time zone boundaries")(
77+
"output,o",
78+
boost::program_options::value<std::filesystem::path>(&contractor_config.output_path),
79+
"Output base path for generated files (default: same as input)");
7680

7781
// hidden options, will be allowed on command line, but will not be shown to the user
7882
boost::program_options::options_description hidden_options("Hidden options");
@@ -166,6 +170,24 @@ try
166170

167171
contractor_config.UseDefaultOutputNames(contractor_config.base_path);
168172

173+
if (!contractor_config.output_path.empty())
174+
{
175+
// Strip known extensions from the user-provided output path
176+
std::string path = contractor_config.output_path.string();
177+
const std::array<std::string, 6> known_extensions{
178+
{".osm.bz2", ".osm.pbf", ".osm.xml", ".pbf", ".osm", ".osrm"}};
179+
for (const auto &ext : known_extensions)
180+
{
181+
const auto pos = path.find(ext);
182+
if (pos != std::string::npos)
183+
{
184+
path.replace(pos, ext.size(), "");
185+
break;
186+
}
187+
}
188+
contractor_config.output_path = path;
189+
}
190+
169191
if (1 > contractor_config.requested_num_threads)
170192
{
171193
util::Log(logERROR) << "Number of threads must be 1 or larger";

src/tools/customize.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "util/program_options_path.hpp"
99
#include <boost/program_options.hpp>
1010

11+
#include <array>
1112
#include <filesystem>
1213
#include <iostream>
1314
#include <set>
@@ -72,7 +73,10 @@ return_code parseArguments(int argc,
7273
&customization_config.updater_config.tz_file_path)
7374
->default_value(""),
7475
"Required for conditional turn restriction parsing, provide a geojson file containing "
75-
"time zone boundaries");
76+
"time zone boundaries")(
77+
"output,o",
78+
boost::program_options::value<std::filesystem::path>(&customization_config.output_path),
79+
"Output base path for generated files (default: same as input)");
7680

7781
// hidden options, will be allowed on command line, but will not be
7882
// shown to the user
@@ -167,6 +171,24 @@ try
167171
// set the default in/output names
168172
customization_config.UseDefaultOutputNames(customization_config.base_path);
169173

174+
if (!customization_config.output_path.empty())
175+
{
176+
// Strip known extensions from the user-provided output path
177+
std::string path = customization_config.output_path.string();
178+
const std::array<std::string, 6> known_extensions{
179+
{".osm.bz2", ".osm.pbf", ".osm.xml", ".pbf", ".osm", ".osrm"}};
180+
for (const auto &ext : known_extensions)
181+
{
182+
const auto pos = path.find(ext);
183+
if (pos != std::string::npos)
184+
{
185+
path.replace(pos, ext.size(), "");
186+
break;
187+
}
188+
}
189+
customization_config.output_path = path;
190+
}
191+
170192
if (1 > customization_config.requested_num_threads)
171193
{
172194
util::Log(logERROR) << "Number of threads must be 1 or larger";

src/tools/partition.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <boost/algorithm/string/join.hpp>
1212
#include <boost/program_options.hpp>
1313

14+
#include <array>
1415
#include <filesystem>
1516
#include <iostream>
1617
#include <iterator>
@@ -115,7 +116,10 @@ return_code parseArguments(int argc,
115116
boost::program_options::value<MaxCellSizesArgument>()->default_value(
116117
MaxCellSizesArgument{config.max_cell_sizes}),
117118
"Maximum cell sizes starting from the level 1. The first cell size value is a bisection "
118-
"termination citerion");
119+
"termination criterion")(
120+
"output,o",
121+
boost::program_options::value<std::filesystem::path>(&config.output_path),
122+
"Output base path for generated files (default: same as input)");
119123

120124
// hidden options, will be allowed on command line, but will not be
121125
// shown to the user
@@ -220,6 +224,24 @@ try
220224
// set the default in/output names
221225
partition_config.UseDefaultOutputNames(partition_config.base_path);
222226

227+
if (!partition_config.output_path.empty())
228+
{
229+
// Strip known extensions from the user-provided output path
230+
std::string path = partition_config.output_path.string();
231+
const std::array<std::string, 6> known_extensions{
232+
{".osm.bz2", ".osm.pbf", ".osm.xml", ".pbf", ".osm", ".osrm"}};
233+
for (const auto &ext : known_extensions)
234+
{
235+
const auto pos = path.find(ext);
236+
if (pos != std::string::npos)
237+
{
238+
path.replace(pos, ext.size(), "");
239+
break;
240+
}
241+
}
242+
partition_config.output_path = path;
243+
}
244+
223245
if (1 > partition_config.requested_num_threads)
224246
{
225247
util::Log(logERROR) << "Number of threads must be 1 or larger";

0 commit comments

Comments
 (0)