Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/router/RONetHandler.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2002-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 RONetHandler.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Christian Roessel
18
/// @author Michael Behrisch
19
/// @author Yun-Pang Floetteroed
20
/// @date Sept 2002
21
///
22
// The handler for SUMO-Networks
23
/****************************************************************************/
24
#include <config.h>
25
26
#include <string>
27
#include <utils/common/MsgHandler.h>
28
#include <utils/common/StringTokenizer.h>
29
#include <utils/common/UtilExceptions.h>
30
#include <utils/common/ToString.h>
31
#include <utils/common/StringUtils.h>
32
#include <utils/geom/PositionVector.h>
33
#include <utils/geom/GeoConvHelper.h>
34
#include <utils/vehicle/SUMORouteHandler.h>
35
#include <utils/xml/SUMOSAXHandler.h>
36
#include <utils/xml/SUMOXMLDefinitions.h>
37
#include "ROEdge.h"
38
#include "ROLane.h"
39
#include "RONode.h"
40
#include "RONet.h"
41
#include "RONetHandler.h"
42
#include "ROAbstractEdgeBuilder.h"
43
44
45
// ===========================================================================
46
// method definitions
47
// ===========================================================================
48
RONetHandler::RONetHandler(RONet& net, ROAbstractEdgeBuilder& eb, const bool ignoreInternal, const double minorPenalty, double tlsPenalty, double turnaroundPenalty) :
49
SUMOSAXHandler("sumo-network"),
50
myNet(net),
51
myNetworkVersion(0, 0),
52
myEdgeBuilder(eb), myIgnoreInternal(ignoreInternal),
53
myCurrentName(), myCurrentEdge(nullptr), myCurrentStoppingPlace(nullptr),
54
myMinorPenalty(minorPenalty),
55
myTLSPenalty(tlsPenalty),
56
myTurnaroundPenalty(turnaroundPenalty)
57
{}
58
59
60
RONetHandler::~RONetHandler() {}
61
62
63
void
64
RONetHandler::myStartElement(int element,
65
const SUMOSAXAttributes& attrs) {
66
switch (element) {
67
case SUMO_TAG_LOCATION:
68
setLocation(attrs);
69
break;
70
case SUMO_TAG_NET: {
71
bool ok;
72
myNetworkVersion = StringUtils::toVersion(attrs.get<std::string>(SUMO_ATTR_VERSION, nullptr, ok, false));
73
break;
74
}
75
case SUMO_TAG_EDGE:
76
// in the first step, we do need the name to allocate the edge
77
// in the second, we need it to know to which edge we have to add
78
// the following edges to
79
parseEdge(attrs);
80
break;
81
case SUMO_TAG_LANE:
82
parseLane(attrs);
83
break;
84
case SUMO_TAG_JUNCTION:
85
parseJunction(attrs);
86
break;
87
case SUMO_TAG_CONNECTION:
88
parseConnection(attrs);
89
break;
90
case SUMO_TAG_BUS_STOP:
91
case SUMO_TAG_TRAIN_STOP:
92
case SUMO_TAG_CONTAINER_STOP:
93
case SUMO_TAG_PARKING_AREA:
94
case SUMO_TAG_CHARGING_STATION:
95
case SUMO_TAG_OVERHEAD_WIRE_SEGMENT:
96
parseStoppingPlace(attrs, (SumoXMLTag)element);
97
break;
98
case SUMO_TAG_ACCESS:
99
parseAccess(attrs);
100
break;
101
case SUMO_TAG_TAZ:
102
parseDistrict(attrs);
103
break;
104
case SUMO_TAG_TAZSOURCE:
105
parseDistrictEdge(attrs, true);
106
break;
107
case SUMO_TAG_TAZSINK:
108
parseDistrictEdge(attrs, false);
109
break;
110
case SUMO_TAG_TYPE: {
111
bool ok = true;
112
myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
113
break;
114
}
115
case SUMO_TAG_RESTRICTION: {
116
bool ok = true;
117
const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
118
const double speed = attrs.get<double>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
119
if (ok) {
120
myNet.addRestriction(myCurrentTypeID, svc, speed);
121
}
122
break;
123
}
124
case SUMO_TAG_PARAM:
125
addParam(attrs);
126
break;
127
default:
128
break;
129
}
130
}
131
132
133
void
134
RONetHandler::myEndElement(int element) {
135
switch (element) {
136
case SUMO_TAG_NET:
137
// build junction graph
138
for (std::set<std::string>::const_iterator it = myUnseenNodeIDs.begin(); it != myUnseenNodeIDs.end(); ++it) {
139
WRITE_ERRORF(TL("Unknown node '%'."), *it);
140
}
141
break;
142
default:
143
break;
144
}
145
}
146
147
148
void
149
RONetHandler::addParam(const SUMOSAXAttributes& attrs) {
150
bool ok = true;
151
const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
152
// circumventing empty string test
153
const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
154
// add parameter in current created element, or in myLoadedParameterised
155
if (myCurrentEdge != nullptr) {
156
myCurrentEdge->setParameter(key, val);
157
}
158
}
159
160
161
void
162
RONetHandler::parseEdge(const SUMOSAXAttributes& attrs) {
163
// get the id, report an error if not given or empty...
164
bool ok = true;
165
myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
166
if (!ok) {
167
throw ProcessError();
168
}
169
const SumoXMLEdgeFunc func = attrs.getOpt<SumoXMLEdgeFunc>(SUMO_ATTR_FUNCTION, myCurrentName.c_str(), ok, SumoXMLEdgeFunc::NORMAL);
170
if (!ok) {
171
return;
172
}
173
// get the edge
174
std::string from;
175
std::string to;
176
int priority;
177
myCurrentEdge = nullptr;
178
if (func == SumoXMLEdgeFunc::INTERNAL || func == SumoXMLEdgeFunc::CROSSING || func == SumoXMLEdgeFunc::WALKINGAREA) {
179
assert(myCurrentName[0] == ':');
180
const std::string junctionID = SUMOXMLDefinitions::getJunctionIDFromInternalEdge(myCurrentName);
181
from = junctionID;
182
to = junctionID;
183
priority = -1;
184
} else {
185
from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
186
to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
187
priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
188
if (!ok) {
189
return;
190
}
191
}
192
RONode* fromNode = myNet.getNode(from);
193
if (fromNode == nullptr) {
194
myUnseenNodeIDs.insert(from);
195
fromNode = new RONode(from);
196
myNet.addNode(fromNode);
197
}
198
RONode* toNode = myNet.getNode(to);
199
if (toNode == nullptr) {
200
myUnseenNodeIDs.insert(to);
201
toNode = new RONode(to);
202
myNet.addNode(toNode);
203
}
204
const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentName.c_str(), ok, "");
205
// build the edge
206
myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority, type);
207
myCurrentEdge->setFunction(func);
208
209
if (myNet.addEdge(myCurrentEdge)) {
210
fromNode->addOutgoing(myCurrentEdge);
211
toNode->addIncoming(myCurrentEdge);
212
const std::string bidi = attrs.getOpt<std::string>(SUMO_ATTR_BIDI, myCurrentName.c_str(), ok, "");
213
if (bidi != "") {
214
myBidiEdges[myCurrentEdge] = bidi;
215
}
216
} else {
217
myCurrentEdge = nullptr;
218
}
219
}
220
221
222
void
223
RONetHandler::parseLane(const SUMOSAXAttributes& attrs) {
224
if (myCurrentEdge == nullptr) {
225
// was an internal edge to skip or an error occurred
226
return;
227
}
228
bool ok = true;
229
// get the id, report an error if not given or empty...
230
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
231
if (!ok) {
232
return;
233
}
234
// get the speed
235
double maxSpeed = attrs.get<double>(SUMO_ATTR_SPEED, id.c_str(), ok);
236
double length = attrs.get<double>(SUMO_ATTR_LENGTH, id.c_str(), ok);
237
std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
238
std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
239
const PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
240
if (!ok) {
241
return;
242
}
243
if (shape.size() < 2) {
244
WRITE_ERRORF(TL("Ignoring lane '%' with broken shape."), id);
245
return;
246
}
247
// get the length
248
// get the vehicle classes
249
SVCPermissions permissions = parseVehicleClasses(allow, disallow, myNetworkVersion);
250
if (permissions != SVCAll) {
251
myNet.setPermissionsFound();
252
}
253
// add when both values are valid
254
if (maxSpeed > 0 && length > 0 && id.length() > 0) {
255
myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions, shape));
256
} else {
257
WRITE_WARNING("Ignoring lane '" + id + "' with speed " + toString(maxSpeed) + " and length " + toString(length));
258
}
259
}
260
261
262
void
263
RONetHandler::parseJunction(const SUMOSAXAttributes& attrs) {
264
bool ok = true;
265
// get the id, report an error if not given or empty...
266
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
267
const SumoXMLNodeType type = attrs.get<SumoXMLNodeType>(SUMO_ATTR_TYPE, id.c_str(), ok);
268
if (type == SumoXMLNodeType::INTERNAL) {
269
return;
270
}
271
myUnseenNodeIDs.erase(id);
272
// get the position of the node
273
const double x = attrs.get<double>(SUMO_ATTR_X, id.c_str(), ok);
274
const double y = attrs.get<double>(SUMO_ATTR_Y, id.c_str(), ok);
275
const double z = attrs.getOpt<double>(SUMO_ATTR_Z, id.c_str(), ok, 0.);
276
if (!ok) {
277
return;
278
}
279
RONode* n = myNet.getNode(id);
280
if (n == nullptr) {
281
WRITE_WARNINGF(TL("Skipping isolated junction '%'."), id);
282
} else {
283
n->setPosition(Position(x, y, z));
284
}
285
}
286
287
288
void
289
RONetHandler::parseConnection(const SUMOSAXAttributes& attrs) {
290
bool ok = true;
291
std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
292
std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
293
const int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, nullptr, ok);
294
const int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, nullptr, ok);
295
std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, nullptr, ok);
296
std::string viaID = attrs.getOpt<std::string>(SUMO_ATTR_VIA, nullptr, ok, "");
297
std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, nullptr, ok, "");
298
ROEdge* from = myNet.getEdge(fromID);
299
ROEdge* to = myNet.getEdge(toID);
300
if (from == nullptr) {
301
throw ProcessError(TLF("unknown from-edge '%' in connection", fromID));
302
}
303
if (to == nullptr) {
304
throw ProcessError(TLF("unknown to-edge '%' in connection", toID));
305
}
306
if ((int)from->getLanes().size() <= fromLane) {
307
throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
308
}
309
if ((int)to->getLanes().size() <= toLane) {
310
throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
311
}
312
if (myIgnoreInternal || viaID == "") {
313
std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, nullptr, ok, "");
314
std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, nullptr, ok, "");
315
ROEdge* dummyVia = nullptr;
316
SVCPermissions permissions;
317
if (allow == "" && disallow == "") {
318
permissions = SVC_UNSPECIFIED;
319
} else {
320
myNet.setPermissionsFound();
321
// dummyVia is only needed to hold permissions
322
permissions = parseVehicleClasses(allow, disallow);
323
dummyVia = new ROEdge("dummyVia_" + from->getLanes()[fromLane]->getID() + "->" + to->getLanes()[toLane]->getID(),
324
from->getToJunction(), from->getToJunction(), permissions);
325
}
326
from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane], dummyVia);
327
from->addSuccessor(to, nullptr, dir);
328
} else {
329
ROEdge* const via = myNet.getEdge(SUMOXMLDefinitions::getEdgeIDFromLane(viaID));
330
if (via == nullptr) {
331
throw ProcessError(TLF("unknown via-edge '%' in connection", viaID));
332
}
333
from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane], via);
334
from->addSuccessor(to, via, dir);
335
via->addSuccessor(to, nullptr, dir);
336
LinkState state = SUMOXMLDefinitions::LinkStates.get(attrs.get<std::string>(SUMO_ATTR_STATE, nullptr, ok));
337
if (state == LINKSTATE_MINOR || state == LINKSTATE_EQUAL || state == LINKSTATE_STOP || state == LINKSTATE_ALLWAY_STOP) {
338
via->setTimePenalty(myMinorPenalty);
339
}
340
if (dir == toString(LinkDirection::TURN) || dir == toString(LinkDirection::TURN_LEFTHAND)) {
341
via->setTimePenalty(myTurnaroundPenalty);
342
}
343
if (tlID != "") {
344
via->setTimePenalty(myTLSPenalty);
345
}
346
}
347
if (to->isCrossing()) {
348
to->setTimePenalty(myTLSPenalty);
349
}
350
}
351
352
353
void
354
RONetHandler::parseStoppingPlace(const SUMOSAXAttributes& attrs, const SumoXMLTag element) {
355
bool ok = true;
356
myCurrentStoppingPlace = new SUMOVehicleParameter::Stop();
357
// get the id, throw if not given or empty...
358
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, toString(element).c_str(), ok);
359
// get the lane
360
myCurrentStoppingPlace->lane = attrs.get<std::string>(SUMO_ATTR_LANE, toString(element).c_str(), ok);
361
if (!ok) {
362
throw ProcessError();
363
}
364
const ROEdge* edge = myNet.getEdgeForLaneID(myCurrentStoppingPlace->lane);
365
if (edge == nullptr) {
366
throw InvalidArgument("Unknown lane '" + myCurrentStoppingPlace->lane + "' for " + toString(element) + " '" + id + "'.");
367
}
368
// get the positions
369
myCurrentStoppingPlace->startPos = attrs.getOpt<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0.);
370
myCurrentStoppingPlace->endPos = attrs.getOpt<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
371
const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
372
if (!ok || (SUMORouteHandler::checkStopPos(myCurrentStoppingPlace->startPos, myCurrentStoppingPlace->endPos, edge->getLength(), POSITION_EPS, friendlyPos) != SUMORouteHandler::StopPos::STOPPOS_VALID)) {
373
throw InvalidArgument("Invalid position for " + toString(element) + " '" + id + "'.");
374
}
375
// this is a hack: the busstop attribute is meant to hold the id within the simulation context but this is not used within the router context
376
myCurrentStoppingPlace->busstop = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, "");
377
// this is a hack: the actType is not used when using this to encode a stopping place
378
myCurrentStoppingPlace->actType = toString(element);
379
myNet.addStoppingPlace(id, element, myCurrentStoppingPlace);
380
}
381
382
383
void
384
RONetHandler::parseAccess(const SUMOSAXAttributes& attrs) {
385
bool ok = true;
386
const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
387
const ROEdge* edge = myNet.getEdgeForLaneID(lane);
388
if (edge == nullptr) {
389
throw InvalidArgument("Unknown lane '" + lane + "' for access.");
390
}
391
if ((edge->getPermissions() & SVC_PEDESTRIAN) == 0) {
392
WRITE_WARNINGF(TL("Ignoring invalid access from non-pedestrian edge '%'."), edge->getID());
393
return;
394
}
395
const bool random = attrs.getOpt<std::string>(SUMO_ATTR_POSITION, "access", ok) == "random";
396
double pos = random ? edge->getLength() / 2. : attrs.getOpt<double>(SUMO_ATTR_POSITION, "access", ok, 0.);
397
double length = attrs.getOpt<double>(SUMO_ATTR_LENGTH, "access", ok, -1);
398
const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, "access", ok, false);
399
if (!ok || (SUMORouteHandler::checkStopPos(pos, pos, edge->getLength(), 0., friendlyPos) != SUMORouteHandler::StopPos::STOPPOS_VALID)) {
400
throw InvalidArgument("Invalid position " + toString(pos) + " for access on lane '" + lane + "'.");
401
}
402
if (!ok) {
403
throw ProcessError();
404
}
405
if (length < 0) {
406
const Position accPos = myNet.getLane(lane)->geometryPositionAtOffset(pos);
407
const double stopCenter = (myCurrentStoppingPlace->startPos + myCurrentStoppingPlace->endPos) / 2;
408
const Position stopPos = myNet.getLane(myCurrentStoppingPlace->lane)->geometryPositionAtOffset(stopCenter);
409
length = accPos.distanceTo(stopPos);
410
}
411
myCurrentStoppingPlace->accessPos.push_back(std::make_tuple(lane, pos, length));
412
}
413
414
415
void
416
RONetHandler::parseDistrict(const SUMOSAXAttributes& attrs) {
417
myCurrentEdge = nullptr;
418
bool ok = true;
419
myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
420
if (!ok) {
421
return;
422
}
423
ROEdge* const sink = myEdgeBuilder.buildEdge(myCurrentName + "-sink", nullptr, nullptr, 0, "");
424
ROEdge* const source = myEdgeBuilder.buildEdge(myCurrentName + "-source", nullptr, nullptr, 0, "");
425
myNet.addDistrict(myCurrentName, source, sink);
426
if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
427
const std::vector<std::string>& desc = attrs.get<std::vector<std::string> >(SUMO_ATTR_EDGES, myCurrentName.c_str(), ok);
428
for (const std::string& eID : desc) {
429
myNet.addDistrictEdge(myCurrentName, eID, true);
430
myNet.addDistrictEdge(myCurrentName, eID, false);
431
}
432
}
433
}
434
435
436
void
437
RONetHandler::parseDistrictEdge(const SUMOSAXAttributes& attrs, bool isSource) {
438
bool ok = true;
439
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
440
myNet.addDistrictEdge(myCurrentName, id, isSource);
441
}
442
443
void
444
RONetHandler::setLocation(const SUMOSAXAttributes& attrs) {
445
bool ok = true;
446
PositionVector s = attrs.get<PositionVector>(SUMO_ATTR_NET_OFFSET, nullptr, ok);
447
Boundary convBoundary = attrs.get<Boundary>(SUMO_ATTR_CONV_BOUNDARY, nullptr, ok);
448
Boundary origBoundary = attrs.get<Boundary>(SUMO_ATTR_ORIG_BOUNDARY, nullptr, ok);
449
std::string proj = attrs.get<std::string>(SUMO_ATTR_ORIG_PROJ, nullptr, ok);
450
if (ok) {
451
Position networkOffset = s[0];
452
GeoConvHelper::init(proj, networkOffset, origBoundary, convBoundary);
453
}
454
}
455
456
457
/****************************************************************************/
458
459