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-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// The XML-Handler for network loading
19/****************************************************************************/
20#include <config.h>
21
22#include <string>
25#include <utils/xml/XMLSubSys.h>
35
36#include "Shape.h"
37#include "ShapeContainer.h"
38#include "ShapeHandler.h"
39
40
41// ===========================================================================
42// method definitions
43// ===========================================================================
44
45ShapeHandler::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
60void
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 was successfully 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(TL("Error parsing key from shape generic parameter. Key cannot be empty"));
86 WRITE_WARNING(TL("Error parsing key from shape generic parameter. Key contains invalid characters"));
87 } else {
88 WRITE_DEBUG("Inserting generic parameter '" + key + "|" + val + "' into shape.");
90 }
91 }
92 }
93 break;
94 default:
95 break;
96 }
97 } catch (InvalidArgument& e) {
98 WRITE_ERROR(e.what());
99 }
100}
101
102
103void
105 if (element != SUMO_TAG_PARAM) {
106 myLastParameterised = nullptr;
107 }
108}
109
110
111void
112ShapeHandler::addPOI(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
113 bool ok = true;
114 const double INVALID_POSITION(-1000000);
115 const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
116 double x = attrs.getOpt<double>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
117 const double y = attrs.getOpt<double>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
118 double lon = attrs.getOpt<double>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
119 double lat = attrs.getOpt<double>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
120 const double lanePos = attrs.getOpt<double>(SUMO_ATTR_POSITION, id.c_str(), ok, 0);
121 const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
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 double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
127 std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
128 const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : (imgFile != "" ? RGBColor::WHITE : myDefaultColor);
129 bool relativePath = attrs.getOpt<bool>(SUMO_ATTR_RELATIVEPATH, id.c_str(), ok, Shape::DEFAULT_RELATIVEPATH);
130
131 // If the image file is set, change the default POI color to white.
132 if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
134 }
135 const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
136 const double height = attrs.getOpt<double>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
137 // check if ID is valid
138 if (SUMOXMLDefinitions::isValidTypeID(id) == false) {
139 WRITE_WARNING(TL("Invalid characters for PoI ID"));
140 ok = false;
141 }
142 // continue
143 if (ok) {
144 const GeoConvHelper* gch;
145 // set GEOConverter
146 if (myGeoConvHelper != nullptr) {
147 gch = myGeoConvHelper;
148 } else if (useProcessing) {
150 } else {
152 }
153 // check if GEOProjection has to be used
154 if (useProcessing && gch->usingGeoProjection()) {
155 if ((lat == INVALID_POSITION) || (lon == INVALID_POSITION)) {
156 lon = x;
157 lat = y;
159 }
160 }
161 Position pos(x, y);
162 bool useGeo = false;
163 if ((x == INVALID_POSITION) || (y == INVALID_POSITION)) {
164 // try computing x,y from lane,pos
165 if (laneID != "") {
166 pos = getLanePos(id, laneID, lanePos, friendlyPos, lanePosLat);
167 } else {
168 // try computing x,y from lon,lat
169 if ((lat == INVALID_POSITION) || (lon == INVALID_POSITION)) {
170 WRITE_ERROR("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '" + id + "'.");
171 return;
172 } else if (!gch->usingGeoProjection()) {
173 WRITE_ERROR("(lon, lat) is specified for PoI '" + id + "' but no geo-conversion is specified for the network.");
174 return;
175 }
176 pos.set(lon, lat);
177 useGeo = true;
178 bool success = true;
179 if (useProcessing) {
181 } else {
182 success = gch->x2cartesian_const(pos);
183 }
184 if (!success) {
185 WRITE_ERROR("Unable to project coordinates for PoI '" + id + "'.");
186 return;
187 }
188 }
189 }
190 if (!myShapeContainer.addPOI(id, type, color, pos, useGeo, laneID, lanePos, friendlyPos, lanePosLat, layer, angle, imgFile, relativePath, width, height, ignorePruning)) {
191 WRITE_ERROR("PoI '" + id + "' already exists.");
192 }
194 if ((laneID != "") && addLanePosParams()) {
198 }
199 }
200}
201
202
203void
204ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
205 bool ok = true;
206 const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
207 // check if ID is valid
208 if (SUMOXMLDefinitions::isValidTypeID(id) == false) {
209 WRITE_WARNING(TL("Invalid characters for Poly ID"));
210 ok = false;
211 }
212 // get the id, report an error if not given or empty...
213 if (ok) {
214 // continue loading parameters
215 const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
216 const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
217 const double lineWidth = attrs.getOpt<double>(SUMO_ATTR_LINEWIDTH, id.c_str(), ok, Shape::DEFAULT_LINEWIDTH);
218 const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
219 const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
220 PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
221 const bool geo = attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false);
222 // set geo converter
223 const GeoConvHelper* gch;
224 if (myGeoConvHelper != nullptr) {
225 gch = myGeoConvHelper;
226 } else {
228 }
229 // check if poly use geo coordinates
230 if (geo || useProcessing) {
231 bool success = true;
232 for (int i = 0; i < (int)shape.size(); i++) {
233 if (useProcessing) {
234 success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
235 } else {
236 success &= gch->x2cartesian_const(shape[i]);
237 }
238 }
239 if (!success) {
240 WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
241 return;
242 }
243 }
244 const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
245 std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
246 bool relativePath = attrs.getOpt<bool>(SUMO_ATTR_RELATIVEPATH, id.c_str(), ok, Shape::DEFAULT_RELATIVEPATH);
247 if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
249 }
250 // check that shape's size is valid
251 if (shape.size() == 0) {
252 WRITE_ERROR(TL("Polygon's shape cannot be empty."));
253 return;
254 }
255 // check that lineWidth is positive
256 if (lineWidth <= 0) {
257 WRITE_ERROR(TL("Polygon's lineWidth must be greater than 0."));
258 return;
259 }
260 // create polygon, or show an error if polygon already exists
261 if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, relativePath, shape, geo, fill, lineWidth, ignorePruning)) {
262 WRITE_ERROR("Polygon '" + id + "' already exists.");
263 }
265 }
266}
267
268
271 return myLastParameterised;
272}
273
274
275bool
276ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
277 for (const auto& fileIt : files) {
278 if (!XMLSubSys::runParser(sh, fileIt, false)) {
279 WRITE_MESSAGE("Loading of shapes from " + fileIt + " failed.");
280 return false;
281 }
282 }
283 return true;
284}
285
286
287void
288ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const double layer, const bool fill) {
289 myPrefix = prefix;
290 myDefaultColor = color;
291 myDefaultLayer = layer;
292 myDefaultFill = fill;
293}
294
295
296bool
298 return false;
299}
300
301
302/****************************************************************************/
#define INVALID_POSITION
#define WRITE_DEBUG(msg)
Definition: MsgHandler.h:276
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:267
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
#define TL(string)
Definition: MsgHandler.h:282
@ SUMO_TAG_POI
begin/end of the description of a Point of interest
@ SUMO_TAG_POLY
begin/end of the description of a polygon
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_ATTR_LANE
@ SUMO_ATTR_LON
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_Y
@ SUMO_ATTR_X
@ SUMO_ATTR_LINEWIDTH
@ SUMO_ATTR_POSITION_LAT
@ SUMO_ATTR_GEO
@ SUMO_ATTR_SHAPE
edge: the shape in xml-definition
@ SUMO_ATTR_FILL
Fill the polygon.
@ SUMO_ATTR_LAYER
A layer number.
@ SUMO_ATTR_ANGLE
@ SUMO_ATTR_HEIGHT
@ SUMO_ATTR_FRIENDLY_POS
@ SUMO_ATTR_LAT
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_COLOR
A color information.
@ SUMO_ATTR_ID
@ SUMO_ATTR_IMGFILE
@ SUMO_ATTR_WIDTH
@ SUMO_ATTR_KEY
@ SUMO_ATTR_POSITION
@ SUMO_ATTR_RELATIVEPATH
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
const std::string & getFileName() const
returns the current file name
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:53
bool x2cartesian(Position &from, bool includeInBoundary=true)
Converts the given coordinate into a cartesian and optionally update myConvBoundary.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:84
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
bool x2cartesian_const(Position &from) const
Converts the given coordinate into a cartesian using the previous initialisation.
T get(const std::string &id) const
Retrieves an item.
An upper class for objects with additional parameters.
Definition: Parameterised.h:41
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
void set(double x, double y)
set positions x and y
Definition: Position.h:85
A list of positions.
static const RGBColor WHITE
Definition: RGBColor.h:192
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), 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.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAX-handler base for SUMO-files.
static bool isValidTypeID(const std::string &value)
whether the given string is a valid id for an edge or vehicle type
static bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter
Storage for geometrical objects.
const Polygons & getPolygons() const
Returns all polygons.
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, const std::string &name=Shape::DEFAULT_NAME)
Builds a polygon using the given values and adds it to the container.
const POIs & getPOIs() const
Returns all pois.
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, bool friendlyPos, 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.
The XML-Handler for network loading.
Definition: ShapeHandler.h:47
virtual bool addLanePosParams()
Whether some input attributes shall be automatically added as params (Can be implemented in all child...
std::string myPrefix
The prefix to use.
Definition: ShapeHandler.h:112
virtual void myEndElement(int element)
Called when a closing tag occurs.
void addPOI(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a POI
void addPoly(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a polygon
RGBColor myDefaultColor
The default color to use.
Definition: ShapeHandler.h:115
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
ShapeContainer & myShapeContainer
reference to shape container in which all Shares are being added
Definition: ShapeHandler.h:109
Parameterised * myLastParameterised
element to receive parameters
Definition: ShapeHandler.h:124
static bool loadFiles(const std::vector< std::string > &files, ShapeHandler &sh)
loads all of the given files
virtual Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos, bool friendlyPos, double lanePosLat)=0
get position for a given laneID (Has to be implemented in all child)
bool myDefaultFill
Information whether polygons should be filled.
Definition: ShapeHandler.h:121
virtual ~ShapeHandler()
Destructor.
void setDefaults(const std::string &prefix, const RGBColor &color, const double layer, const bool fill=false)
set default values
ShapeHandler(const std::string &file, ShapeContainer &sc, const GeoConvHelper *=nullptr)
Constructor.
Parameterised * getLastParameterised() const
get last parameterised object
const GeoConvHelper * myGeoConvHelper
geo-conversion to use during loading
Definition: ShapeHandler.h:127
double myDefaultLayer
The default layer to use.
Definition: ShapeHandler.h:118
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:48
static const double DEFAULT_LAYER
Definition: Shape.h:43
static const double DEFAULT_LAYER_POI
Definition: Shape.h:45
static const double DEFAULT_IMG_WIDTH
Definition: Shape.h:49
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:47
static const double DEFAULT_LINEWIDTH
Definition: Shape.h:44
static const double DEFAULT_ANGLE
Definition: Shape.h:46
static const double DEFAULT_IMG_HEIGHT
Definition: Shape.h:50
static const std::string DEFAULT_TYPE
Definition: Shape.h:42
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:137