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