Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/shapes/ShapeHandler.cpp
169678 views
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
/****************************************************************************/
14
/// @file ShapeHandler.cpp
15
/// @author Jakob Erdmann
16
/// @date Feb 2015
17
///
18
// The XML-Handler for network loading
19
/****************************************************************************/
20
#include <config.h>
21
22
#include <string>
23
#include <utils/xml/SUMOXMLDefinitions.h>
24
#include <utils/xml/SUMOSAXHandler.h>
25
#include <utils/xml/XMLSubSys.h>
26
#include <utils/common/MsgHandler.h>
27
#include <utils/common/StringUtils.h>
28
#include <utils/common/StringTokenizer.h>
29
#include <utils/common/RGBColor.h>
30
#include <utils/geom/GeomConvHelper.h>
31
#include <utils/iodevices/OutputDevice.h>
32
#include <utils/common/UtilExceptions.h>
33
#include <utils/geom/GeoConvHelper.h>
34
#include <utils/gui/globjects/GUIGlObjectTypes.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
myDefaultIcon(SUMOXMLDefinitions::POIIcons.getString(POIIcon::NONE)),
51
myDefaultLayer(0),
52
myDefaultFill(false),
53
myLastParameterised(nullptr),
54
myGeoConvHelper(geoConvHelper) {
55
}
56
57
58
ShapeHandler::~ShapeHandler() {}
59
60
61
void
62
ShapeHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
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
67
myDefaultLayer = Shape::DEFAULT_LAYER;
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
72
myDefaultLayer = Shape::DEFAULT_LAYER_POI;
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"));
86
} else if (!SUMOXMLDefinitions::isValidParameterKey(key)) {
87
WRITE_WARNING(TL("Error parsing key from shape generic parameter. Key contains invalid characters"));
88
} else {
89
myLastParameterised->setParameter(key, val);
90
}
91
}
92
}
93
break;
94
default:
95
break;
96
}
97
} catch (InvalidArgument& e) {
98
WRITE_ERROR(e.what());
99
}
100
}
101
102
103
void
104
ShapeHandler::myEndElement(int element) {
105
if (element != SUMO_TAG_PARAM) {
106
myLastParameterised = nullptr;
107
}
108
}
109
110
111
void
112
ShapeHandler::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)) {
138
imgFile = FileHelpers::getConfigurationRelative(getFileName(), 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
143
if (!SUMOXMLDefinitions::isValidTypeID(id)) {
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) {
154
gch = &GeoConvHelper::getProcessing();
155
} else {
156
gch = &GeoConvHelper::getFinal();
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;
163
x = INVALID_POSITION;
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) {
185
success = GeoConvHelper::getProcessing().x2cartesian(pos);
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
}
202
myLastParameterised = myShapeContainer.getPOIs().get(id);
203
if ((laneID != "") && addLanePosParams()) {
204
myLastParameterised->setParameter(toString(SUMO_ATTR_LANE), laneID);
205
myLastParameterised->setParameter(toString(SUMO_ATTR_POSITION), toString(lanePos));
206
myLastParameterised->setParameter(toString(SUMO_ATTR_POSITION_LAT), toString(lanePosLat));
207
}
208
}
209
}
210
211
212
void
213
ShapeHandler::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
217
if (!SUMOXMLDefinitions::isValidTypeID(id)) {
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 {
236
gch = &GeoConvHelper::getFinal();
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)) {
256
imgFile = FileHelpers::getConfigurationRelative(getFileName(), 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
}
272
myLastParameterised = myShapeContainer.getPolygons().get(id);
273
}
274
}
275
276
277
Parameterised*
278
ShapeHandler::getLastParameterised() const {
279
return myLastParameterised;
280
}
281
282
283
bool
284
ShapeHandler::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
295
void
296
ShapeHandler::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
305
bool
306
ShapeHandler::addLanePosParams() {
307
return false;
308
}
309
310
311
/****************************************************************************/
312
313