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