osrm-backend/extractor.cpp

217 lines
8.6 KiB
C++
Raw Normal View History

/*
open source routing machine
Copyright (C) Dennis Luxen, others 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
2011-11-24 13:37:49 -05:00
#include "Extractor/ExtractorCallbacks.h"
#include "Extractor/ExtractionContainers.h"
#include "Extractor/ScriptingEnvironment.h"
2012-08-27 11:40:59 -04:00
#include "Extractor/PBFParser.h"
#include "Extractor/XMLParser.h"
2013-08-10 07:29:24 -04:00
#include "Util/GitDescription.h"
2011-05-07 03:36:17 -04:00
#include "Util/MachineInfo.h"
#include "Util/OpenMPWrapper.h"
2013-08-05 11:28:57 -04:00
#include "Util/OSRMException.h"
2013-08-10 07:29:24 -04:00
#include "Util/ProgramOptions.h"
#include "Util/SimpleLogger.h"
#include "Util/StringUtil.h"
#include "Util/UUID.h"
2013-06-24 16:47:35 -04:00
#include "typedefs.h"
#include <cstdlib>
2013-06-26 20:05:03 -04:00
2013-06-24 16:47:35 -04:00
#include <iostream>
#include <fstream>
#include <string>
ExtractorCallbacks * extractCallBacks;
UUID uuid;
int main (int argc, char *argv[]) {
2013-06-27 11:44:32 -04:00
try {
LogPolicy::GetInstance().Unmute();
2013-06-27 11:44:32 -04:00
double startup_time = get_timestamp();
2013-08-10 07:29:24 -04:00
boost::filesystem::path config_file_path, input_path, profile_path;
int requested_num_threads;
// declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options");
generic_options.add_options()
("version,v", "Show version")
("help,h", "Show this help message")
("config,c", boost::program_options::value<boost::filesystem::path>(&config_file_path)->default_value("extractor.ini"),
"Path to a configuration file.");
// declare a group of options that will be allowed both on command line and in config file
boost::program_options::options_description config_options("Configuration");
config_options.add_options()
("profile,p", boost::program_options::value<boost::filesystem::path>(&profile_path)->default_value("profile.lua"),
"Path to LUA routing profile")
("threads,t", boost::program_options::value<int>(&requested_num_threads)->default_value(10),
"Number of threads to use");
// hidden options, will be allowed both on command line and in config file, but will not be shown to the user
boost::program_options::options_description hidden_options("Hidden options");
hidden_options.add_options()
("input,i", boost::program_options::value<boost::filesystem::path>(&input_path),
"Input file in .osm, .osm.bz2 or .osm.pbf format");
// positional option
boost::program_options::positional_options_description positional_options;
positional_options.add("input", 1);
// combine above options for parsing
boost::program_options::options_description cmdline_options;
cmdline_options.add(generic_options).add(config_options).add(hidden_options);
boost::program_options::options_description config_file_options;
config_file_options.add(config_options).add(hidden_options);
boost::program_options::options_description visible_options(boost::filesystem::basename(argv[0]) + " <input.osm/.osm.bz2/.osm.pbf> [<profile.lua>]");
visible_options.add(generic_options).add(config_options);
// parse command line options
boost::program_options::variables_map option_variables;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
options(cmdline_options).positional(positional_options).run(), option_variables);
if(option_variables.count("version")) {
SimpleLogger().Write() << g_GIT_DESCRIPTION;
return 0;
2013-06-27 11:44:32 -04:00
}
2011-12-01 09:12:30 -05:00
2013-08-10 07:29:24 -04:00
if(option_variables.count("help")) {
SimpleLogger().Write() << visible_options;
return 0;
}
2012-11-22 13:24:34 -05:00
2013-08-10 07:29:24 -04:00
boost::program_options::notify(option_variables);
2013-08-14 05:59:46 -04:00
2013-08-10 07:29:24 -04:00
// parse config file
if(boost::filesystem::is_regular_file(config_file_path)) {
std::ifstream ifs(config_file_path.c_str());
SimpleLogger().Write() << "Reading options from: " << config_file_path.filename().string();
boost::program_options::store(parse_config_file(ifs, config_file_options), option_variables);
boost::program_options::notify(option_variables);
2013-06-26 20:05:03 -04:00
}
2013-06-27 11:44:32 -04:00
2013-08-10 07:29:24 -04:00
if(!option_variables.count("input")) {
SimpleLogger().Write(logWARNING) << "No input file specified";
return -1;
}
if(1 > requested_num_threads) {
SimpleLogger().Write(logWARNING) << "Number of threads must be 1 or larger";
return -1;
}
SimpleLogger().Write() << "Input file: " << input_path.filename().string();
SimpleLogger().Write() << "Profile: " << profile_path.filename().string();
SimpleLogger().Write() << "Threads: " << requested_num_threads;
/*** Setup Scripting Environment ***/
ScriptingEnvironment scriptingEnvironment(profile_path.c_str());
omp_set_num_threads( std::min( omp_get_num_procs(), requested_num_threads) );
2013-06-27 11:44:32 -04:00
bool file_has_pbf_format(false);
2013-08-10 07:29:24 -04:00
std::string output_file_name(input_path.c_str());
std::string restrictionsFileName(input_path.c_str());
2013-06-27 11:44:32 -04:00
std::string::size_type pos = output_file_name.find(".osm.bz2");
if(pos==std::string::npos) {
pos = output_file_name.find(".osm.pbf");
if(pos!=std::string::npos) {
file_has_pbf_format = true;
2013-07-17 07:20:48 -04:00
}
}
if(pos==std::string::npos) {
pos = output_file_name.find(".pbf");
if(pos!=std::string::npos) {
file_has_pbf_format = true;
2013-06-27 11:44:32 -04:00
}
}
if(pos!=std::string::npos) {
2013-06-27 11:44:32 -04:00
output_file_name.replace(pos, 8, ".osrm");
restrictionsFileName.replace(pos, 8, ".osrm.restrictions");
} else {
2013-06-27 11:44:32 -04:00
pos=output_file_name.find(".osm");
if(pos!=std::string::npos) {
output_file_name.replace(pos, 5, ".osrm");
restrictionsFileName.replace(pos, 5, ".osrm.restrictions");
} else {
output_file_name.append(".osrm");
restrictionsFileName.append(".osrm.restrictions");
}
}
2013-06-27 11:44:32 -04:00
unsigned amountOfRAM = 1;
unsigned installedRAM = GetPhysicalmemory();
if(installedRAM < 2048264) {
SimpleLogger().Write(logWARNING) << "Machine has less than 2GB RAM.";
2013-06-27 11:44:32 -04:00
}
2013-06-24 16:47:35 -04:00
2013-06-27 11:44:32 -04:00
StringMap stringMap;
ExtractionContainers externalMemory;
2013-06-27 11:44:32 -04:00
stringMap[""] = 0;
extractCallBacks = new ExtractorCallbacks(&externalMemory, &stringMap);
BaseParser* parser;
if(file_has_pbf_format) {
2013-08-10 07:29:24 -04:00
parser = new PBFParser(input_path.c_str(), extractCallBacks, scriptingEnvironment);
2013-06-27 11:44:32 -04:00
} else {
2013-08-10 07:29:24 -04:00
parser = new XMLParser(input_path.c_str(), extractCallBacks, scriptingEnvironment);
2013-06-27 11:44:32 -04:00
}
2013-06-24 16:47:35 -04:00
2013-06-27 11:44:32 -04:00
if(!parser->ReadHeader()) {
2013-08-05 11:28:57 -04:00
throw OSRMException("Parser not initialized!");
2013-06-27 11:44:32 -04:00
}
SimpleLogger().Write() << "Parsing in progress..";
2013-06-27 11:44:32 -04:00
double parsing_start_time = get_timestamp();
parser->Parse();
SimpleLogger().Write() << "Parsing finished after " <<
2013-06-27 11:44:32 -04:00
(get_timestamp() - parsing_start_time) <<
" seconds";
2013-06-27 11:44:32 -04:00
externalMemory.PrepareData(output_file_name, restrictionsFileName, amountOfRAM);
delete parser;
delete extractCallBacks;
SimpleLogger().Write() <<
"extraction finished after " << get_timestamp() - startup_time <<
"s";
2013-06-27 11:44:32 -04:00
SimpleLogger().Write() << "\nRun:\n./osrm-prepare " <<
2013-08-05 11:28:57 -04:00
output_file_name <<
" " <<
restrictionsFileName <<
std::endl;
2013-08-10 07:29:24 -04:00
} catch(boost::program_options::too_many_positional_options_error& e) {
SimpleLogger().Write(logWARNING) << "Only one input file can be specified";
return -1;
} catch(boost::program_options::error& e) {
SimpleLogger().Write(logWARNING) << e.what();
return -1;
2013-06-27 11:44:32 -04:00
} catch(std::exception & e) {
2013-08-10 07:29:24 -04:00
SimpleLogger().Write(logWARNING) << "Unhandled exception: " << e.what();
2013-08-05 11:28:57 -04:00
return -1;
2013-02-10 12:28:04 -05:00
}
2013-08-05 11:28:57 -04:00
return 0;
}