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, "");
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  int layer = 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 = def.layer;
130  } else {
131  id = myOptions.getString("prefix") + id;
132  type = myOptions.getString("type");
133  color = RGBColor::parseColor(myOptions.getString("color"));
134  }
135  if (!discard) {
136  bool ignorePrunning = false;
137  if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) {
138  ignorePrunning = true;
139  }
140  PointOfInterest* poi = new PointOfInterest(id, type, pos, color);
141  if (!myCont.insert(id, poi, layer, ignorePrunning)) {
142  WRITE_ERROR("POI '" + id + "' could not been added.");
143  delete poi;
144  }
145  }
146  }
147  if (element == SUMO_TAG_POLY) {
148  bool discard = myOptions.getBool("discard");
149  int layer = myOptions.getInt("layer");
150  bool ok = true;
151  std::string id = attrs.getOptStringReporting(SUMO_ATTR_ID, myCurrentID.c_str(), ok, "");
152  std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, myCurrentID.c_str(), ok, "");
153  if (!ok) {
154  return;
155  }
156  RGBColor color;
157  if (myTypeMap.has(type)) {
158  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
159  id = def.prefix + id;
160  type = def.id;
161  color = RGBColor::parseColor(def.color);
162  discard = def.discard;
163  layer = def.layer;
164  } else {
165  id = myOptions.getString("prefix") + id;
166  type = myOptions.getString("type");
167  color = RGBColor::parseColor(myOptions.getString("color"));
168  }
169  if (!discard) {
170  bool ignorePrunning = false;
171  if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) {
172  ignorePrunning = true;
173  }
174  myCurrentID = id;
175  myCurrentType = type;
176  myCurrentColor = color;
177  myCurrentIgnorePrunning = ignorePrunning;
178  myCurrentLayer = layer;
179  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
180  // @deprecated At some time, no shape definition using characters will be allowed
181  myCharacters(element, attrs.getStringReporting(SUMO_ATTR_SHAPE, myCurrentID.c_str(), ok));
182  }
183  }
184  }
185 }
186 
187 
188 void
190  const std::string& chars) {
191  if (element == SUMO_TAG_POLY) {
192  bool ok = true;
193  PositionVector pshape = GeomConvHelper::parseShapeReporting(chars, "poly", myCurrentID.c_str(), ok, false);
194  if (!ok) {
195  return;
196  }
197  PositionVector shape;
198  for (PositionVector::ContType::const_iterator i = pshape.begin(); i != pshape.end(); ++i) {
199  Position pos((*i));
200  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
201  WRITE_WARNING("Unable to project coordinates for polygon '" + myCurrentID + "'.");
202  }
203  shape.push_back(pos);
204  }
205  Polygon* poly = new Polygon(myCurrentID, myCurrentType, myCurrentColor, shape, false);
207  WRITE_ERROR("Polygon '" + myCurrentID + "' could not been added.");
208  delete poly;
209  }
210  }
211 }
212 
213 
214 /****************************************************************************/
215