SUMO - Simulation of Urban MObility
ShapeHandler.cpp
Go to the documentation of this file.
1 
2 /****************************************************************************/
8 // The XML-Handler for network loading
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <string>
33 #include <utils/xml/XMLSubSys.h>
37 #include <utils/common/RGBColor.h>
43 #include "Shape.h"
44 #include "ShapeContainer.h"
45 #include "ShapeHandler.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 ShapeHandler::ShapeHandler(const std::string& file, ShapeContainer& sc) :
56  SUMOSAXHandler(file),
57  myShapeContainer(sc) {}
58 
59 
61 
62 
63 void
65  const SUMOSAXAttributes& attrs) {
66  try {
67  switch (element) {
68  case SUMO_TAG_POLY:
69  addPoly(attrs);
70  break;
71  case SUMO_TAG_POI:
72  addPOI(attrs);
73  break;
74  default:
75  break;
76  }
77  } catch (InvalidArgument& e) {
78  WRITE_ERROR(e.what());
79  }
80 }
81 
82 
83 
84 void
86  bool ok = true;
87  const SUMOReal INVALID_POSITION(-1000000);
88  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
89  SUMOReal x = attrs.getOpt<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
90  SUMOReal y = attrs.getOpt<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
91  SUMOReal lon = attrs.getOpt<SUMOReal>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
92  SUMOReal lat = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
93  SUMOReal lanePos = attrs.getOpt<SUMOReal>(SUMO_ATTR_POSITION, id.c_str(), ok, INVALID_POSITION);
94  SUMOReal layer = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAYER, id.c_str(), ok, (SUMOReal)GLO_POI);
95  std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
96  std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
97  RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : RGBColor::RED;
98  SUMOReal angle = attrs.getOpt<SUMOReal>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
99  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
100  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
102  }
103  SUMOReal width = attrs.getOpt<SUMOReal>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
104  SUMOReal height = attrs.getOpt<SUMOReal>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
105  if (!ok) {
106  return;
107  }
108  Position pos(x, y);
109  if (x == INVALID_POSITION || y == INVALID_POSITION) {
110  // try computing x,y from lane,pos
111  if (laneID != "") {
112  pos = getLanePos(id, laneID, lanePos);
113  } else {
114  // try computing x,y from lon,lat
115  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
116  WRITE_ERROR("Either (x,y), (lon,lat) or (lane,pos) must be specified for poi '" + id + "'.");
117  return;
118  } else if (!GeoConvHelper::getFinal().usingGeoProjection()) {
119  WRITE_ERROR("(lon, lat) is specified for poi '" + id + "' but no geo-conversion is specified for the network.");
120  return;
121  }
122  pos.set(lon, lat);
124  }
125  }
126  if (!myShapeContainer.addPOI(id, type, color, layer, angle, imgFile, pos, width, height)) {
127  WRITE_ERROR("PoI '" + id + "' already exists.");
128  }
129 }
130 
131 
132 void
134  bool ok = true;
135  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
136  // get the id, report an error if not given or empty...
137  if (!ok) {
138  return;
139  }
140  SUMOReal layer = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAYER, id.c_str(), ok, Shape::DEFAULT_LAYER);
141  bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, false);
142  std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
143  std::string colorStr = attrs.get<std::string>(SUMO_ATTR_COLOR, id.c_str(), ok);
144  RGBColor color = attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok);
145  PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
146  if (attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false)) {
147  for (int i = 0; i < (int) shape.size(); i++) {
149  }
150  }
151  SUMOReal angle = attrs.getOpt<SUMOReal>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
152  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
153  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
155  }
156  if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, shape, fill)) {
157  WRITE_ERROR("Polygon '" + id + "' already exists.");
158  }
159 }
160 
161 
162 
163 bool
164 ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
165  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
166  if (!XMLSubSys::runParser(sh, *fileIt, false)) {
167  WRITE_MESSAGE("Loading of shapes from " + *fileIt + " failed.");
168  return false;
169  }
170  }
171  return true;
172 }
173 /****************************************************************************/
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:86
ShapeHandler(const std::string &file, ShapeContainer &sc)
Constructor.
bool x2cartesian_const(Position &from) const
Converts the given coordinate into a cartesian using the previous initialisation. ...
void addPoly(const SUMOSAXAttributes &attrs)
adds a polygon
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
A layer number.
static bool loadFiles(const std::vector< std::string > &files, ShapeHandler &sh)
loads all of the given files
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Storage for geometrical objects.
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&#39;s ok.
Definition: XMLSubSys.cpp:114
static const std::string DEFAULT_TYPE
Definition: Shape.h:149
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
The XML-Handler for network loading.
Definition: ShapeHandler.h:55
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:151
const std::string & getFileName() const
returns the current file name
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:154
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, SUMOReal layer, SUMOReal angle, const std::string &imgFile, const Position &pos, SUMOReal width, SUMOReal height)
Builds a POI using the given values and adds it to the container.
virtual Position getLanePos(const std::string &poiID, const std::string &laneID, SUMOReal lanePos)=0
get position for a given laneID
static const SUMOReal DEFAULT_LAYER
Definition: Shape.h:150
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
virtual ~ShapeHandler()
Destructor.
static const RGBColor RED
Definition: RGBColor.h:189
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, SUMOReal layer, SUMOReal angle, const std::string &imgFile, const PositionVector &shape, bool fill)
Builds a polygon using the given values and adds it to the container.
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
ShapeContainer & myShapeContainer
Definition: ShapeHandler.h:108
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
#define SUMOReal
Definition: config.h:214
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 WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
A color information.
Fill the polygon.
void addPOI(const SUMOSAXAttributes &attrs)
adds a polygon
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:153