SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PCLoaderXML.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A reader for polygons and pois stored in XML-format
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2012 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>
38 #include <utils/options/Option.h>
39 #include <utils/common/StdDefs.h>
41 #include <utils/common/RGBColor.h>
42 #include <utils/geom/GeomHelper.h>
43 #include <utils/geom/Boundary.h>
44 #include <utils/geom/Position.h>
46 #include <utils/xml/XMLSubSys.h>
51 #include "PCLoaderXML.h"
52 
53 #ifdef CHECK_MEMORY_LEAKS
54 #include <foreign/nvwa/debug_new.h>
55 #endif // CHECK_MEMORY_LEAKS
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 // ---------------------------------------------------------------------------
62 // static interface
63 // ---------------------------------------------------------------------------
64 void
66  PCTypeMap& tm) {
67  if (!oc.isSet("xml-files")) {
68  return;
69  }
70  PCLoaderXML handler(toFill, tm, oc);
71  // parse file(s)
72  std::vector<std::string> files = oc.getStringVector("xml");
73  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
74  if (!FileHelpers::exists(*file)) {
75  throw ProcessError("Could not open xml-file '" + *file + "'.");
76  }
77  PROGRESS_BEGIN_MESSAGE("Parsing XML from '" + *file + "'");
78  if (!XMLSubSys::runParser(handler, *file)) {
79  throw ProcessError();
80  }
82  }
83 }
84 
85 
86 
87 // ---------------------------------------------------------------------------
88 // handler methods
89 // ---------------------------------------------------------------------------
91  PCTypeMap& tm, OptionsCont& oc)
92  : SUMOSAXHandler("xml-poi-definition"),
93  myCont(toFill), myTypeMap(tm), myOptions(oc) {}
94 
95 
97 
98 
99 void
101  const SUMOSAXAttributes& attrs) {
102  if (element != SUMO_TAG_POI && element != SUMO_TAG_POLY) {
103  return;
104  }
105  if (element == SUMO_TAG_POI) {
106  bool ok = true;
107  // get the id, report an error if not given or empty...
108  std::string id = attrs.getStringReporting(SUMO_ATTR_ID, 0, ok);
109  SUMOReal x = attrs.getSUMORealReporting(SUMO_ATTR_X, id.c_str(), ok);
110  SUMOReal y = attrs.getSUMORealReporting(SUMO_ATTR_Y, id.c_str(), ok);
111  std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, id.c_str(), ok, myOptions.getString("type"));
112  if (!ok) {
113  return;
114  }
115  Position pos(x, y);
116  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
117  WRITE_WARNING("Unable to project coordinates for POI '" + id + "'.");
118  }
119  // patch the values
120  bool discard = myOptions.getBool("discard");
121  SUMOReal layer = (SUMOReal)myOptions.getInt("layer");
122  RGBColor color;
123  if (myTypeMap.has(type)) {
124  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
125  id = def.prefix + id;
126  type = def.id;
127  color = RGBColor::parseColor(def.color);
128  discard = def.discard;
129  layer = (SUMOReal)def.layer;
130  } else {
131  id = myOptions.getString("prefix") + id;
132  color = RGBColor::parseColor(myOptions.getString("color"));
133  }
134  layer = attrs.getOptSUMORealReporting(SUMO_ATTR_LAYER, id.c_str(), ok, layer);
135  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
136  color = attrs.getColorReporting(id.c_str(), ok);
137  }
139  std::string imgFile = attrs.getOptStringReporting(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
140  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
142  }
145  if (!ok) {
146  return;
147  }
148  if (!discard) {
149  bool ignorePrunning = false;
150  if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) {
151  ignorePrunning = true;
152  }
153  PointOfInterest* poi = new PointOfInterest(id, type, color, pos, layer, angle, imgFile, imgWidth, imgHeight);
154  if (!myCont.insert(id, poi, (int)layer, ignorePrunning)) {
155  WRITE_ERROR("POI '" + id + "' could not be added.");
156  delete poi;
157  }
158  }
159  }
160  if (element == SUMO_TAG_POLY) {
161  bool discard = myOptions.getBool("discard");
162  SUMOReal layer = (SUMOReal)myOptions.getInt("layer");
163  bool ok = true;
164  std::string id = attrs.getOptStringReporting(SUMO_ATTR_ID, myCurrentID.c_str(), ok, "");
165  std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, myCurrentID.c_str(), ok, myOptions.getString("type"));
166  if (!ok) {
167  return;
168  }
169  RGBColor color;
170  if (myTypeMap.has(type)) {
171  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
172  id = def.prefix + id;
173  type = def.id;
174  color = RGBColor::parseColor(def.color);
175  discard = def.discard;
176  layer = (SUMOReal)def.layer;
177  } else {
178  id = myOptions.getString("prefix") + id;
179  color = RGBColor::parseColor(myOptions.getString("color"));
180  }
181  layer = attrs.getOptSUMORealReporting(SUMO_ATTR_LAYER, id.c_str(), ok, layer);
182  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
183  color = attrs.getColorReporting(id.c_str(), ok);
184  }
186  std::string imgFile = attrs.getOptStringReporting(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
187  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
189  }
190  bool fill = attrs.getOptBoolReporting(SUMO_ATTR_FILL, id.c_str(), ok, false);
191  if (!ok) {
192  return;
193  }
194  if (!discard) {
195  bool ignorePrunning = false;
196  if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) {
197  ignorePrunning = true;
198  }
199  myCurrentID = id;
200  myCurrentType = type;
201  myCurrentColor = color;
202  myCurrentIgnorePrunning = ignorePrunning;
203  myCurrentLayer = layer;
204  PositionVector pshape = attrs.getShapeReporting(SUMO_ATTR_SHAPE, myCurrentID.c_str(), ok, false);
205  if (!ok) {
206  return;
207  }
208  PositionVector shape;
209  for (PositionVector::ContType::const_iterator i = pshape.begin(); i != pshape.end(); ++i) {
210  Position pos((*i));
211  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
212  WRITE_WARNING("Unable to project coordinates for polygon '" + myCurrentID + "'.");
213  }
214  shape.push_back(pos);
215  }
216  Polygon* poly = new Polygon(myCurrentID, myCurrentType, myCurrentColor, shape, fill, layer, angle, imgFile);
218  WRITE_ERROR("Polygon '" + myCurrentID + "' could not be added.");
219  delete poly;
220  }
221  }
222  }
223 }
224 
225 
226 /****************************************************************************/
227