Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
PCLoaderDlrNavteq.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
21// A reader of pois and polygons stored in DLR-Navteq (Elmar)-format
22/****************************************************************************/
23#include <config.h>
24
25#include <string>
26#include <map>
27#include <fstream>
28#include <sstream>
29#include <iostream>
42#include "PCLoaderDlrNavteq.h"
45#include <utils/geom/Boundary.h>
46#include <utils/geom/Position.h>
48
49
50// ===========================================================================
51// method definitions
52// ===========================================================================
53void
55 PCTypeMap& tm) {
56 if (oc.isSet("dlr-navteq-poly-files")) {
57 loadPolyFiles(oc, toFill, tm);
58 }
59 if (oc.isSet("dlr-navteq-poi-files")) {
60 loadPOIFiles(oc, toFill, tm);
61 }
62}
63
64
65void
67 PCTypeMap& tm) {
68 std::vector<std::string> files = oc.getStringVector("dlr-navteq-poi-files");
69 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
70 if (!FileHelpers::isReadable(*file)) {
71 throw ProcessError(TLF("Could not open dlr-navteq-poi-file '%'.", *file));
72 }
73 PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poi-file '" + *file + "'");
74 loadPOIFile(*file, oc, toFill, tm);
76 }
77}
78
79
80void
82 PCTypeMap& tm) {
83 std::vector<std::string> files = oc.getStringVector("dlr-navteq-poly-files");
84 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
85 if (!FileHelpers::isReadable(*file)) {
86 throw ProcessError(TLF("Could not open dlr-navteq-poly-file '%'.", *file));
87 }
88 PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poly-file '" + *file + "'");
89 loadPolyFile(*file, oc, toFill, tm);
91 }
92}
93
94
95void
96PCLoaderDlrNavteq::loadPOIFile(const std::string& file,
97 OptionsCont& oc, PCPolyContainer& toFill,
98 PCTypeMap& tm) {
99 // get the defaults
100 RGBColor c = RGBColor::parseColor(oc.getString("color"));
101 // parse
102 int l = 0;
103 LineReader lr(file);
104 while (lr.hasMore()) {
105 std::string line = lr.readLine();
106 ++l;
107 // skip invalid/empty lines
108 if (line.length() == 0 || line.find("#") != std::string::npos) {
109 continue;
110 }
111 if (StringUtils::prune(line) == "") {
112 continue;
113 }
114 // parse the poi
115 std::istringstream stream(line);
116 // attributes of the poi
117 std::string name, skip, type, desc;
118 std::getline(stream, name, '\t');
119 std::getline(stream, skip, '\t');
120 std::getline(stream, type, '\t');
121 std::getline(stream, desc, '\t');
122 if (stream.fail()) {
123 throw ProcessError("Invalid dlr-navteq-poi in line " + toString(l) + ":\n" + line);
124 }
125 double x, y;
126 stream >> x;
127 if (stream.fail()) {
128 throw ProcessError(TLF("Invalid x coordinate for POI '%'.", name));
129 }
130 stream >> y;
131 if (stream.fail()) {
132 throw ProcessError(TLF("Invalid y coordinate for POI '%'.", name));
133 }
134 Position pos(x, y);
135 // check the poi
136 if (name == "") {
137 throw ProcessError(TL("The name of a POI is missing."));
138 }
139 if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) {
140 throw ProcessError(TLF("Unable to project coordinates for POI '%'.", name));
141 }
142
143 // patch the values
144 bool discard = oc.getBool("discard");
145 std::string icon = oc.getString("icon");
146 double layer = oc.getFloat("layer");
147 RGBColor color;
148 if (tm.has(type)) {
149 const PCTypeMap::TypeDef& def = tm.get(type);
150 name = def.prefix + name;
151 type = def.id;
152 color = def.color;
153 discard = def.discard;
154 icon = def.icon;
155 layer = def.layer;
156 } else {
157 name = oc.getString("prefix") + name;
158 type = oc.getString("type");
159 color = c;
160 }
161 if (!discard) {
162 PointOfInterest* poi = new PointOfInterest(name, type, color, pos, false, "", 0, false, 0, icon, layer);
163 toFill.add(poi, OptionsCont::getOptions().isInStringVector("prune.keep-list", name));
164 }
165 }
166}
167
168
169void
170PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
171 OptionsCont& oc, PCPolyContainer& toFill,
172 PCTypeMap& tm) {
173 // get the defaults
174 RGBColor c = RGBColor::parseColor(oc.getString("color"));
175 // attributes of the poly
176 // parse
177 LineReader lr(file);
178 while (lr.hasMore()) {
179 std::string line = lr.readLine();
180 // skip invalid/empty lines
181 if (line.length() == 0 || line.find("#") != std::string::npos) {
182 continue;
183 }
184 if (StringUtils::prune(line) == "") {
185 continue;
186 }
187 // parse the poi
188 StringTokenizer st(line, "\t");
189 std::vector<std::string> values = st.getVector();
190 if (values.size() < 6 || values.size() % 2 != 0) {
191 throw ProcessError(TLF("Invalid dlr-navteq-polygon - line: '%'.", line));
192 }
193 std::string id = values[0];
194 std::string ort = values[1];
195 std::string type = values[2];
196 std::string name = values[3];
197 PositionVector vec;
198 int index = 4;
199 // now collect the positions
200 while ((int)values.size() > index) {
201 std::string xpos = values[index];
202 std::string ypos = values[index + 1];
203 index += 2;
204 double x = StringUtils::toDouble(xpos);
205 double y = StringUtils::toDouble(ypos);
206 Position pos(x, y);
207 if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
208 WRITE_WARNINGF(TL("Unable to project coordinates for polygon '%'."), id);
209 }
210 vec.push_back(pos);
211 }
212
213 name = StringUtils::convertUmlaute(name);
214 if (name == "noname" || toFill.getPolygons().get(name) != 0) {
215 name = name + "#" + toString(toFill.getEnumIDFor(name));
216 }
217
218 // check the polygon
219 if (vec.size() == 0) {
220 WRITE_WARNINGF(TL("The polygon '%' is empty."), id);
221 continue;
222 }
223 if (id == "") {
224 WRITE_WARNING(TL("The name of a polygon is missing; it will be discarded."));
225 continue;
226 }
227
228 // patch the values
229 bool fill = vec.front() == vec.back();
230 bool discard = oc.getBool("discard");
231 std::string icon = oc.getString("icon");
232 double layer = oc.getFloat("layer");
233 RGBColor color;
234 if (tm.has(type)) {
235 const PCTypeMap::TypeDef& def = tm.get(type);
236 name = def.prefix + name;
237 type = def.id;
238 color = def.color;
239 fill = fill && def.allowFill != PCTypeMap::Filltype::NOFILL;
240 discard = def.discard;
241 icon = def.icon;
242 layer = def.layer;
243 } else {
244 name = oc.getString("prefix") + name;
245 type = oc.getString("type");
246 color = c;
247 }
248 if (!discard) {
249 SUMOPolygon* poly = new SUMOPolygon(name, type, color, vec, false, fill, 1, layer);
250 toFill.add(poly);
251 }
252 vec.clear();
253 }
254}
255
256
257/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
#define TL(string)
Definition MsgHandler.h:315
#define PROGRESS_DONE_MESSAGE()
Definition MsgHandler.h:300
#define TLF(string,...)
Definition MsgHandler.h:317
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition MsgHandler.h:299
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
static bool isReadable(std::string path)
Checks whether the given file is readable.
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Retrieves a file linewise and reports the lines to a handler.
Definition LineReader.h:48
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
T get(const std::string &id) const
Retrieves an item.
A storage for options typed value containers)
Definition OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
static void loadPOIFile(const std::string &file, OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads DLR-Navteq (Elmar)-pois from the given file.
static void loadPOIFiles(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois assumed to be stored as according DLR-Navteq (Elmar)-files.
static void loadPolyFiles(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads polygons assumed to be stored as according DLR-Navteq (Elmar)-files.
static void loadPolyFile(const std::string &file, OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads DLR-Navteq (Elmar)-polygons from the given file.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as according DLR-Navteq (Elmar)-files.
A storage for loaded polygons and pois.
bool add(SUMOPolygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
int getEnumIDFor(const std::string &key)
Retuns a unique id for a given name.
A storage for type mappings.
Definition PCTypeMap.h:42
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition PCTypeMap.cpp:70
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition PCTypeMap.cpp:76
A point-of-interest.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
A list of positions.
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition RGBColor.cpp:239
const Polygons & getPolygons() const
Returns all polygons.
std::vector< std::string > getVector()
return vector of strings
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
A single definition of values that shall be used for a given type.
Definition PCTypeMap.h:61
std::string icon
the icon to use
Definition PCTypeMap.h:69
bool discard
Information whether polygons of this type shall be discarded.
Definition PCTypeMap.h:77
std::string prefix
The prefix to use.
Definition PCTypeMap.h:67
double layer
The layer to use.
Definition PCTypeMap.h:71
std::string id
The new type id to use.
Definition PCTypeMap.h:63
RGBColor color
The color to use.
Definition PCTypeMap.h:65
Filltype allowFill
Information whether polygons of this type can be filled.
Definition PCTypeMap.h:79