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-sim.org/
13 // Copyright (C) 2001-2014 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.get<std::string>(SUMO_ATTR_ID, 0, ok);
109  SUMOReal x = attrs.get<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok);
110  SUMOReal y = attrs.get<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok);
111  std::string type = attrs.getOpt<std::string>(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 = 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.getOpt<SUMOReal>(SUMO_ATTR_LAYER, id.c_str(), ok, layer);
135  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
136  color = attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok);
137  }
138  SUMOReal angle = attrs.getOpt<SUMOReal>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
139  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
140  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
142  }
143  SUMOReal imgWidth = attrs.getOpt<SUMOReal>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
144  SUMOReal imgHeight = attrs.getOpt<SUMOReal>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
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  myCont.insert(id, poi, (int)layer, ignorePrunning);
155  }
156  }
157  if (element == SUMO_TAG_POLY) {
158  bool discard = myOptions.getBool("discard");
159  SUMOReal layer = (SUMOReal)myOptions.getInt("layer");
160  bool ok = true;
161  std::string id = attrs.getOpt<std::string>(SUMO_ATTR_ID, myCurrentID.c_str(), ok, "");
162  std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentID.c_str(), ok, myOptions.getString("type"));
163  if (!ok) {
164  return;
165  }
166  RGBColor color;
167  if (myTypeMap.has(type)) {
168  const PCTypeMap::TypeDef& def = myTypeMap.get(type);
169  id = def.prefix + id;
170  type = def.id;
171  color = def.color;
172  discard = def.discard;
173  layer = (SUMOReal)def.layer;
174  } else {
175  id = myOptions.getString("prefix") + id;
176  color = RGBColor::parseColor(myOptions.getString("color"));
177  }
178  layer = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAYER, id.c_str(), ok, layer);
179  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
180  color = attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok);
181  }
182  SUMOReal angle = attrs.getOpt<SUMOReal>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
183  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
184  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
186  }
187  bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, false);
188  if (!ok) {
189  return;
190  }
191  if (!discard) {
192  bool ignorePrunning = false;
193  if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) {
194  ignorePrunning = true;
195  }
196  myCurrentID = id;
197  myCurrentType = type;
198  myCurrentColor = color;
199  myCurrentIgnorePrunning = ignorePrunning;
200  myCurrentLayer = layer;
201  PositionVector pshape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, myCurrentID.c_str(), ok);
202  if (!ok) {
203  return;
204  }
205  PositionVector shape;
206  for (PositionVector::const_iterator i = pshape.begin(); i != pshape.end(); ++i) {
207  Position pos((*i));
208  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
209  WRITE_WARNING("Unable to project coordinates for polygon '" + myCurrentID + "'.");
210  }
211  shape.push_back(pos);
212  }
213  Polygon* poly = new Polygon(myCurrentID, myCurrentType, myCurrentColor, shape, fill, layer, angle, imgFile);
215  }
216  }
217 }
218 
219 
220 /****************************************************************************/
221 
std::string id
The new type id to use.
Definition: PCTypeMap.h:67
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) ...
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:154
PCLoaderXML(PCPolyContainer &toFill, PCTypeMap &tm, OptionsCont &oc)
Constructor.
Definition: PCLoaderXML.cpp:90
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:65
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:85
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:151
A reader for polygons and pois stored in XML-format.
Definition: PCLoaderXML.h:58
bool isInStringVector(const std::string &optionName, const std::string &itemName)
Returns the named option is a list of string values containing the specified item.
PCTypeMap & myTypeMap
The type map to use.
Definition: PCLoaderXML.h:109
RGBColor myCurrentColor
The color of the currently parsed polygon.
Definition: PCLoaderXML.h:125
A layer number.
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:97
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
OptionsCont & myOptions
Settings to use.
Definition: PCLoaderXML.h:112
bool myCurrentIgnorePrunning
Whether the current polygon must not be prunned.
Definition: PCLoaderXML.h:128
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
SAX-handler base for SUMO-files.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:114
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
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:67
RGBColor color
The color to use.
Definition: PCTypeMap.h:69
A 2D- or 3D-polygon.
Definition: Polygon.h:48
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:150
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
std::string myCurrentType
The type of the currently parsed polygon.
Definition: PCLoaderXML.h:122
A storage for type mappings.
Definition: PCTypeMap.h:51
const std::string & getFileName() const
returns the current file name
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:77
Encapsulated SAX-Attributes.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:153
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:83
static bool exists(std::string path)
Checks whether the given file exists.
Definition: FileHelpers.cpp:57
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:198
~PCLoaderXML()
Destructor.
Definition: PCLoaderXML.cpp:96
std::string myCurrentID
The id of the currently parsed polygon.
Definition: PCLoaderXML.h:119
void push_back(const PositionVector &p)
Appends all positions from the given vector.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
SUMOReal myCurrentLayer
The layer of the currently parsed polygon.
Definition: PCLoaderXML.h:131
std::string prefix
The prefix to use.
Definition: PCTypeMap.h:71
A storage for options typed value containers)
Definition: OptionsCont.h:108
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as XML.
Definition: PCLoaderXML.cpp:65
#define SUMOReal
Definition: config.h:215
A point-of-interest.
int layer
The layer to use.
Definition: PCTypeMap.h:73
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:199
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
A color information.
Fill the polygon.
PCPolyContainer & myCont
The container to store the converted polygons/pois into.
Definition: PCLoaderXML.h:106
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:152