SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
13 // Copyright (C) 2001-2013 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::exists(*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::exists(*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  if (!toFill.insert(name, poi, layer, ignorePrunning)) {
179  WRITE_ERROR("POI '" + name + "' could not be added.");
180  delete poi;
181  }
182  }
183  }
184 }
185 
186 
187 void
188 PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
189  OptionsCont& oc, PCPolyContainer& toFill,
190  PCTypeMap& tm) {
191  // get the defaults
192  RGBColor c = RGBColor::parseColor(oc.getString("color"));
193  // attributes of the poly
194  // parse
195  int l = 0;
196  LineReader lr(file);
197  while (lr.hasMore()) {
198  std::string line = lr.readLine();
199  ++l;
200  // skip invalid/empty lines
201  if (line.length() == 0 || line.find("#") != std::string::npos) {
202  continue;
203  }
204  if (StringUtils::prune(line) == "") {
205  continue;
206  }
207  // parse the poi
208  StringTokenizer st(line, "\t");
209  std::vector<std::string> values = st.getVector();
210  if (values.size() < 6 || values.size() % 2 != 0) {
211  throw ProcessError("Invalid dlr-navteq-polygon - line: '" + line + "'.");
212  }
213  std::string id = values[0];
214  std::string ort = values[1];
215  std::string type = values[2];
216  std::string name = values[3];
217  PositionVector vec;
218  size_t index = 4;
219  // now collect the positions
220  while (values.size() > index) {
221  std::string xpos = values[index];
222  std::string ypos = values[index + 1];
223  index += 2;
224  SUMOReal x = TplConvert::_2SUMOReal(xpos.c_str());
225  SUMOReal y = TplConvert::_2SUMOReal(ypos.c_str());
226  Position pos(x, y);
227  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
228  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
229  }
230  vec.push_back(pos);
231  }
232 
233  name = StringUtils::convertUmlaute(name);
234  if (name == "noname" || toFill.containsPolygon(name)) {
235  name = name + "#" + toString(toFill.getEnumIDFor(name));
236  }
237 
238  // check the polygon
239  if (vec.size() == 0) {
240  WRITE_WARNING("The polygon '" + id + "' is empty.");
241  continue;
242  }
243  if (id == "") {
244  WRITE_WARNING("The name of a polygon is missing; it will be discarded.");
245  continue;
246  }
247 
248  // patch the values
249  bool fill = vec.front() == vec.back();
250  bool discard = oc.getBool("discard");
251  int layer = oc.getInt("layer");
252  RGBColor color;
253  if (tm.has(type)) {
254  const PCTypeMap::TypeDef& def = tm.get(type);
255  name = def.prefix + name;
256  type = def.id;
257  color = def.color;
258  fill = fill && def.allowFill;
259  discard = def.discard;
260  layer = def.layer;
261  } else {
262  name = oc.getString("prefix") + name;
263  type = oc.getString("type");
264  color = c;
265  }
266  if (!discard) {
267  Polygon* poly = new Polygon(name, type, color, vec, fill, (SUMOReal)layer);
268  if (!toFill.insert(name, poly, layer)) {
269  WRITE_ERROR("Polygon '" + name + "' could not be added.");
270  delete poly;
271  }
272  }
273  vec.clear();
274  }
275 }
276 
277 
278 
279 
280 
281 /****************************************************************************/
282