Create public facing libraries for extractor, contractor and datastore

New libraries libosrm_extract, libosrm_contract, libosrm_store
This commit is contained in:
Patrick Niklaus
2016-01-07 19:19:55 +01:00
parent b36145e3c4
commit 439eb9da3d
68 changed files with 3266 additions and 3104 deletions
+118 -120
View File
@@ -98,141 +98,139 @@ std::size_t loadGraph(const char *path,
}
}
int main(int argc, char *argv[])
int main(int argc, char *argv[]) try
{
std::vector<osrm::extractor::QueryNode> coordinate_list;
osrm::util::LogPolicy::GetInstance().Unmute();
try
// enable logging
if (argc < 2)
{
// enable logging
if (argc < 2)
osrm::util::SimpleLogger().Write(logWARNING) << "usage:\n" << argv[0] << " <osrm>";
return EXIT_FAILURE;
}
std::vector<osrm::tools::TarjanEdge> graph_edge_list;
auto number_of_nodes = osrm::tools::loadGraph(argv[1], coordinate_list, graph_edge_list);
tbb::parallel_sort(graph_edge_list.begin(), graph_edge_list.end());
const auto graph =
std::make_shared<osrm::tools::TarjanGraph>(number_of_nodes, graph_edge_list);
graph_edge_list.clear();
graph_edge_list.shrink_to_fit();
osrm::util::SimpleLogger().Write() << "Starting SCC graph traversal";
auto tarjan =
osrm::util::make_unique<osrm::extractor::TarjanSCC<osrm::tools::TarjanGraph>>(graph);
tarjan->run();
osrm::util::SimpleLogger().Write() << "identified: " << tarjan->get_number_of_components()
<< " many components";
osrm::util::SimpleLogger().Write() << "identified " << tarjan->get_size_one_count()
<< " size 1 SCCs";
// output
TIMER_START(SCC_RUN_SETUP);
// remove files from previous run if exist
osrm::tools::deleteFileIfExists("component.dbf");
osrm::tools::deleteFileIfExists("component.shx");
osrm::tools::deleteFileIfExists("component.shp");
osrm::util::Percent percentage(graph->GetNumberOfNodes());
OGRRegisterAll();
const char *psz_driver_name = "ESRI Shapefile";
auto *po_driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(psz_driver_name);
if (nullptr == po_driver)
{
throw osrm::util::exception("ESRI Shapefile driver not available");
}
auto *po_datasource = po_driver->CreateDataSource("component.shp", nullptr);
if (nullptr == po_datasource)
{
throw osrm::util::exception("Creation of output file failed");
}
auto *po_srs = new OGRSpatialReference();
po_srs->importFromEPSG(4326);
auto *po_layer = po_datasource->CreateLayer("component", po_srs, wkbLineString, nullptr);
if (nullptr == po_layer)
{
throw osrm::util::exception("Layer creation failed.");
}
TIMER_STOP(SCC_RUN_SETUP);
osrm::util::SimpleLogger().Write() << "shapefile setup took "
<< TIMER_MSEC(SCC_RUN_SETUP) / 1000. << "s";
uint64_t total_network_length = 0;
percentage.reinit(graph->GetNumberOfNodes());
TIMER_START(SCC_OUTPUT);
for (const NodeID source : osrm::util::irange(0u, graph->GetNumberOfNodes()))
{
percentage.printIncrement();
for (const auto current_edge : graph->GetAdjacentEdgeRange(source))
{
osrm::util::SimpleLogger().Write(logWARNING) << "usage:\n" << argv[0] << " <osrm>";
return -1;
}
const auto target = graph->GetTarget(current_edge);
std::vector<osrm::tools::TarjanEdge> graph_edge_list;
auto number_of_nodes = osrm::tools::loadGraph(argv[1], coordinate_list, graph_edge_list);
tbb::parallel_sort(graph_edge_list.begin(), graph_edge_list.end());
const auto graph =
std::make_shared<osrm::tools::TarjanGraph>(number_of_nodes, graph_edge_list);
graph_edge_list.clear();
graph_edge_list.shrink_to_fit();
osrm::util::SimpleLogger().Write() << "Starting SCC graph traversal";
auto tarjan =
osrm::util::make_unique<osrm::extractor::TarjanSCC<osrm::tools::TarjanGraph>>(graph);
tarjan->run();
osrm::util::SimpleLogger().Write() << "identified: " << tarjan->get_number_of_components()
<< " many components";
osrm::util::SimpleLogger().Write() << "identified " << tarjan->get_size_one_count()
<< " size 1 SCCs";
// output
TIMER_START(SCC_RUN_SETUP);
// remove files from previous run if exist
osrm::tools::deleteFileIfExists("component.dbf");
osrm::tools::deleteFileIfExists("component.shx");
osrm::tools::deleteFileIfExists("component.shp");
osrm::util::Percent percentage(graph->GetNumberOfNodes());
OGRRegisterAll();
const char *psz_driver_name = "ESRI Shapefile";
auto *po_driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(psz_driver_name);
if (nullptr == po_driver)
{
throw osrm::util::exception("ESRI Shapefile driver not available");
}
auto *po_datasource = po_driver->CreateDataSource("component.shp", nullptr);
if (nullptr == po_datasource)
{
throw osrm::util::exception("Creation of output file failed");
}
auto *po_srs = new OGRSpatialReference();
po_srs->importFromEPSG(4326);
auto *po_layer = po_datasource->CreateLayer("component", po_srs, wkbLineString, nullptr);
if (nullptr == po_layer)
{
throw osrm::util::exception("Layer creation failed.");
}
TIMER_STOP(SCC_RUN_SETUP);
osrm::util::SimpleLogger().Write() << "shapefile setup took "
<< TIMER_MSEC(SCC_RUN_SETUP) / 1000. << "s";
uint64_t total_network_length = 0;
percentage.reinit(graph->GetNumberOfNodes());
TIMER_START(SCC_OUTPUT);
for (const NodeID source : osrm::util::irange(0u, graph->GetNumberOfNodes()))
{
percentage.printIncrement();
for (const auto current_edge : graph->GetAdjacentEdgeRange(source))
if (source < target || SPECIAL_EDGEID == graph->FindEdge(target, source))
{
const auto target = graph->GetTarget(current_edge);
total_network_length +=
100 * osrm::util::coordinate_calculation::greatCircleDistance(
coordinate_list[source].lat, coordinate_list[source].lon,
coordinate_list[target].lat, coordinate_list[target].lon);
if (source < target || SPECIAL_EDGEID == graph->FindEdge(target, source))
BOOST_ASSERT(current_edge != SPECIAL_EDGEID);
BOOST_ASSERT(source != SPECIAL_NODEID);
BOOST_ASSERT(target != SPECIAL_NODEID);
const unsigned size_of_containing_component =
std::min(tarjan->get_component_size(tarjan->get_component_id(source)),
tarjan->get_component_size(tarjan->get_component_id(target)));
// edges that end on bollard nodes may actually be in two distinct components
if (size_of_containing_component < 1000)
{
total_network_length +=
100 * osrm::util::coordinate_calculation::greatCircleDistance(
coordinate_list[source].lat, coordinate_list[source].lon,
coordinate_list[target].lat, coordinate_list[target].lon);
OGRLineString line_string;
line_string.addPoint(
coordinate_list[source].lon / osrm::COORDINATE_PRECISION,
coordinate_list[source].lat / osrm::COORDINATE_PRECISION);
line_string.addPoint(
coordinate_list[target].lon / osrm::COORDINATE_PRECISION,
coordinate_list[target].lat / osrm::COORDINATE_PRECISION);
BOOST_ASSERT(current_edge != SPECIAL_EDGEID);
BOOST_ASSERT(source != SPECIAL_NODEID);
BOOST_ASSERT(target != SPECIAL_NODEID);
OGRFeature *po_feature =
OGRFeature::CreateFeature(po_layer->GetLayerDefn());
const unsigned size_of_containing_component =
std::min(tarjan->get_component_size(tarjan->get_component_id(source)),
tarjan->get_component_size(tarjan->get_component_id(target)));
// edges that end on bollard nodes may actually be in two distinct components
if (size_of_containing_component < 1000)
po_feature->SetGeometry(&line_string);
if (OGRERR_NONE != po_layer->CreateFeature(po_feature))
{
OGRLineString line_string;
line_string.addPoint(
coordinate_list[source].lon / osrm::COORDINATE_PRECISION,
coordinate_list[source].lat / osrm::COORDINATE_PRECISION);
line_string.addPoint(
coordinate_list[target].lon / osrm::COORDINATE_PRECISION,
coordinate_list[target].lat / osrm::COORDINATE_PRECISION);
OGRFeature *po_feature =
OGRFeature::CreateFeature(po_layer->GetLayerDefn());
po_feature->SetGeometry(&line_string);
if (OGRERR_NONE != po_layer->CreateFeature(po_feature))
{
throw osrm::util::exception("Failed to create feature in shapefile.");
}
OGRFeature::DestroyFeature(po_feature);
throw osrm::util::exception("Failed to create feature in shapefile.");
}
OGRFeature::DestroyFeature(po_feature);
}
}
}
OGRSpatialReference::DestroySpatialReference(po_srs);
OGRDataSource::DestroyDataSource(po_datasource);
TIMER_STOP(SCC_OUTPUT);
osrm::util::SimpleLogger().Write()
<< "generating output took: " << TIMER_MSEC(SCC_OUTPUT) / 1000. << "s";
osrm::util::SimpleLogger().Write()
<< "total network distance: "
<< static_cast<uint64_t>(total_network_length / 100 / 1000.) << " km";
osrm::util::SimpleLogger().Write() << "finished component analysis";
}
catch (const std::exception &e)
{
osrm::util::SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
}
return 0;
OGRSpatialReference::DestroySpatialReference(po_srs);
OGRDataSource::DestroyDataSource(po_datasource);
TIMER_STOP(SCC_OUTPUT);
osrm::util::SimpleLogger().Write()
<< "generating output took: " << TIMER_MSEC(SCC_OUTPUT) / 1000. << "s";
osrm::util::SimpleLogger().Write()
<< "total network distance: "
<< static_cast<uint64_t>(total_network_length / 100 / 1000.) << " km";
osrm::util::SimpleLogger().Write() << "finished component analysis";
return EXIT_SUCCESS;
}
catch (const std::exception &e)
{
osrm::util::SimpleLogger().Write(logWARNING) << "[exception] " << e.what();
return EXIT_FAILURE;
}