Eclipse SUMO - Simulation of Urban MObility
PCLoaderDlrNavteq.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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>
34 #include <utils/common/ToString.h>
39 #include <utils/options/Option.h>
40 #include <utils/common/StdDefs.h>
42 #include "PCLoaderDlrNavteq.h"
43 #include <utils/common/RGBColor.h>
44 #include <utils/geom/GeomHelper.h>
45 #include <utils/geom/Boundary.h>
46 #include <utils/geom/Position.h>
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 void
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 
65 void
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("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 
80 void
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("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 
95 void
96 PCLoaderDlrNavteq::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("Invalid x coordinate for POI '" + name + "'.");
129  }
130  stream >> y;
131  if (stream.fail()) {
132  throw ProcessError("Invalid y coordinate for POI '" + name + "'.");
133  }
134  Position pos(x, y);
135  // check the poi
136  if (name == "") {
137  throw ProcessError("The name of a POI is missing.");
138  }
139  if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) {
140  throw ProcessError("Unable to project coordinates for POI '" + name + "'.");
141  }
142 
143  // patch the values
144  bool discard = oc.getBool("discard");
145  double layer = oc.getFloat("layer");
146  RGBColor color;
147  if (tm.has(type)) {
148  const PCTypeMap::TypeDef& def = tm.get(type);
149  name = def.prefix + name;
150  type = def.id;
151  color = def.color;
152  discard = def.discard;
153  layer = def.layer;
154  } else {
155  name = oc.getString("prefix") + name;
156  type = oc.getString("type");
157  color = c;
158  }
159  if (!discard) {
160  PointOfInterest* poi = new PointOfInterest(name, type, color, pos, false, "", 0, 0, layer);
161  toFill.add(poi, OptionsCont::getOptions().isInStringVector("prune.keep-list", name));
162  }
163  }
164 }
165 
166 
167 void
168 PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
169  OptionsCont& oc, PCPolyContainer& toFill,
170  PCTypeMap& tm) {
171  // get the defaults
172  RGBColor c = RGBColor::parseColor(oc.getString("color"));
173  // attributes of the poly
174  // parse
175  int l = 0;
176  LineReader lr(file);
177  while (lr.hasMore()) {
178  std::string line = lr.readLine();
179  ++l;
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("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_WARNING("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_WARNING("The polygon '" + id + "' is empty.");
221  continue;
222  }
223  if (id == "") {
224  WRITE_WARNING("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  double layer = oc.getFloat("layer");
232  RGBColor color;
233  if (tm.has(type)) {
234  const PCTypeMap::TypeDef& def = tm.get(type);
235  name = def.prefix + name;
236  type = def.id;
237  color = def.color;
238  fill = fill && def.allowFill;
239  discard = def.discard;
240  layer = def.layer;
241  } else {
242  name = oc.getString("prefix") + name;
243  type = oc.getString("type");
244  color = c;
245  }
246  if (!discard) {
247  SUMOPolygon* poly = new SUMOPolygon(name, type, color, vec, false, fill, 1, layer);
248  toFill.add(poly);
249  }
250  vec.clear();
251  }
252 }
253 
254 
255 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:280
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:279
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:48
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:84
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.
Definition: LineReader.cpp:67
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:51
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.
Definition: OptionsCont.cpp:58
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:69
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:75
A point-of-interest.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
A list of positions.
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:168
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.
Definition: StringUtils.cpp:47
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
Definition: StringUtils.cpp:86
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:56
bool discard
Information whether polygons of this type shall be discarded.
Definition: PCTypeMap.h:70
std::string prefix
The prefix to use.
Definition: PCTypeMap.h:62
double layer
The layer to use.
Definition: PCTypeMap.h:64
bool allowFill
Information whether polygons of this type can be filled.
Definition: PCTypeMap.h:72
std::string id
The new type id to use.
Definition: PCTypeMap.h:58
RGBColor color
The color to use.
Definition: PCTypeMap.h:60