Io.cpp
Go to the documentation of this file.
1 #include "lanelet2_io/Io.h"
2 
3 #include <boost/filesystem.hpp>
4 
7 
8 namespace fs = boost::filesystem;
9 
10 namespace lanelet {
11 namespace {
12 std::string extension(const std::string& path) { return fs::path(path).extension().string(); }
13 
14 template <typename ExceptionT>
15 void handleErrorsOrThrow(const ErrorMessages& errors, ErrorMessages* targetErrs) {
16  if (targetErrs != nullptr) {
17  *targetErrs = errors;
18  } else if (!errors.empty()) {
19  throw ExceptionT(errors);
20  }
21 }
22 } // namespace
23 
24 std::unique_ptr<LaneletMap> load(const std::string& filename, const Origin& origin, ErrorMessages* errors,
25  const io::Configuration& params) {
26  return load(filename, defaultProjection(origin), errors, params);
27 }
28 
29 std::unique_ptr<LaneletMap> load(const std::string& filename, const Projector& projector, ErrorMessages* errors,
30  const io::Configuration& params) {
31  if (!fs::exists(fs::path(filename))) {
32  throw FileNotFoundError("Could not find lanelet map under " + filename);
33  }
34  ErrorMessages err;
35  auto map =
36  io_handlers::ParserFactory::createFromExtension(extension(filename), projector, params)->parse(filename, err);
37  handleErrorsOrThrow<ParseError>(err, errors);
38  return map;
39 }
40 
41 std::unique_ptr<LaneletMap> load(const std::string& filename, const std::string& parserName, const Origin& origin,
42  ErrorMessages* errors, const io::Configuration& params) {
43  return load(filename, parserName, defaultProjection(origin), errors, params);
44 }
45 
46 std::unique_ptr<LaneletMap> load(const std::string& filename, const std::string& parserName, const Projector& projector,
47  ErrorMessages* errors, const io::Configuration& params) {
48  if (!fs::exists(fs::path(filename))) {
49  throw FileNotFoundError("Could not find lanelet map under " + filename);
50  }
51  ErrorMessages err;
52  auto map = io_handlers::ParserFactory::create(parserName, projector, params)->parse(filename, err);
53  handleErrorsOrThrow<ParseError>(err, errors);
54  return map;
55 }
56 
57 std::vector<std::string> supportedParsers() { return io_handlers::ParserFactory::availableParsers(); }
58 
60 
61 void write(const std::string& filename, const LaneletMap& map, const Origin& origin, ErrorMessages* errors, const io::Configuration& params) {
62  write(filename, map, defaultProjection(origin), errors, params);
63 }
64 
65 void write(const std::string& filename, const LaneletMap& map, const Projector& projector, ErrorMessages* errors, const io::Configuration& params) {
66  ErrorMessages err;
67  io_handlers::WriterFactory::createFromExtension(extension(filename), projector, params)->write(filename, map, err, params);
68  handleErrorsOrThrow<WriteError>(err, errors);
69 }
70 
71 void write(const std::string& filename, const LaneletMap& map, const std::string& writerName, const Origin& origin, ErrorMessages* errors, const io::Configuration& params) {
72  write(filename, map, writerName, defaultProjection(origin), errors, params);
73 }
74 
75 void write(const std::string& filename, const LaneletMap& map, const std::string& writerName,
76  const Projector& projector, ErrorMessages* errors, const io::Configuration& params) {
77  ErrorMessages err;
78  io_handlers::WriterFactory::create(writerName, projector, params)->write(filename, map, err, params);
79  handleErrorsOrThrow<WriteError>(err, errors);
80 }
81 
82 std::vector<std::string> supportedWriters() { return io_handlers::WriterFactory::availableWriters(); }
83 
85 } // namespace lanelet
lanelet::io_handlers::WriterFactory::availableExtensions
static std::vector< std::string > availableExtensions()
returns all available extensions as vector
Definition: Factory.cpp:114
lanelet::ErrorMessages
std::vector< std::string > ErrorMessages
Definition: Io.h:11
lanelet::supportedWriters
std::vector< std::string > supportedWriters()
returns the names of the currently registered writing (writers from plugins included)
Definition: Io.cpp:82
lanelet::io_handlers::ParserFactory::availableParsers
static std::vector< std::string > availableParsers()
returns all available parsers as vector
Definition: Factory.cpp:47
lanelet::io_handlers::ParserFactory::createFromExtension
static Parser::Ptr createFromExtension(const std::string &extension, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a matching parser for the given file extension
Definition: Factory.cpp:35
lanelet
lanelet::io::Configuration
std::map< std::string, Attribute > Configuration
Definition: Configuration.h:8
lanelet::supportedWriterExtensions
std::vector< std::string > supportedWriterExtensions()
returns the names of the currently supported extensions for writing (including the dot)
Definition: Io.cpp:84
Io.h
lanelet::Projector
Definition: Projection.h:22
lanelet::LaneletMap
lanelet::io_handlers::WriterFactory::create
static Writer::Ptr create(const std::string &writerName, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a writer that matches the given name.
Definition: Factory.cpp:81
lanelet::load
std::unique_ptr< LaneletMap > load(const std::string &filename, const Origin &origin=Origin::defaultOrigin(), ErrorMessages *errors=nullptr, const io::Configuration &params=io::Configuration())
Loads a lanelet map from a file.
Definition: Io.cpp:24
lanelet::FileNotFoundError
Error for not existent filepaths.
Definition: Exceptions.h:20
lanelet::Origin
Definition: Projection.h:11
lanelet::write
void write(const std::string &filename, const lanelet::LaneletMap &map, const Origin &origin=Origin::defaultOrigin(), ErrorMessages *errors=nullptr, const io::Configuration &params=io::Configuration())
writes a map to a file
Definition: Io.cpp:61
lanelet::io_handlers::WriterFactory::createFromExtension
static Writer::Ptr createFromExtension(const std::string &extension, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a matching writer for the given file extension
Definition: Factory.cpp:93
lanelet::io_handlers::ParserFactory::create
static Parser::Ptr create(const std::string &parserName, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a parser that matches the given name.
Definition: Factory.cpp:23
lanelet::supportedParserExtensions
std::vector< std::string > supportedParserExtensions()
Definition: Io.cpp:59
lanelet::defaultProjection
DefaultProjector defaultProjection(Origin origin=Origin::defaultOrigin())
Definition: Projection.h:78
lanelet::io_handlers::ParserFactory::availableExtensions
static std::vector< std::string > availableExtensions()
returns all available extensions as vector
Definition: Factory.cpp:56
Factory.h
lanelet::io_handlers::WriterFactory::availableWriters
static std::vector< std::string > availableWriters()
returns all available writers as vector
Definition: Factory.cpp:105
Exceptions.h
lanelet::supportedParsers
std::vector< std::string > supportedParsers()
returns the names of the currently registered parsers (parsers from plugins included)
Definition: Io.cpp:57


lanelet2_io
Author(s): Fabian Poggenhans
autogenerated on Thu Mar 6 2025 03:26:03