osrm-backend/Extractor/Extractor.cpp

241 lines
9.0 KiB
C++
Raw Normal View History

2014-07-02 08:05:37 -04:00
/*
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Extractor.h"
2014-07-10 08:45:59 -04:00
2014-07-02 08:05:37 -04:00
#include "ExtractionContainers.h"
2014-08-26 11:50:34 -04:00
#include "ExtractionNode.h"
#include "ExtractionWay.h"
#include "ExtractorCallbacks.h"
#include "ExtractorOptions.h"
2014-08-26 11:50:34 -04:00
#include "RestrictionParser.h"
2014-07-02 10:47:57 -04:00
#include "ScriptingEnvironment.h"
2014-07-10 08:45:59 -04:00
2014-07-02 08:05:37 -04:00
#include "../Util/GitDescription.h"
2014-10-09 08:08:12 -04:00
#include "../Util/IniFileUtil.h"
2014-07-02 08:05:37 -04:00
#include "../Util/OSRMException.h"
#include "../Util/simple_logger.hpp"
2014-07-02 08:05:37 -04:00
#include "../Util/TimingUtil.h"
2014-08-26 11:50:34 -04:00
#include "../Util/make_unique.hpp"
2014-07-02 08:05:37 -04:00
#include "../typedefs.h"
2014-08-26 11:50:34 -04:00
#include <luabind/luabind.hpp>
#include <osmium/io/any_input.hpp>
#include <tbb/task_scheduler_init.h>
2014-07-02 08:05:37 -04:00
#include <cstdlib>
2014-07-02 10:47:57 -04:00
2014-07-02 08:05:37 -04:00
#include <chrono>
#include <fstream>
2014-07-10 08:45:59 -04:00
#include <iostream>
#include <thread>
2014-07-02 08:05:37 -04:00
#include <unordered_map>
2014-07-02 10:47:57 -04:00
namespace
2014-07-02 08:05:37 -04:00
{
2014-08-26 11:50:34 -04:00
int lua_error_callback(lua_State *L) // This is so I can use my own function as an
// exception handler, pcall_log()
{
luabind::object error_msg(luabind::from_stack(L, -1));
std::ostringstream error_stream;
error_stream << error_msg;
throw OSRMException("ERROR occured in profile script:\n" + error_stream.str());
}
}
2014-07-02 08:05:37 -04:00
2014-07-10 08:45:59 -04:00
int Extractor::Run(int argc, char *argv[])
2014-07-02 08:05:37 -04:00
{
ExtractorConfig extractor_config;
2014-07-02 08:05:37 -04:00
try
{
LogPolicy::GetInstance().Unmute();
TIMER_START(extracting);
if (!ExtractorOptions::ParseArguments(argc, argv, extractor_config))
{
2014-07-02 08:05:37 -04:00
return 0;
}
ExtractorOptions::GenerateOutputFilesNames(extractor_config);
2014-07-02 08:05:37 -04:00
if (1 > extractor_config.requested_num_threads)
2014-07-02 08:05:37 -04:00
{
SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger";
return 1;
}
if (!boost::filesystem::is_regular_file(extractor_config.input_path))
2014-07-02 08:05:37 -04:00
{
SimpleLogger().Write(logWARNING)
<< "Input file " << extractor_config.input_path.string() << " not found!";
2014-07-02 08:05:37 -04:00
return 1;
}
if (!boost::filesystem::is_regular_file(extractor_config.profile_path))
2014-07-02 08:05:37 -04:00
{
SimpleLogger().Write(logWARNING) << "Profile " << extractor_config.profile_path.string()
2014-07-02 08:05:37 -04:00
<< " not found!";
return 1;
}
const unsigned recommended_num_threads = tbb::task_scheduler_init::default_num_threads();
SimpleLogger().Write() << "Input file: " << extractor_config.input_path.filename().string();
SimpleLogger().Write() << "Profile: " << extractor_config.profile_path.filename().string();
SimpleLogger().Write() << "Threads: " << extractor_config.requested_num_threads;
if (recommended_num_threads != extractor_config.requested_num_threads)
2014-07-02 08:05:37 -04:00
{
SimpleLogger().Write(logWARNING) << "The recommended number of threads is "
<< recommended_num_threads
<< "! This setting may have performance side-effects.";
}
tbb::task_scheduler_init init(extractor_config.requested_num_threads);
2014-07-02 08:05:37 -04:00
/*** Setup Scripting Environment ***/
ScriptingEnvironment scripting_environment(extractor_config.profile_path.string().c_str());
2014-07-02 08:05:37 -04:00
std::unordered_map<std::string, NodeID> string_map;
ExtractionContainers extraction_containers;
string_map[""] = 0;
auto extractor_callbacks =
osrm::make_unique<ExtractorCallbacks>(extraction_containers, string_map);
2014-07-02 08:05:37 -04:00
osmium::io::File infile(extractor_config.input_path.string());
2014-08-26 11:50:34 -04:00
osmium::io::Reader reader(infile);
osmium::io::Header header = reader.header();
unsigned number_of_nodes = 0;
unsigned number_of_ways = 0;
unsigned number_of_relations = 0;
unsigned number_of_others = 0;
2014-07-10 08:45:59 -04:00
2014-07-02 08:05:37 -04:00
SimpleLogger().Write() << "Parsing in progress..";
TIMER_START(parsing);
2014-08-26 11:50:34 -04:00
std::string generator = header.get("generator");
if (generator.empty())
{
generator = "unknown tool";
}
SimpleLogger().Write() << "input file generated by " << generator;
2014-07-02 08:05:37 -04:00
2014-08-26 11:50:34 -04:00
// TODO: write timestamp if non-empty
std::string timestamp = header.get("osmosis_replication_timestamp");
if (timestamp.empty())
{
timestamp = "n/a";
}
SimpleLogger().Write() << "timestamp: " << timestamp;
boost::filesystem::ofstream timestamp_out(extractor_config.timestamp_file_name);
2014-08-26 11:50:34 -04:00
timestamp_out.write(timestamp.c_str(), timestamp.length());
timestamp_out.close();
lua_State *lua_state = scripting_environment.getLuaState();
luabind::set_pcall_callback(&lua_error_callback);
RestrictionParser restriction_parser(scripting_environment);
ExtractionNode result_node;
ExtractionWay result_way;
while (osmium::memory::Buffer buffer = reader.read())
{
2014-08-26 11:50:34 -04:00
for (osmium::OSMEntity &entity : buffer)
{
switch (entity.type())
{
case osmium::item_type::node:
++number_of_nodes;
result_node.Clear();
luabind::call_function<void>(lua_state,
"node_function",
boost::cref(static_cast<osmium::Node &>(entity)),
boost::ref(result_node));
extractor_callbacks->ProcessNode(static_cast<osmium::Node &>(entity),
result_node);
2014-08-26 11:50:34 -04:00
break;
case osmium::item_type::way:
++number_of_ways;
result_way.Clear();
luabind::call_function<void>(lua_state,
"way_function",
boost::cref(static_cast<osmium::Way &>(entity)),
boost::ref(result_way));
extractor_callbacks->ProcessWay(static_cast<osmium::Way &>(entity), result_way);
2014-08-26 11:50:34 -04:00
break;
case osmium::item_type::relation:
++number_of_relations;
extractor_callbacks->ProcessRestriction(
restriction_parser.TryParse(static_cast<osmium::Relation &>(entity)));
break;
default:
++number_of_others;
break;
}
}
}
2014-07-02 08:05:37 -04:00
TIMER_STOP(parsing);
2014-07-10 08:45:59 -04:00
SimpleLogger().Write() << "Parsing finished after " << TIMER_SEC(parsing) << " seconds";
SimpleLogger().Write() << "Raw input contains " << number_of_nodes << " nodes, "
<< number_of_ways << " ways, and " << number_of_relations
<< " relations";
2014-08-26 12:08:33 -04:00
2014-08-26 11:50:34 -04:00
extractor_callbacks.reset();
2014-07-02 08:05:37 -04:00
if (extraction_containers.all_edges_list.empty())
{
SimpleLogger().Write(logWARNING) << "The input data is empty, exiting.";
return 1;
}
extraction_containers.PrepareData(extractor_config.output_file_name,
extractor_config.restriction_file_name);
2014-07-02 08:05:37 -04:00
TIMER_STOP(extracting);
2014-07-10 08:45:59 -04:00
SimpleLogger().Write() << "extraction finished after " << TIMER_SEC(extracting) << "s";
2014-07-02 08:05:37 -04:00
SimpleLogger().Write() << "To prepare the data for routing, run: "
<< "./osrm-prepare " << extractor_config.output_file_name
<< std::endl;
2014-07-02 08:05:37 -04:00
}
catch (boost::program_options::too_many_positional_options_error &)
{
SimpleLogger().Write(logWARNING) << "Only one input file can be specified";
return 1;
}
catch (std::exception &e)
{
SimpleLogger().Write(logWARNING) << e.what();
return 1;
}
return 0;
}