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-2017 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 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 void
64  PCTypeMap& tm) {
65  if (oc.isSet("dlr-navteq-poly-files")) {
66  loadPolyFiles(oc, toFill, tm);
67  }
68  if (oc.isSet("dlr-navteq-poi-files")) {
69  loadPOIFiles(oc, toFill, tm);
70  }
71 }
72 
73 
74 void
76  PCTypeMap& tm) {
77  std::vector<std::string> files = oc.getStringVector("dlr-navteq-poi-files");
78  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
79  if (!FileHelpers::isReadable(*file)) {
80  throw ProcessError("Could not open dlr-navteq-poi-file '" + *file + "'.");
81  }
82  PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poi-file '" + *file + "'");
83  loadPOIFile(*file, oc, toFill, tm);
85  }
86 }
87 
88 
89 void
91  PCTypeMap& tm) {
92  std::vector<std::string> files = oc.getStringVector("dlr-navteq-poly-files");
93  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
94  if (!FileHelpers::isReadable(*file)) {
95  throw ProcessError("Could not open dlr-navteq-poly-file '" + *file + "'.");
96  }
97  PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poly-file '" + *file + "'");
98  loadPolyFile(*file, oc, toFill, tm);
100  }
101 }
102 
103 
104 void
105 PCLoaderDlrNavteq::loadPOIFile(const std::string& file,
106  OptionsCont& oc, PCPolyContainer& toFill,
107  PCTypeMap& tm) {
108  // get the defaults
109  RGBColor c = RGBColor::parseColor(oc.getString("color"));
110  // parse
111  int l = 0;
112  LineReader lr(file);
113  while (lr.hasMore()) {
114  std::string line = lr.readLine();
115  ++l;
116  // skip invalid/empty lines
117  if (line.length() == 0 || line.find("#") != std::string::npos) {
118  continue;
119  }
120  if (StringUtils::prune(line) == "") {
121  continue;
122  }
123  // parse the poi
124  std::istringstream stream(line);
125  // attributes of the poi
126  std::string name, skip, type, desc;
127  std::getline(stream, name, '\t');
128  std::getline(stream, skip, '\t');
129  std::getline(stream, type, '\t');
130  std::getline(stream, desc, '\t');
131  if (stream.fail()) {
132  throw ProcessError("Invalid dlr-navteq-poi in line " + toString(l) + ":\n" + line);
133  }
134  double x, y;
135  stream >> x;
136  if (stream.fail()) {
137  throw ProcessError("Invalid x coordinate for POI '" + name + "'.");
138  }
139  stream >> y;
140  if (stream.fail()) {
141  throw ProcessError("Invalid y coordinate for POI '" + name + "'.");
142  }
143  Position pos(x, y);
144  // check the poi
145  if (name == "") {
146  throw ProcessError("The name of a POI is missing.");
147  }
148  if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) {
149  throw ProcessError("Unable to project coordinates for POI '" + name + "'.");
150  }
151 
152  // patch the values
153  bool discard = oc.getBool("discard");
154  double layer = oc.getFloat("layer");
155  RGBColor color;
156  if (tm.has(type)) {
157  const PCTypeMap::TypeDef& def = tm.get(type);
158  name = def.prefix + name;
159  type = def.id;
160  color = def.color;
161  discard = def.discard;
162  layer = def.layer;
163  } else {
164  name = oc.getString("prefix") + name;
165  type = oc.getString("type");
166  color = c;
167  }
168  if (!discard) {
169  PointOfInterest* poi = new PointOfInterest(name, type, color, pos, layer);
170  toFill.add(poi, OptionsCont::getOptions().isInStringVector("prune.keep-list", name));
171  }
172  }
173 }
174 
175 
176 void
177 PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
178  OptionsCont& oc, PCPolyContainer& toFill,
179  PCTypeMap& tm) {
180  // get the defaults
181  RGBColor c = RGBColor::parseColor(oc.getString("color"));
182  // attributes of the poly
183  // parse
184  int l = 0;
185  LineReader lr(file);
186  while (lr.hasMore()) {
187  std::string line = lr.readLine();
188  ++l;
189  // skip invalid/empty lines
190  if (line.length() == 0 || line.find("#") != std::string::npos) {
191  continue;
192  }
193  if (StringUtils::prune(line) == "") {
194  continue;
195  }
196  // parse the poi
197  StringTokenizer st(line, "\t");
198  std::vector<std::string> values = st.getVector();
199  if (values.size() < 6 || values.size() % 2 != 0) {
200  throw ProcessError("Invalid dlr-navteq-polygon - line: '" + line + "'.");
201  }
202  std::string id = values[0];
203  std::string ort = values[1];
204  std::string type = values[2];
205  std::string name = values[3];
206  PositionVector vec;
207  int index = 4;
208  // now collect the positions
209  while ((int)values.size() > index) {
210  std::string xpos = values[index];
211  std::string ypos = values[index + 1];
212  index += 2;
213  double x = TplConvert::_2double(xpos.c_str());
214  double y = TplConvert::_2double(ypos.c_str());
215  Position pos(x, y);
216  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
217  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
218  }
219  vec.push_back(pos);
220  }
221 
222  name = StringUtils::convertUmlaute(name);
223  if (name == "noname" || toFill.getPolygons().get(name) != 0) {
224  name = name + "#" + toString(toFill.getEnumIDFor(name));
225  }
226 
227  // check the polygon
228  if (vec.size() == 0) {
229  WRITE_WARNING("The polygon '" + id + "' is empty.");
230  continue;
231  }
232  if (id == "") {
233  WRITE_WARNING("The name of a polygon is missing; it will be discarded.");
234  continue;
235  }
236 
237  // patch the values
238  bool fill = vec.front() == vec.back();
239  bool discard = oc.getBool("discard");
240  double layer = oc.getFloat("layer");
241  RGBColor color;
242  if (tm.has(type)) {
243  const PCTypeMap::TypeDef& def = tm.get(type);
244  name = def.prefix + name;
245  type = def.id;
246  color = def.color;
247  fill = fill && def.allowFill;
248  discard = def.discard;
249  layer = def.layer;
250  } else {
251  name = oc.getString("prefix") + name;
252  type = oc.getString("type");
253  color = c;
254  }
255  if (!discard) {
256  SUMO::Polygon* poly = new SUMO::Polygon(name, type, color, vec, fill, layer);
257  toFill.add(poly);
258  }
259  vec.clear();
260  }
261 }
262 
263 
264 
265 
266 
267 /****************************************************************************/
268 
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.
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:168
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:54
bool readLine(LineHandler &lh)
Reads a single (the next) line from the file and reports it to the given LineHandler.
Definition: LineReader.cpp:76
const Polygons & getPolygons() const
Returns all polygons.
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
double layer
The layer to use.
Definition: PCTypeMap.h:74
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: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:65
RGBColor color
The color to use.
Definition: PCTypeMap.h:70
A 2D- or 3D-polygon.
Definition: Polygon.h:57
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:52
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:75
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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:81
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:202
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool add(SUMO::Polygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
Definition: StringUtils.cpp:91
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:52
std::string prefix
The prefix to use.
Definition: PCTypeMap.h:72
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:60
int getEnumIDFor(const std::string &key)
Retuns a unique id for a given name.
A storage for options typed value containers)
Definition: OptionsCont.h:99
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
A point-of-interest.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
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