SUMO - Simulation of Urban MObility
PCLoaderDlrNavteq.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A reader of pois and polygons stored in DLR-Navteq (Elmar)-format
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <map>
36 #include <fstream>
37 #include <sstream>
38 #include <iostream>
43 #include <utils/common/ToString.h>
48 #include <utils/options/Option.h>
49 #include <utils/common/StdDefs.h>
51 #include "PCLoaderDlrNavteq.h"
52 #include <utils/common/RGBColor.h>
53 #include <utils/geom/GeomHelper.h>
54 #include <utils/geom/Boundary.h>
55 #include <utils/geom/Position.h>
57 
58 #ifdef CHECK_MEMORY_LEAKS
59 #include <foreign/nvwa/debug_new.h>
60 #endif // CHECK_MEMORY_LEAKS
61 
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 void
68  PCTypeMap& tm) {
69  if (oc.isSet("dlr-navteq-poly-files")) {
70  loadPolyFiles(oc, toFill, tm);
71  }
72  if (oc.isSet("dlr-navteq-poi-files")) {
73  loadPOIFiles(oc, toFill, tm);
74  }
75 }
76 
77 
78 void
80  PCTypeMap& tm) {
81  std::vector<std::string> files = oc.getStringVector("dlr-navteq-poi-files");
82  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
83  if (!FileHelpers::isReadable(*file)) {
84  throw ProcessError("Could not open dlr-navteq-poi-file '" + *file + "'.");
85  }
86  PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poi-file '" + *file + "'");
87  loadPOIFile(*file, oc, toFill, tm);
89  }
90 }
91 
92 
93 void
95  PCTypeMap& tm) {
96  std::vector<std::string> files = oc.getStringVector("dlr-navteq-poly-files");
97  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
98  if (!FileHelpers::isReadable(*file)) {
99  throw ProcessError("Could not open dlr-navteq-poly-file '" + *file + "'.");
100  }
101  PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poly-file '" + *file + "'");
102  loadPolyFile(*file, oc, toFill, tm);
104  }
105 }
106 
107 
108 void
109 PCLoaderDlrNavteq::loadPOIFile(const std::string& file,
110  OptionsCont& oc, PCPolyContainer& toFill,
111  PCTypeMap& tm) {
112  // get the defaults
113  RGBColor c = RGBColor::parseColor(oc.getString("color"));
114  // parse
115  int l = 0;
116  LineReader lr(file);
117  while (lr.hasMore()) {
118  std::string line = lr.readLine();
119  ++l;
120  // skip invalid/empty lines
121  if (line.length() == 0 || line.find("#") != std::string::npos) {
122  continue;
123  }
124  if (StringUtils::prune(line) == "") {
125  continue;
126  }
127  // parse the poi
128  std::istringstream stream(line);
129  // attributes of the poi
130  std::string name, skip, type, desc;
131  std::getline(stream, name, '\t');
132  std::getline(stream, skip, '\t');
133  std::getline(stream, type, '\t');
134  std::getline(stream, desc, '\t');
135  if (stream.fail()) {
136  throw ProcessError("Invalid dlr-navteq-poi in line " + toString(l) + ":\n" + line);
137  }
138  double x, y;
139  stream >> x;
140  if (stream.fail()) {
141  throw ProcessError("Invalid x coordinate for POI '" + name + "'.");
142  }
143  stream >> y;
144  if (stream.fail()) {
145  throw ProcessError("Invalid y coordinate for POI '" + name + "'.");
146  }
147  Position pos(x, y);
148  // check the poi
149  if (name == "") {
150  throw ProcessError("The name of a POI is missing.");
151  }
152  if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) {
153  throw ProcessError("Unable to project coordinates for POI '" + name + "'.");
154  }
155 
156  // patch the values
157  bool discard = oc.getBool("discard");
158  int layer = oc.getInt("layer");
159  RGBColor color;
160  if (tm.has(type)) {
161  const PCTypeMap::TypeDef& def = tm.get(type);
162  name = def.prefix + name;
163  type = def.id;
164  color = def.color;
165  discard = def.discard;
166  layer = def.layer;
167  } else {
168  name = oc.getString("prefix") + name;
169  type = oc.getString("type");
170  color = c;
171  }
172  if (!discard) {
173  bool ignorePrunning = false;
174  if (OptionsCont::getOptions().isInStringVector("prune.keep-list", name)) {
175  ignorePrunning = true;
176  }
177  PointOfInterest* poi = new PointOfInterest(name, type, color, pos, (SUMOReal)layer);
178  toFill.insert(name, poi, layer, ignorePrunning);
179  }
180  }
181 }
182 
183 
184 void
185 PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
186  OptionsCont& oc, PCPolyContainer& toFill,
187  PCTypeMap& tm) {
188  // get the defaults
189  RGBColor c = RGBColor::parseColor(oc.getString("color"));
190  // attributes of the poly
191  // parse
192  int l = 0;
193  LineReader lr(file);
194  while (lr.hasMore()) {
195  std::string line = lr.readLine();
196  ++l;
197  // skip invalid/empty lines
198  if (line.length() == 0 || line.find("#") != std::string::npos) {
199  continue;
200  }
201  if (StringUtils::prune(line) == "") {
202  continue;
203  }
204  // parse the poi
205  StringTokenizer st(line, "\t");
206  std::vector<std::string> values = st.getVector();
207  if (values.size() < 6 || values.size() % 2 != 0) {
208  throw ProcessError("Invalid dlr-navteq-polygon - line: '" + line + "'.");
209  }
210  std::string id = values[0];
211  std::string ort = values[1];
212  std::string type = values[2];
213  std::string name = values[3];
214  PositionVector vec;
215  size_t index = 4;
216  // now collect the positions
217  while (values.size() > index) {
218  std::string xpos = values[index];
219  std::string ypos = values[index + 1];
220  index += 2;
221  SUMOReal x = TplConvert::_2SUMOReal(xpos.c_str());
222  SUMOReal y = TplConvert::_2SUMOReal(ypos.c_str());
223  Position pos(x, y);
224  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
225  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
226  }
227  vec.push_back(pos);
228  }
229 
230  name = StringUtils::convertUmlaute(name);
231  if (name == "noname" || toFill.containsPolygon(name)) {
232  name = name + "#" + toString(toFill.getEnumIDFor(name));
233  }
234 
235  // check the polygon
236  if (vec.size() == 0) {
237  WRITE_WARNING("The polygon '" + id + "' is empty.");
238  continue;
239  }
240  if (id == "") {
241  WRITE_WARNING("The name of a polygon is missing; it will be discarded.");
242  continue;
243  }
244 
245  // patch the values
246  bool fill = vec.front() == vec.back();
247  bool discard = oc.getBool("discard");
248  int layer = oc.getInt("layer");
249  RGBColor color;
250  if (tm.has(type)) {
251  const PCTypeMap::TypeDef& def = tm.get(type);
252  name = def.prefix + name;
253  type = def.id;
254  color = def.color;
255  fill = fill && def.allowFill;
256  discard = def.discard;
257  layer = def.layer;
258  } else {
259  name = oc.getString("prefix") + name;
260  type = oc.getString("type");
261  color = c;
262  }
263  if (!discard) {
264  Polygon* poly = new Polygon(name, type, color, vec, fill, (SUMOReal)layer);
265  toFill.insert(name, poly, layer);
266  }
267  vec.clear();
268  }
269 }
270 
271 
272 
273 
274 
275 /****************************************************************************/
276 
std::string id
The new type id to use.
Definition: PCTypeMap.h:68
static void loadPOIFiles(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois assumed to be stored as according DLR-Navteq (Elmar)-files.
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) ...
bool containsPolygon(const std::string &kidey)
Returns the information whether a polygon with the given key is in the container. ...
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:172
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:66
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:80
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:242
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:58
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:98
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
static void loadPolyFiles(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads polygons assumed to be stored as according DLR-Navteq (Elmar)-files.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
bool discard
Information whether polygons of this type shall be discarded.
Definition: PCTypeMap.h:76
A storage for loaded polygons and pois.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
RGBColor color
The color to use.
Definition: PCTypeMap.h:70
A 2D- or 3D-polygon.
Definition: Polygon.h:52
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A storage for type mappings.
Definition: PCTypeMap.h:52
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:79
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
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:85
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:54
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
Definition: StringUtils.cpp:95
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:56
std::string prefix
The prefix to use.
Definition: PCTypeMap.h:72
int getEnumIDFor(const std::string &key)
Retuns a unique id for a given name.
A storage for options typed value containers)
Definition: OptionsCont.h:108
#define SUMOReal
Definition: config.h:213
A point-of-interest.
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:64
int layer
The layer to use.
Definition: PCTypeMap.h:74
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
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:78
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.