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