Eclipse SUMO - Simulation of Urban MObility
ShapeHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // The XML-Handler for network loading
16 /****************************************************************************/
17 // ===========================================================================
18 // included modules
19 // ===========================================================================
20 #include <config.h>
21 
22 #include <string>
25 #include <utils/xml/XMLSubSys.h>
29 #include <utils/common/RGBColor.h>
35 
36 #include "Shape.h"
37 #include "ShapeContainer.h"
38 #include "ShapeHandler.h"
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 
45 ShapeHandler::ShapeHandler(const std::string& file, ShapeContainer& sc, const GeoConvHelper* geoConvHelper) :
46  SUMOSAXHandler(file),
47  myShapeContainer(sc),
48  myPrefix(""),
49  myDefaultColor(RGBColor::RED),
50  myDefaultLayer(0),
51  myDefaultFill(false),
52  myLastParameterised(nullptr),
53  myGeoConvHelper(geoConvHelper) {
54 }
55 
56 
58 
59 
60 void
61 ShapeHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
62  try {
63  switch (element) {
64  case SUMO_TAG_POLY:
65  // default layer is different depending if we're parsing a Poly or a POI, therefore it has to be here defined
67  addPoly(attrs, false, false);
68  break;
69  case SUMO_TAG_POI:
70  // default layer is different depending if we're parsing a Poly or a POI, therefore it has to be here defined
72  addPOI(attrs, false, false);
73  break;
74  case SUMO_TAG_PARAM:
75  if (myLastParameterised != nullptr) {
76  bool ok = true;
77  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
78  // continue if key awas sucesfully loaded
79  if (ok) {
80  // circumventing empty string value
81  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
82  // show warnings if values are invalid
83  if (key.empty()) {
84  WRITE_WARNING("Error parsing key from shape generic parameter. Key cannot be empty");
86  WRITE_WARNING("Error parsing key from shape generic parameter. Key contains invalid characters");
88  WRITE_WARNING("Error parsing value from shape generic parameter. Value contains invalid characters");
89  } else {
90  WRITE_DEBUG("Inserting generic parameter '" + key + "|" + val + "' into shape.");
92  }
93  }
94  }
95  default:
96  break;
97  }
98  } catch (InvalidArgument& e) {
99  WRITE_ERROR(e.what());
100  }
101 }
102 
103 
104 void
106  if (element != SUMO_TAG_PARAM) {
107  myLastParameterised = nullptr;
108  }
109 }
110 
111 
112 void
113 ShapeHandler::addPOI(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
114  bool ok = true;
115  const double INVALID_POSITION(-1000000);
116  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
117  double x = attrs.getOpt<double>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
118  const double y = attrs.getOpt<double>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
119  double lon = attrs.getOpt<double>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
120  double lat = attrs.getOpt<double>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
121  const double lanePos = attrs.getOpt<double>(SUMO_ATTR_POSITION, id.c_str(), ok, 0);
122  const double lanePosLat = attrs.getOpt<double>(SUMO_ATTR_POSITION_LAT, id.c_str(), ok, 0);
123  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
124  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
125  const std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
126  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
127  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
128  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
129  bool relativePath = attrs.getOpt<bool>(SUMO_ATTR_RELATIVEPATH, id.c_str(), ok, Shape::DEFAULT_RELATIVEPATH);
130  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
132  }
133  const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
134  const double height = attrs.getOpt<double>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
135  if (!ok) {
136  return;
137  }
138  const GeoConvHelper* gch = (myGeoConvHelper != nullptr
140  : (useProcessing
143  if (useProcessing && gch->usingGeoProjection()) {
144  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
145  lon = x;
146  lat = y;
147  x = INVALID_POSITION;
148  }
149  }
150  Position pos(x, y);
151  bool useGeo = false;
152  if (x == INVALID_POSITION || y == INVALID_POSITION) {
153  // try computing x,y from lane,pos
154  if (laneID != "") {
155  pos = getLanePos(id, laneID, lanePos, lanePosLat);
156  } else {
157  // try computing x,y from lon,lat
158  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
159  WRITE_ERROR("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '" + id + "'.");
160  return;
161  } else if (!gch->usingGeoProjection()) {
162  WRITE_ERROR("(lon, lat) is specified for PoI '" + id + "' but no geo-conversion is specified for the network.");
163  return;
164  }
165  pos.set(lon, lat);
166  useGeo = true;
167  bool success = true;
168  if (useProcessing) {
169  success = GeoConvHelper::getProcessing().x2cartesian(pos);
170  } else {
171  success = gch->x2cartesian_const(pos);
172  }
173  if (!success) {
174  WRITE_ERROR("Unable to project coordinates for PoI '" + id + "'.");
175  return;
176  }
177  }
178  }
179  if (!myShapeContainer.addPOI(id, type, color, pos, useGeo, laneID, lanePos, lanePosLat, layer, angle, imgFile, relativePath, width, height, ignorePruning)) {
180  WRITE_ERROR("PoI '" + id + "' already exists.");
181  }
183  if (laneID != "" && addLanePosParams()) {
187  }
188 }
189 
190 
191 void
192 ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
193  bool ok = true;
194  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
195  // get the id, report an error if not given or empty...
196  if (!ok) {
197  return;
198  }
199  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
200  const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
201  const double lineWidth = attrs.getOpt<double>(SUMO_ATTR_LINEWIDTH, id.c_str(), ok, Shape::DEFAULT_LINEWIDTH);
202  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
203  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
204  PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
205  bool geo = false;
207  if (attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false)) {
208  geo = true;
209  bool success = true;
210  for (int i = 0; i < (int)shape.size(); i++) {
211  if (useProcessing) {
212  success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
213  } else {
214  success &= gch->x2cartesian_const(shape[i]);
215  }
216  }
217  if (!success) {
218  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
219  return;
220  }
221  }
222  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
223  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
224  bool relativePath = attrs.getOpt<bool>(SUMO_ATTR_RELATIVEPATH, id.c_str(), ok, Shape::DEFAULT_RELATIVEPATH);
225  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
227  }
228  // check that shape's size is valid
229  if (shape.size() == 0) {
230  WRITE_ERROR("Polygon's shape cannot be empty.");
231  return;
232  }
233  // check that lineWidth is positive
234  if (lineWidth <= 0) {
235  WRITE_ERROR("Polygon's lineWidth must be greather than 0.");
236  return;
237  }
238  // create polygon, or show an error if polygon already exists
239  if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, relativePath, shape, geo, fill, lineWidth, ignorePruning)) {
240  WRITE_ERROR("Polygon '" + id + "' already exists.");
241  }
243 }
244 
245 
248  return myLastParameterised;
249 }
250 
251 
252 bool
253 ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
254  for (auto fileIt : files) {
255  if (!XMLSubSys::runParser(sh, fileIt, false)) {
256  WRITE_MESSAGE("Loading of shapes from " + fileIt + " failed.");
257  return false;
258  }
259  }
260  return true;
261 }
262 
263 
264 void
265 ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const double layer, const bool fill) {
266  myPrefix = prefix;
267  myDefaultColor = color;
268  myDefaultLayer = layer;
269  myDefaultFill = fill;
270 }
271 
272 
273 bool
275  return false;
276 }
277 
278 /****************************************************************************/
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:382
SUMO_ATTR_ANGLE
Definition: SUMOXMLDefinitions.h:791
XMLSubSys::runParser
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:113
GUIGlObjectTypes.h
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SUMO_ATTR_IMGFILE
Definition: SUMOXMLDefinitions.h:789
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
ShapeHandler::~ShapeHandler
virtual ~ShapeHandler()
Destructor.
Definition: ShapeHandler.cpp:57
Parameterised
An upper class for objects with additional parameters.
Definition: Parameterised.h:43
ShapeHandler::myLastParameterised
Parameterised * myLastParameterised
element to receive parameters
Definition: ShapeHandler.h:127
SUMO_TAG_POLY
begin/end of the description of a polygon
Definition: SUMOXMLDefinitions.h:58
ShapeHandler::getLastParameterised
Parameterised * getLastParameterised() const
get last parameterised object
Definition: ShapeHandler.cpp:247
SUMOSAXAttributes::getString
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:42
GeoConvHelper::x2cartesian_const
bool x2cartesian_const(Position &from) const
Converts the given coordinate into a cartesian using the previous initialisation.
Definition: GeoConvHelper.cpp:418
GeomConvHelper.h
ShapeHandler::getLanePos
virtual Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos, double lanePosLat)=0
get position for a given laneID (Has to be implemented in all child)
SUMO_TAG_PARAM
parameter associated to a certain key
Definition: SUMOXMLDefinitions.h:170
SUMOSAXAttributes::get
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:493
MsgHandler.h
FileHelpers::getConfigurationRelative
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:106
SUMOSAXHandler.h
Shape::DEFAULT_LAYER
static const double DEFAULT_LAYER
Definition: Shape.h:44
ShapeHandler::addPoly
void addPoly(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a polygon
Definition: ShapeHandler.cpp:192
GeoConvHelper::getProcessing
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:87
ShapeHandler::myDefaultFill
bool myDefaultFill
Information whether polygons should be filled.
Definition: ShapeHandler.h:124
GeoConvHelper.h
ShapeContainer
Storage for geometrical objects.
Definition: ShapeContainer.h:50
SUMO_ATTR_COLOR
A color information.
Definition: SUMOXMLDefinitions.h:701
SUMO_TAG_POI
begin/end of the description of a Point of interest
Definition: SUMOXMLDefinitions.h:54
ShapeHandler::loadFiles
static bool loadFiles(const std::vector< std::string > &files, ShapeHandler &sh)
loads all of the given files
Definition: ShapeHandler.cpp:253
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:379
ShapeHandler::addLanePosParams
virtual bool addLanePosParams()
Whether some input attributes shall be automatically added as params (Can be implemented in all child...
Definition: ShapeHandler.cpp:274
ShapeHandler::myStartElement
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: ShapeHandler.cpp:61
SUMO_ATTR_LANE
Definition: SUMOXMLDefinitions.h:635
PositionVector
A list of positions.
Definition: PositionVector.h:46
GeoConvHelper
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:56
GeoConvHelper::x2cartesian
bool x2cartesian(Position &from, bool includeInBoundary=true)
Converts the given coordinate into a cartesian and optionally update myConvBoundary.
Definition: GeoConvHelper.cpp:327
GeoConvHelper::usingGeoProjection
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
Definition: GeoConvHelper.cpp:282
ShapeHandler::myGeoConvHelper
const GeoConvHelper * myGeoConvHelper
geo-conversion to use during loading
Definition: ShapeHandler.h:130
RGBColor.h
ShapeHandler::addPOI
void addPOI(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a POI
Definition: ShapeHandler.cpp:113
Shape::DEFAULT_IMG_HEIGHT
static const double DEFAULT_IMG_HEIGHT
Definition: Shape.h:51
INVALID_POSITION
#define INVALID_POSITION
Definition: MSDelayBasedTrafficLightLogic.cpp:35
Shape::DEFAULT_RELATIVEPATH
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:49
RGBColor
Definition: RGBColor.h:40
SUMO_ATTR_LINEWIDTH
Definition: SUMOXMLDefinitions.h:712
Shape.h
Shape::DEFAULT_LINEWIDTH
static const double DEFAULT_LINEWIDTH
Definition: Shape.h:45
ShapeHandler::setDefaults
void setDefaults(const std::string &prefix, const RGBColor &color, const double layer, const bool fill=false)
set default values
Definition: ShapeHandler.cpp:265
ShapeHandler::myDefaultColor
RGBColor myDefaultColor
The default color to use.
Definition: ShapeHandler.h:118
GeoConvHelper::getFinal
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
Definition: GeoConvHelper.h:106
ShapeHandler::myEndElement
virtual void myEndElement(int element)
Called when a closing tag occurs.
Definition: ShapeHandler.cpp:105
Position::set
void set(double x, double y)
set positions x and y
Definition: Position.h:87
ShapeHandler::myDefaultLayer
double myDefaultLayer
The default layer to use.
Definition: ShapeHandler.h:121
SUMO_ATTR_WIDTH
Definition: SUMOXMLDefinitions.h:387
ShapeHandler::ShapeHandler
ShapeHandler(const std::string &file, ShapeContainer &sc, const GeoConvHelper *=nullptr)
Constructor.
Definition: ShapeHandler.cpp:45
SUMO_ATTR_LAYER
A layer number.
Definition: SUMOXMLDefinitions.h:709
OutputDevice.h
Shape::DEFAULT_TYPE
static const std::string DEFAULT_TYPE
Definition: Shape.h:43
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
SUMOXMLDefinitions::isValidGenericParameterKey
static bool isValidGenericParameterKey(const std::string &value)
whether the given string is a valid key for a generic parameter
Definition: SUMOXMLDefinitions.cpp:1034
UtilExceptions.h
SUMO_ATTR_Y
Definition: SUMOXMLDefinitions.h:400
SUMOSAXAttributes::getOpt
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.
Definition: SUMOSAXAttributes.h:519
SUMO_ATTR_RELATIVEPATH
Definition: SUMOXMLDefinitions.h:790
SUMOXMLDefinitions::isValidGenericParameterValue
static bool isValidGenericParameterValue(const std::string &value)
whether the given string is a valid value for a generic parameter
Definition: SUMOXMLDefinitions.cpp:1041
SUMO_ATTR_POSITION
Definition: SUMOXMLDefinitions.h:658
SUMO_ATTR_FILL
Fill the polygon.
Definition: SUMOXMLDefinitions.h:711
ShapeContainer::getPOIs
const POIs & getPOIs() const
Returns all pois.
Definition: ShapeContainer.h:155
ShapeContainer::addPolygon
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, double layer, double angle, const std::string &imgFile, bool relativePath, const PositionVector &shape, bool geo, bool fill, double lineWidth, bool ignorePruning=false)
Builds a polygon using the given values and adds it to the container.
Definition: ShapeContainer.cpp:65
SUMO_ATTR_HEIGHT
Definition: SUMOXMLDefinitions.h:786
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
StringUtils.h
ShapeHandler::myShapeContainer
ShapeContainer & myShapeContainer
reference to shape container in which all Shares are being added
Definition: ShapeHandler.h:112
InvalidArgument
Definition: UtilExceptions.h:57
SUMO_ATTR_LAT
Definition: SUMOXMLDefinitions.h:812
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:75
SUMO_ATTR_KEY
Definition: SUMOXMLDefinitions.h:409
NamedObjectCont::get
T get(const std::string &id) const
Retrieves an item.
Definition: NamedObjectCont.h:99
SUMO_ATTR_VALUE
Definition: SUMOXMLDefinitions.h:776
SUMO_ATTR_POSITION_LAT
Definition: SUMOXMLDefinitions.h:659
Parameterised::setParameter
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
Definition: Parameterised.cpp:45
FileHelpers::isAbsolute
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Definition: FileHelpers.cpp:120
ShapeContainer::addPOI
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, const Position &pos, bool geo, const std::string &lane, double posOverLane, double posLat, double layer, double angle, const std::string &imgFile, bool relativePath, double width, double height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
Definition: ShapeContainer.cpp:146
Shape::DEFAULT_ANGLE
static const double DEFAULT_ANGLE
Definition: Shape.h:47
config.h
ShapeHandler
The XML-Handler for network loading.
Definition: ShapeHandler.h:50
ShapeContainer.h
StringTokenizer.h
ShapeHandler.h
ShapeContainer::getPolygons
const Polygons & getPolygons() const
Returns all polygons.
Definition: ShapeContainer.h:150
Shape::DEFAULT_IMG_FILE
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:48
SUMO_ATTR_LON
Definition: SUMOXMLDefinitions.h:811
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
SUMO_ATTR_SHAPE
edge: the shape in xml-definition
Definition: SUMOXMLDefinitions.h:687
SUMO_ATTR_X
Definition: SUMOXMLDefinitions.h:399
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
ShapeHandler::myPrefix
std::string myPrefix
The prefix to use.
Definition: ShapeHandler.h:115
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
Shape::DEFAULT_LAYER_POI
static const double DEFAULT_LAYER_POI
Definition: Shape.h:46
WRITE_DEBUG
#define WRITE_DEBUG(msg)
Definition: MsgHandler.h:246
SUMOXMLDefinitions.h
SUMO_ATTR_GEO
Definition: SUMOXMLDefinitions.h:813
XMLSubSys.h
Shape::DEFAULT_IMG_WIDTH
static const double DEFAULT_IMG_WIDTH
Definition: Shape.h:50