Added OSM to XODR to PythonAPI.

This commit is contained in:
Axel1092 2020-07-15 12:12:58 +02:00 committed by bernat
parent f87593aa30
commit f9baef5050
10 changed files with 183 additions and 42 deletions

View File

@ -17,7 +17,7 @@ void export_converter() {
using namespace converter;
using namespace boost::python;
def("convert_to_odr", &ConvertOSMToOpenDRIVE, (arg("osm_file")));
def("convert_to_odr", &ConvertOSMToOpenDRIVE, (arg("osm_file"), arg("offsetX") = 0, arg("offsetY") = 0));
// class_<Converter>("Converter", init<>())
// .def("convert_to_odr", &Converter::ConvertOSMToOpenDRIVE, (arg("osm_file")))

View File

@ -66,17 +66,66 @@ namespace converter {
return ok;
}
std::string ConvertOSMToOpenDRIVE(std::string osm_file) {
OptionsCont::getOptions().clear();
std::string ConvertOSMToOpenDRIVE(std::string osm_file, double offsetX, double offsetY) {
// std::string OptionsArgs = "--geometry.remove --ramps.guess --edges.join --junctions.join --keep-edges.by-type highway.motorway,highway.motorway_link,highway.trunk,highway.trunk_link,highway.primary,highway.primary_link,highway.secondary,highway.secondary_link,highway.tertiary,highway.tertiary_link,highway.unclassified,highway.residential --tls.discard-loaded --tls.discard-simple --default.lanewidth 4.0 --osm.layer-elevation 4";
std::vector<std::string> OptionsArgs = {
"--proj", "+proj=tmerc",
"--geometry.remove", "--ramps.guess", "--edges.join", "--junctions.join",
"--keep-edges.by-type", "highway.motorway,highway.motorway_link,highway.trunk,highway.trunk_link,highway.primary,highway.primary_link,highway.secondary,highway.secondary_link,highway.tertiary,highway.tertiary_link,highway.unclassified,highway.residential",
"--tls.discard-loaded", "--tls.discard-simple", "--default.lanewidth", "4.0",
"--osm.layer-elevation", "4",
"--osm-files", "TRUE", "--opendrive-output", "TRUE", // necessary for now to enable osm input and xodr output
"--offset.x", std::to_string(offsetX), "--offset.y", std::to_string(offsetY)
};
// OptionsCont::getOptions().clear();
OptionsCont& oc = OptionsCont::getOptions();
oc.input_osm_file = osm_file;
XMLSubSys::init();
fillOptions();
std::string OptionsArgs = "--geometry.remove --ramps.guess --edges.join --junctions.join --keep-edges.by-type highway.motorway,highway.motorway_link,highway.trunk,highway.trunk_link,highway.primary,highway.primary_link,highway.secondary,highway.secondary_link,highway.tertiary,highway.tertiary_link,highway.unclassified,highway.residential --tls.discard-loaded --tls.discard-simple --default.lanewidth 4.0 --osm.layer-elevation 4"
if(checkOptions()) {
return osm_file + " succesful fail";
} else {
return osm_file + " sucesful success";
OptionsIO::setArgs(OptionsArgs);
OptionsIO::getOptions();
if (oc.processMetaOptions(OptionsArgs.size() < 2)) {
SystemFrame::close();
return 0;
}
XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
if (oc.isDefault("aggregate-warnings")) {
oc.set("aggregate-warnings", "5");
}
MsgHandler::initOutputOptions();
if (!checkOptions()) {
throw ProcessError();
}
RandHelper::initRandGlobal();
// build the projection
if (!GeoConvHelper::init(oc)) {
throw ProcessError("Could not build projection!");
}
NBNetBuilder nb;
nb.applyOptions(oc);
// load data
NILoader nl(nb);
nl.load(oc);
// flush aggregated errors and optionally ignore them
MsgHandler::getErrorInstance()->clear(oc.getBool("ignore-errors"));
// check whether any errors occurred
if (MsgHandler::getErrorInstance()->wasInformed()) {
throw ProcessError();
}
nb.compute(oc);
// check whether any errors occurred
if (MsgHandler::getErrorInstance()->wasInformed()) {
throw ProcessError();
}
NWFrame::writeNetwork(oc, nb);
DistributionCont::clear();
SystemFrame::close();
return oc.output_xodr_file;
}

View File

@ -10,6 +10,6 @@
namespace converter {
std::string ConvertOSMToOpenDRIVE(std::string osm_file);
std::string ConvertOSMToOpenDRIVE(std::string osm_file, double offsetX = 0, double offsetY = 0);
} // namespace converter

View File

@ -133,31 +133,47 @@ NIImporter_OpenStreetMap::load(const OptionsCont& oc, NBNetBuilder& nb) {
std::vector<std::string> files = oc.getStringVector("osm-files");
// load nodes, first
NodesHandler nodesHandler(myOSMNodes, myUniqueNodes, oc);
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
// nodes
if (!FileHelpers::isReadable(*file)) {
WRITE_ERROR("Could not open osm-file '" + *file + "'.");
return;
}
nodesHandler.setFileName(*file);
PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm-file '" + *file + "'");
if (!XMLSubSys::runParser(nodesHandler, *file)) {
return;
}
if (nodesHandler.getDuplicateNodes() > 0) {
WRITE_MESSAGE("Found and substituted " + toString(nodesHandler.getDuplicateNodes()) + " osm nodes.");
}
PROGRESS_DONE_MESSAGE();
// for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
// // nodes
// if (!FileHelpers::isReadable(*file)) {
// WRITE_ERROR("Could not open osm-file '" + *file + "'.");
// return;
// }
// nodesHandler.setFileName(*file);
// PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm-file '" + *file + "'");
// if (!XMLSubSys::runParser(nodesHandler, *file)) {
// return;
// }
// if (nodesHandler.getDuplicateNodes() > 0) {
// WRITE_MESSAGE("Found and substituted " + toString(nodesHandler.getDuplicateNodes()) + " osm nodes.");
// }
// PROGRESS_DONE_MESSAGE();
// }
nodesHandler.setFileName("osm_file");
PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm string");
if (!XMLSubSys::runParserFromString(nodesHandler, oc.input_osm_file)) {
return;
}
if (nodesHandler.getDuplicateNodes() > 0) {
WRITE_MESSAGE("Found and substituted " + toString(nodesHandler.getDuplicateNodes()) + " osm nodes.");
}
PROGRESS_DONE_MESSAGE();
// load edges, then
EdgesHandler edgesHandler(myOSMNodes, myEdges, myPlatformShapes);
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
// edges
edgesHandler.setFileName(*file);
PROGRESS_BEGIN_MESSAGE("Parsing edges from osm-file '" + *file + "'");
XMLSubSys::runParser(edgesHandler, *file);
PROGRESS_DONE_MESSAGE();
}
// for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
// // edges
// edgesHandler.setFileName(*file);
// PROGRESS_BEGIN_MESSAGE("Parsing edges from osm-file '" + *file + "'");
// XMLSubSys::runParser(edgesHandler, *file);
// PROGRESS_DONE_MESSAGE();
// }
// edges
edgesHandler.setFileName("osm_file");
PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm string");
XMLSubSys::runParserFromString(edgesHandler, oc.input_osm_file);
PROGRESS_DONE_MESSAGE();
/* Remove duplicate edges with the same shape and attributes */
if (!oc.getBool("osm.skip-duplicates-check")) {
@ -260,13 +276,19 @@ NIImporter_OpenStreetMap::load(const OptionsCont& oc, NBNetBuilder& nb) {
// turn-restrictions directly to NBEdges)
RelationHandler relationHandler(myOSMNodes, myEdges, &(nb.getPTStopCont()), myPlatformShapes,
&nb.getPTLineCont(), oc);
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
// relations
relationHandler.setFileName(*file);
PROGRESS_BEGIN_MESSAGE("Parsing relations from osm-file '" + *file + "'");
XMLSubSys::runParser(relationHandler, *file);
PROGRESS_DONE_MESSAGE();
}
// for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
// // relations
// relationHandler.setFileName(*file);
// PROGRESS_BEGIN_MESSAGE("Parsing relations from osm-file '" + *file + "'");
// XMLSubSys::runParser(relationHandler, *file);
// PROGRESS_DONE_MESSAGE();
// }
// relations
relationHandler.setFileName("osm-file");
PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm string");
XMLSubSys::runParserFromString(edgesHandler, oc.input_osm_file);
PROGRESS_DONE_MESSAGE();
}
NBNode*

View File

@ -67,7 +67,7 @@ NWWriter_OpenDrive::writeNetwork(const OptionsCont& oc, NBNetBuilder& nb) {
StringBijection<int> edgeMap;
StringBijection<int> nodeMap;
//
OutputDevice& device = OutputDevice::getDevice(oc.getString("opendrive-output"));
OutputDevice_String device;
device << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
device.openTag("OpenDRIVE");
time_t now = time(nullptr);
@ -212,7 +212,10 @@ NWWriter_OpenDrive::writeNetwork(const OptionsCont& oc, NBNetBuilder& nb) {
}
device.closeTag();
device.close();
OptionsCont::getOptions().output_xodr_file = device.getString();
// device.close();
}

View File

@ -670,6 +670,9 @@ public:
return myFullName;
}
std::string input_osm_file;
std::string output_xodr_file;
private:
/** @brief Returns the named option
*

View File

@ -55,6 +55,27 @@ OptionsParser::parse(int argc, char** argv) {
return ok;
}
bool
OptionsParser::parse(std::vector<std::string> args) {
bool ok = true;
for (size_t i = 1; i < args.size();) {
try {
int add;
// try to set the current option
if (i < args.size()- 1) {
add = check(args[i].c_str(), args[i + 1].c_str(), ok);
} else {
add = check(args[i].c_str(), nullptr, ok);
}
i += add;
} catch (ProcessError& e) {
WRITE_ERROR("On processing option '" + args[i] + "':\n " + e.what());
i++;
ok = false;
}
}
return ok;
}
int
OptionsParser::check(const char* arg1, const char* arg2, bool& ok) {

View File

@ -22,6 +22,8 @@
#pragma once
#include <config.h>
#include <string>
#include <vector>
// ===========================================================================
// class declarations
@ -55,6 +57,8 @@ public:
*/
static bool parse(int argc, char** argv);
static bool parse(std::vector<std::string> args);
private:
/** @brief parses the previous arguments
*

View File

@ -143,5 +143,42 @@ XMLSubSys::runParser(GenericSAXHandler& handler,
return !MsgHandler::getErrorInstance()->wasInformed();
}
bool
XMLSubSys::runParserFromString(GenericSAXHandler& handler,
const std::string& xml, const bool isNet) {
std::string file = "String XML";
MsgHandler::getErrorInstance()->clear();
try {
XERCES_CPP_NAMESPACE::SAX2XMLReader::ValSchemes validationScheme = isNet ? myNetValidationScheme : myValidationScheme;
if (myNextFreeReader == (int)myReaders.size()) {
myReaders.push_back(new SUMOSAXReader(handler, validationScheme));
} else {
myReaders[myNextFreeReader]->setValidation(validationScheme);
myReaders[myNextFreeReader]->setHandler(handler);
}
myNextFreeReader++;
std::string prevFile = handler.getFileName();
handler.setFileName(file);
myReaders[myNextFreeReader - 1]->parseString(xml);
handler.setFileName(prevFile);
myNextFreeReader--;
} catch (AbortParsing&) {
return false;
} catch (ProcessError& e) {
WRITE_ERROR(std::string(e.what()) != std::string("") ? std::string(e.what()) : std::string("Process Error"));
return false;
} catch (const std::runtime_error& re) {
WRITE_ERROR("Runtime error: " + std::string(re.what()) + " while parsing '" + file + "'");
return false;
} catch (const std::exception& ex) {
WRITE_ERROR("Error occurred: " + std::string(ex.what()) + " while parsing '" + file + "'");
return false;
} catch (...) {
WRITE_ERROR("Unspecified error occurred wile parsing '" + file + "'");
return false;
}
return !MsgHandler::getErrorInstance()->wasInformed();
}
/****************************************************************************/

View File

@ -146,6 +146,8 @@ public:
static bool runParser(GenericSAXHandler& handler,
const std::string& file, const bool isNet = false);
static bool runParserFromString(GenericSAXHandler& handler,
const std::string& xml, const bool isNet = false);
private:
/// @brief The XML Readers used for repeated parsing