Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-2024 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 9967 : RONetHandler::RONetHandler(RONet& net, ROAbstractEdgeBuilder& eb, const bool ignoreInternal, const double minorPenalty, double tlsPenalty, double turnaroundPenalty) :
49 : SUMOSAXHandler("sumo-network"),
50 9967 : myNet(net),
51 : myNetworkVersion(0, 0),
52 9967 : myEdgeBuilder(eb), myIgnoreInternal(ignoreInternal),
53 9967 : myCurrentName(), myCurrentEdge(nullptr), myCurrentStoppingPlace(nullptr),
54 9967 : myMinorPenalty(minorPenalty),
55 9967 : myTLSPenalty(tlsPenalty),
56 19934 : myTurnaroundPenalty(turnaroundPenalty)
57 9967 : {}
58 :
59 :
60 19934 : RONetHandler::~RONetHandler() {}
61 :
62 :
63 : void
64 2334581 : RONetHandler::myStartElement(int element,
65 : const SUMOSAXAttributes& attrs) {
66 2334581 : switch (element) {
67 9967 : case SUMO_TAG_LOCATION:
68 9967 : setLocation(attrs);
69 9967 : break;
70 9967 : case SUMO_TAG_NET: {
71 : bool ok;
72 19934 : myNetworkVersion = StringUtils::toVersion(attrs.get<std::string>(SUMO_ATTR_VERSION, nullptr, ok, false));
73 : break;
74 : }
75 446332 : 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 446332 : parseEdge(attrs);
80 446320 : break;
81 595889 : case SUMO_TAG_LANE:
82 595889 : parseLane(attrs);
83 595889 : break;
84 147707 : case SUMO_TAG_JUNCTION:
85 147707 : parseJunction(attrs);
86 147707 : break;
87 710747 : case SUMO_TAG_CONNECTION:
88 710747 : parseConnection(attrs);
89 710429 : break;
90 1590 : 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 1590 : parseStoppingPlace(attrs, (SumoXMLTag)element);
97 1590 : break;
98 1088 : case SUMO_TAG_ACCESS:
99 1088 : parseAccess(attrs);
100 1088 : break;
101 12631 : case SUMO_TAG_TAZ:
102 12631 : parseDistrict(attrs);
103 12631 : break;
104 116 : case SUMO_TAG_TAZSOURCE:
105 116 : parseDistrictEdge(attrs, true);
106 116 : break;
107 117 : case SUMO_TAG_TAZSINK:
108 117 : parseDistrictEdge(attrs, false);
109 117 : break;
110 1616 : case SUMO_TAG_TYPE: {
111 1616 : bool ok = true;
112 3232 : myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
113 : break;
114 : }
115 1 : case SUMO_TAG_RESTRICTION: {
116 1 : bool ok = true;
117 2 : const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
118 1 : const double speed = attrs.get<double>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
119 1 : if (ok) {
120 1 : myNet.addRestriction(myCurrentTypeID, svc, speed);
121 : }
122 : break;
123 : }
124 52052 : case SUMO_TAG_PARAM:
125 52052 : addParam(attrs);
126 52052 : break;
127 : default:
128 : break;
129 : }
130 2334251 : }
131 :
132 :
133 : void
134 2333486 : RONetHandler::myEndElement(int element) {
135 2333486 : switch (element) {
136 9382 : case SUMO_TAG_NET:
137 : // build junction graph
138 9460 : for (std::set<std::string>::const_iterator it = myUnseenNodeIDs.begin(); it != myUnseenNodeIDs.end(); ++it) {
139 234 : WRITE_ERRORF(TL("Unknown node '%'."), *it);
140 : }
141 : break;
142 : default:
143 : break;
144 : }
145 2333486 : }
146 :
147 :
148 : void
149 52052 : RONetHandler::addParam(const SUMOSAXAttributes& attrs) {
150 52052 : bool ok = true;
151 52052 : const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
152 : // circumventing empty string test
153 52052 : 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 52052 : if (myCurrentEdge != nullptr) {
156 52052 : myCurrentEdge->setParameter(key, val);
157 : }
158 52052 : }
159 :
160 :
161 : void
162 446332 : RONetHandler::parseEdge(const SUMOSAXAttributes& attrs) {
163 : // get the id, report an error if not given or empty...
164 446332 : bool ok = true;
165 446332 : myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
166 446332 : if (!ok) {
167 12 : throw ProcessError();
168 : }
169 446320 : const SumoXMLEdgeFunc func = attrs.getOpt<SumoXMLEdgeFunc>(SUMO_ATTR_FUNCTION, myCurrentName.c_str(), ok, SumoXMLEdgeFunc::NORMAL);
170 446320 : if (!ok) {
171 54 : return;
172 : }
173 : // get the edge
174 : std::string from;
175 : std::string to;
176 : int priority;
177 446314 : myCurrentEdge = nullptr;
178 446314 : if (func == SumoXMLEdgeFunc::INTERNAL || func == SumoXMLEdgeFunc::CROSSING || func == SumoXMLEdgeFunc::WALKINGAREA) {
179 : assert(myCurrentName[0] == ':');
180 593768 : const std::string junctionID = SUMOXMLDefinitions::getJunctionIDFromInternalEdge(myCurrentName);
181 : from = junctionID;
182 : to = junctionID;
183 : priority = -1;
184 296884 : } else {
185 298860 : from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
186 298860 : to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
187 149430 : priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
188 149430 : if (!ok) {
189 : return;
190 : }
191 : }
192 446266 : RONode* fromNode = myNet.getNode(from);
193 358801 : if (fromNode == nullptr) {
194 : myUnseenNodeIDs.insert(from);
195 87465 : fromNode = new RONode(from);
196 87465 : myNet.addNode(fromNode);
197 : }
198 446266 : RONode* toNode = myNet.getNode(to);
199 427873 : if (toNode == nullptr) {
200 : myUnseenNodeIDs.insert(to);
201 18393 : toNode = new RONode(to);
202 18393 : myNet.addNode(toNode);
203 : }
204 : // build the edge
205 446266 : myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
206 : // set the type
207 446266 : myCurrentEdge->setRestrictions(myNet.getRestrictions(attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentName.c_str(), ok, "")));
208 446266 : myCurrentEdge->setFunction(func);
209 :
210 446266 : if (myNet.addEdge(myCurrentEdge)) {
211 446254 : fromNode->addOutgoing(myCurrentEdge);
212 446254 : toNode->addIncoming(myCurrentEdge);
213 892508 : const std::string bidi = attrs.getOpt<std::string>(SUMO_ATTR_BIDI, myCurrentName.c_str(), ok, "");
214 446254 : if (bidi != "") {
215 6444 : myBidiEdges[myCurrentEdge] = bidi;
216 : }
217 : } else {
218 12 : myCurrentEdge = nullptr;
219 : }
220 : }
221 :
222 :
223 : void
224 595889 : RONetHandler::parseLane(const SUMOSAXAttributes& attrs) {
225 595889 : if (myCurrentEdge == nullptr) {
226 : // was an internal edge to skip or an error occurred
227 282 : return;
228 : }
229 595763 : bool ok = true;
230 : // get the id, report an error if not given or empty...
231 595763 : std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
232 595763 : if (!ok) {
233 : return;
234 : }
235 : // get the speed
236 595739 : double maxSpeed = attrs.get<double>(SUMO_ATTR_SPEED, id.c_str(), ok);
237 595739 : double length = attrs.get<double>(SUMO_ATTR_LENGTH, id.c_str(), ok);
238 595739 : std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
239 1191478 : std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
240 595739 : const PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
241 595739 : if (!ok) {
242 : return;
243 : }
244 595631 : if (shape.size() < 2) {
245 72 : WRITE_ERRORF(TL("Ignoring lane '%' with broken shape."), id);
246 24 : return;
247 : }
248 : // get the length
249 : // get the vehicle classes
250 595607 : SVCPermissions permissions = parseVehicleClasses(allow, disallow, myNetworkVersion);
251 595607 : if (permissions != SVCAll) {
252 134306 : myNet.setPermissionsFound();
253 : }
254 : // add when both values are valid
255 595607 : if (maxSpeed > 0 && length > 0 && id.length() > 0) {
256 595607 : myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions, shape));
257 : } else {
258 0 : WRITE_WARNING("Ignoring lane '" + id + "' with speed " + toString(maxSpeed) + " and length " + toString(length));
259 : }
260 595739 : }
261 :
262 :
263 : void
264 147707 : RONetHandler::parseJunction(const SUMOSAXAttributes& attrs) {
265 147707 : bool ok = true;
266 : // get the id, report an error if not given or empty...
267 147707 : std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
268 147707 : const SumoXMLNodeType type = attrs.get<SumoXMLNodeType>(SUMO_ATTR_TYPE, id.c_str(), ok);
269 147707 : if (type == SumoXMLNodeType::INTERNAL) {
270 : return;
271 : }
272 : myUnseenNodeIDs.erase(id);
273 : // get the position of the node
274 105350 : const double x = attrs.get<double>(SUMO_ATTR_X, id.c_str(), ok);
275 105350 : const double y = attrs.get<double>(SUMO_ATTR_Y, id.c_str(), ok);
276 105350 : const double z = attrs.getOpt<double>(SUMO_ATTR_Z, id.c_str(), ok, 0.);
277 105350 : if (!ok) {
278 : return;
279 : }
280 105305 : RONode* n = myNet.getNode(id);
281 105201 : if (n == nullptr) {
282 312 : WRITE_WARNINGF(TL("Skipping isolated junction '%'."), id);
283 : } else {
284 105201 : n->setPosition(Position(x, y, z));
285 : }
286 : }
287 :
288 :
289 : void
290 710747 : RONetHandler::parseConnection(const SUMOSAXAttributes& attrs) {
291 710747 : bool ok = true;
292 710747 : std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
293 710747 : std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
294 710747 : const int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, nullptr, ok);
295 710747 : const int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, nullptr, ok);
296 710747 : std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, nullptr, ok);
297 711065 : std::string viaID = attrs.getOpt<std::string>(SUMO_ATTR_VIA, nullptr, ok, "");
298 711065 : std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, nullptr, ok, "");
299 710747 : ROEdge* from = myNet.getEdge(fromID);
300 : ROEdge* to = myNet.getEdge(toID);
301 710747 : if (from == nullptr) {
302 207 : throw ProcessError(TLF("unknown from-edge '%' in connection", fromID));
303 : }
304 710678 : if (to == nullptr) {
305 207 : throw ProcessError(TLF("unknown to-edge '%' in connection", toID));
306 : }
307 710609 : if ((int)from->getLanes().size() <= fromLane) {
308 180 : throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
309 : }
310 710519 : if ((int)to->getLanes().size() <= toLane) {
311 180 : throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
312 : }
313 710429 : if (myIgnoreInternal || viaID == "") {
314 390325 : from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane]);
315 1170975 : from->addSuccessor(to, nullptr, dir);
316 390325 : if (to->isCrossing()) {
317 6224 : to->setTimePenalty(myTLSPenalty);
318 : }
319 : } else {
320 320104 : ROEdge* const via = myNet.getEdge(SUMOXMLDefinitions::getEdgeIDFromLane(viaID));
321 320104 : if (via == nullptr) {
322 0 : throw ProcessError(TLF("unknown via-edge '%' in connection", viaID));
323 : }
324 320104 : from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane], via);
325 640208 : from->addSuccessor(to, via, dir);
326 640208 : via->addSuccessor(to, nullptr, dir);
327 320104 : LinkState state = SUMOXMLDefinitions::LinkStates.get(attrs.get<std::string>(SUMO_ATTR_STATE, nullptr, ok));
328 320104 : if (state == LINKSTATE_MINOR || state == LINKSTATE_EQUAL || state == LINKSTATE_STOP || state == LINKSTATE_ALLWAY_STOP) {
329 157927 : via->setTimePenalty(myMinorPenalty);
330 : }
331 568780 : if (dir == toString(LinkDirection::TURN) || dir == toString(LinkDirection::TURN_LEFTHAND)) {
332 71746 : via->setTimePenalty(myTurnaroundPenalty);
333 : }
334 320104 : if (tlID != "") {
335 7440 : via->setTimePenalty(myTLSPenalty);
336 7440 : if (to->isCrossing()) {
337 : to->setTimePenalty(myTLSPenalty);
338 : }
339 : }
340 : }
341 710429 : }
342 :
343 :
344 : void
345 1590 : RONetHandler::parseStoppingPlace(const SUMOSAXAttributes& attrs, const SumoXMLTag element) {
346 1590 : bool ok = true;
347 1590 : myCurrentStoppingPlace = new SUMOVehicleParameter::Stop();
348 : // get the id, throw if not given or empty...
349 1590 : std::string id = attrs.get<std::string>(SUMO_ATTR_ID, toString(element).c_str(), ok);
350 : // get the lane
351 1590 : myCurrentStoppingPlace->lane = attrs.get<std::string>(SUMO_ATTR_LANE, toString(element).c_str(), ok);
352 1590 : if (!ok) {
353 0 : throw ProcessError();
354 : }
355 1590 : const ROEdge* edge = myNet.getEdgeForLaneID(myCurrentStoppingPlace->lane);
356 1590 : if (edge == nullptr) {
357 0 : throw InvalidArgument("Unknown lane '" + myCurrentStoppingPlace->lane + "' for " + toString(element) + " '" + id + "'.");
358 : }
359 : // get the positions
360 1590 : myCurrentStoppingPlace->startPos = attrs.getOpt<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0.);
361 1590 : myCurrentStoppingPlace->endPos = attrs.getOpt<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
362 1590 : const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
363 1590 : if (!ok || (SUMORouteHandler::checkStopPos(myCurrentStoppingPlace->startPos, myCurrentStoppingPlace->endPos, edge->getLength(), POSITION_EPS, friendlyPos) != SUMORouteHandler::StopPos::STOPPOS_VALID)) {
364 0 : throw InvalidArgument("Invalid position for " + toString(element) + " '" + id + "'.");
365 : }
366 : // 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
367 1590 : myCurrentStoppingPlace->busstop = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, "");
368 : // this is a hack: the actType is not used when using this to encode a stopping place
369 1590 : myCurrentStoppingPlace->actType = toString(element);
370 1590 : myNet.addStoppingPlace(id, element, myCurrentStoppingPlace);
371 1590 : }
372 :
373 :
374 : void
375 1088 : RONetHandler::parseAccess(const SUMOSAXAttributes& attrs) {
376 1088 : bool ok = true;
377 1088 : const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
378 1088 : const ROEdge* edge = myNet.getEdgeForLaneID(lane);
379 1088 : if (edge == nullptr) {
380 0 : throw InvalidArgument("Unknown lane '" + lane + "' for access.");
381 : }
382 1088 : if ((edge->getPermissions() & SVC_PEDESTRIAN) == 0) {
383 0 : WRITE_WARNINGF(TL("Ignoring invalid access from non-pedestrian edge '%'."), edge->getID());
384 : return;
385 : }
386 1088 : const bool random = attrs.getOpt<std::string>(SUMO_ATTR_POSITION, "access", ok) == "random";
387 1088 : double pos = random ? edge->getLength() / 2. : attrs.getOpt<double>(SUMO_ATTR_POSITION, "access", ok, 0.);
388 1088 : double length = attrs.getOpt<double>(SUMO_ATTR_LENGTH, "access", ok, -1);
389 1088 : const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, "access", ok, false);
390 1088 : if (!ok || (SUMORouteHandler::checkStopPos(pos, pos, edge->getLength(), 0., friendlyPos) != SUMORouteHandler::StopPos::STOPPOS_VALID)) {
391 0 : throw InvalidArgument("Invalid position " + toString(pos) + " for access on lane '" + lane + "'.");
392 : }
393 : if (!ok) {
394 : throw ProcessError();
395 : }
396 1088 : if (length < 0) {
397 568 : const Position accPos = myNet.getLane(lane)->geometryPositionAtOffset(pos);
398 568 : const double stopCenter = (myCurrentStoppingPlace->startPos + myCurrentStoppingPlace->endPos) / 2;
399 568 : const Position stopPos = myNet.getLane(myCurrentStoppingPlace->lane)->geometryPositionAtOffset(stopCenter);
400 568 : length = accPos.distanceTo(stopPos);
401 : }
402 2176 : myCurrentStoppingPlace->accessPos.push_back(std::make_tuple(lane, pos, length));
403 : }
404 :
405 :
406 : void
407 12631 : RONetHandler::parseDistrict(const SUMOSAXAttributes& attrs) {
408 12631 : myCurrentEdge = nullptr;
409 12631 : bool ok = true;
410 12631 : myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
411 12631 : if (!ok) {
412 0 : return;
413 : }
414 12631 : ROEdge* const sink = myEdgeBuilder.buildEdge(myCurrentName + "-sink", nullptr, nullptr, 0);
415 12631 : ROEdge* const source = myEdgeBuilder.buildEdge(myCurrentName + "-source", nullptr, nullptr, 0);
416 12631 : myNet.addDistrict(myCurrentName, source, sink);
417 12631 : if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
418 12502 : const std::vector<std::string>& desc = attrs.get<std::vector<std::string> >(SUMO_ATTR_EDGES, myCurrentName.c_str(), ok);
419 31434 : for (const std::string& eID : desc) {
420 56796 : myNet.addDistrictEdge(myCurrentName, eID, true);
421 56796 : myNet.addDistrictEdge(myCurrentName, eID, false);
422 : }
423 12502 : }
424 : }
425 :
426 :
427 : void
428 233 : RONetHandler::parseDistrictEdge(const SUMOSAXAttributes& attrs, bool isSource) {
429 233 : bool ok = true;
430 233 : std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
431 699 : myNet.addDistrictEdge(myCurrentName, id, isSource);
432 233 : }
433 :
434 : void
435 9967 : RONetHandler::setLocation(const SUMOSAXAttributes& attrs) {
436 9967 : bool ok = true;
437 9967 : PositionVector s = attrs.get<PositionVector>(SUMO_ATTR_NET_OFFSET, nullptr, ok);
438 9967 : Boundary convBoundary = attrs.get<Boundary>(SUMO_ATTR_CONV_BOUNDARY, nullptr, ok);
439 9967 : Boundary origBoundary = attrs.get<Boundary>(SUMO_ATTR_ORIG_BOUNDARY, nullptr, ok);
440 9967 : std::string proj = attrs.get<std::string>(SUMO_ATTR_ORIG_PROJ, nullptr, ok);
441 9967 : if (ok) {
442 9967 : Position networkOffset = s[0];
443 9967 : GeoConvHelper::init(proj, networkOffset, origBoundary, convBoundary);
444 : }
445 9967 : }
446 :
447 :
448 : /****************************************************************************/
|