Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ShapeHandler.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 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 myDefaultIcon(SUMOXMLDefinitions::POIIcons.getString(POIIcon::NONE)),
51 myDefaultLayer(0),
52 myDefaultFill(false),
53 myLastParameterised(nullptr),
54 myGeoConvHelper(geoConvHelper) {
55}
56
57
59
60
61void
63 try {
64 switch (element) {
65 case SUMO_TAG_POLY:
66 // default layer is different depending if we're parsing a Poly or a POI, therefore it has to be here defined
68 addPoly(attrs, false, false);
69 break;
70 case SUMO_TAG_POI:
71 // default layer is different depending if we're parsing a Poly or a POI, therefore it has to be here defined
73 addPOI(attrs, false, false);
74 break;
75 case SUMO_TAG_PARAM:
76 if (myLastParameterised != nullptr) {
77 bool ok = true;
78 const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
79 // continue if key was successfully loaded
80 if (ok) {
81 // circumventing empty string value
82 const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
83 // show warnings if values are invalid
84 if (key.empty()) {
85 WRITE_WARNING(TL("Error parsing key from shape generic parameter. Key cannot be empty"));
87 WRITE_WARNING(TL("Error parsing key from shape generic parameter. Key contains invalid characters"));
88 } else {
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 const double z = attrs.getOpt<double>(SUMO_ATTR_Z, 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 bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
123 const double lanePosLat = attrs.getOpt<double>(SUMO_ATTR_POSITION_LAT, id.c_str(), ok, 0);
124 std::string icon = attrs.getOpt<std::string>(SUMO_ATTR_ICON, id.c_str(), ok, myDefaultIcon);
125 // check icon
126 if (!SUMOXMLDefinitions::POIIcons.hasString(icon)) {
127 WRITE_WARNING(TLF("Invalid icon % for POI '%', using default", icon, id));
128 icon = "none";
129 }
130 const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
131 const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
132 const std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
133 const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
134 std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
135 const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : (imgFile != "" ? RGBColor::WHITE : myDefaultColor);
136 // If the image file is set, change the default POI color to white.
137 if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
139 }
140 const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
141 const double height = attrs.getOpt<double>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
142 // check if ID is valid
144 WRITE_WARNINGF(TL("Invalid characters for PoI ID '%'"), id);
145 ok = false;
146 }
147 // continue
148 if (ok) {
149 const GeoConvHelper* gch;
150 // set GEOConverter
151 if (myGeoConvHelper != nullptr) {
152 gch = myGeoConvHelper;
153 } else if (useProcessing) {
155 } else {
157 }
158 // check if GEOProjection has to be used
159 if (useProcessing && gch->usingGeoProjection()) {
160 if ((lat == INVALID_POSITION) || (lon == INVALID_POSITION)) {
161 lon = x;
162 lat = y;
164 }
165 }
166 Position pos(x, y);
167 bool useGeo = false;
168 if ((x == INVALID_POSITION) || (y == INVALID_POSITION)) {
169 // try computing x,y from lane,pos
170 if (laneID != "") {
171 pos = getLanePos(id, laneID, lanePos, friendlyPos, lanePosLat);
172 } else {
173 // try computing x,y from lon,lat
174 if ((lat == INVALID_POSITION) || (lon == INVALID_POSITION)) {
175 WRITE_ERRORF(TL("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '%'."), id);
176 return;
177 } else if (!gch->usingGeoProjection()) {
178 WRITE_ERRORF(TL("(lon, lat) is specified for PoI '%' but no geo-conversion is specified for the network."), id);
179 return;
180 }
181 pos.set(lon, lat);
182 useGeo = true;
183 bool success = true;
184 if (useProcessing) {
186 } else {
187 success = gch->x2cartesian_const(pos);
188 }
189 if (!success) {
190 WRITE_ERRORF(TL("Unable to project coordinates for POI '%'."), id);
191 return;
192 }
193 }
194 }
195 if (z != INVALID_POSITION) {
196 pos.setz(z);
197 }
198 if (!myShapeContainer.addPOI(id, type, color, pos, useGeo, laneID, lanePos, friendlyPos, lanePosLat, icon,
199 layer, angle, imgFile, width, height, ignorePruning)) {
200 WRITE_ERRORF(TL("PoI '%' already exists."), id);
201 }
203 if ((laneID != "") && addLanePosParams()) {
207 }
208 }
209}
210
211
212void
213ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
214 bool ok = true;
215 const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
216 // check if ID is valid
218 WRITE_WARNINGF(TL("Invalid characters for Poly ID '%'"), id);
219 ok = false;
220 }
221 // get the id, report an error if not given or empty...
222 if (ok) {
223 // continue loading parameters
224 const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
225 const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
226 const double lineWidth = attrs.getOpt<double>(SUMO_ATTR_LINEWIDTH, id.c_str(), ok, Shape::DEFAULT_LINEWIDTH);
227 const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
228 const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
229 PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
230 const bool geo = attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false);
231 // set geo converter
232 const GeoConvHelper* gch;
233 if (myGeoConvHelper != nullptr) {
234 gch = myGeoConvHelper;
235 } else {
237 }
238 // check if poly use geo coordinates
239 if (geo || useProcessing) {
240 bool success = true;
241 for (int i = 0; i < (int)shape.size(); i++) {
242 if (useProcessing) {
243 success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
244 } else {
245 success &= gch->x2cartesian_const(shape[i]);
246 }
247 }
248 if (!success) {
249 WRITE_WARNINGF(TL("Unable to project coordinates for polygon '%'."), id);
250 return;
251 }
252 }
253 const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
254 std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
255 if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
257 }
258 // check that shape's size is valid
259 if (shape.size() == 0) {
260 WRITE_ERROR(TL("Polygon's shape cannot be empty."));
261 return;
262 }
263 // check that lineWidth is positive
264 if (lineWidth <= 0) {
265 WRITE_ERROR(TL("Polygon's lineWidth must be greater than 0."));
266 return;
267 }
268 // create polygon, or show an error if polygon already exists
269 if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, shape, geo, fill, lineWidth, ignorePruning)) {
270 WRITE_ERRORF(TL("Polygon '%' already exists."), id);
271 }
273 }
274}
275
276
281
282
283bool
284ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
285 for (const auto& fileIt : files) {
286 if (!XMLSubSys::runParser(sh, fileIt, false)) {
287 WRITE_MESSAGEF(TL("Loading of shapes from % failed."), fileIt);
288 return false;
289 }
290 }
291 return true;
292}
293
294
295void
296ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const std::string& icon, const double layer, const bool fill) {
297 myPrefix = prefix;
298 myDefaultColor = color;
299 myDefaultIcon = icon;
300 myDefaultLayer = layer;
301 myDefaultFill = fill;
302}
303
304
305bool
307 return false;
308}
309
310
311/****************************************************************************/
#define INVALID_POSITION
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:288
#define WRITE_MESSAGEF(...)
Definition MsgHandler.h:290
#define WRITE_ERRORF(...)
Definition MsgHandler.h:297
#define WRITE_ERROR(msg)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:305
#define TLF(string,...)
Definition MsgHandler.h:307
POIIcon
POI icons.
@ 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_ICON
icon
@ SUMO_ATTR_LANE
@ SUMO_ATTR_LON
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_Y
@ SUMO_ATTR_Z
@ 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
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
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
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.
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:82
void setz(double z)
set position z
Definition Position.h:77
A list of positions.
static const RGBColor WHITE
Definition RGBColor.h:195
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.
class for maintaining associations between enums and xml-strings
static bool isValidTypeID(const std::string &value)
whether the given string is a valid id for an edge or vehicle type
static StringBijection< POIIcon > POIIcons
POI icon values.
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.
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, const std::string &icon, double layer, double angle, const std::string &imgFile, double width, double height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, double layer, double angle, const std::string &imgFile, 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.
The XML-Handler for network loading.
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.
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.
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
Parameterised * myLastParameterised
element to receive parameters
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)
void setDefaults(const std::string &prefix, const RGBColor &color, const std::string &icon, const double layer, const bool fill=false)
set default values
bool myDefaultFill
Information whether polygons should be filled.
virtual ~ShapeHandler()
Destructor.
ShapeHandler(const std::string &file, ShapeContainer &sc, const GeoConvHelper *=nullptr)
Constructor.
Parameterised * getLastParameterised() const
get last parameterised object
std::string myDefaultIcon
The default icon to use.
const GeoConvHelper * myGeoConvHelper
geo-conversion to use during loading
double myDefaultLayer
The default layer to use.
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:48
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:49
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, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.