Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-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 NIXMLTypesHandler.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Walter Bamberger
19 : /// @date Tue, 20 Nov 2001
20 : ///
21 : // Importer for edge type information stored in XML
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <string>
26 : #include <iostream>
27 : #include <utils/xml/SUMOSAXHandler.h>
28 : #include <utils/xml/SUMOXMLDefinitions.h>
29 : #include <utils/common/StringUtils.h>
30 : #include <utils/common/MsgHandler.h>
31 : #include <utils/common/ToString.h>
32 : #include <utils/common/SUMOVehicleClass.h>
33 : #include <netbuild/NBEdge.h>
34 : #include <netbuild/NBTypeCont.h>
35 : #include "NIXMLTypesHandler.h"
36 :
37 :
38 : // ===========================================================================
39 : // method definitions
40 : // ===========================================================================
41 3771 : NIXMLTypesHandler::NIXMLTypesHandler(NBTypeCont& tc)
42 : : SUMOSAXHandler("xml-types - file"),
43 7542 : myTypeCont(tc) {}
44 :
45 :
46 3771 : NIXMLTypesHandler::~NIXMLTypesHandler() {}
47 :
48 :
49 : void
50 8805 : NIXMLTypesHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
51 8805 : switch (element) {
52 8472 : case SUMO_TAG_TYPE: {
53 8472 : bool ok = true;
54 : // get the id, report a warning if not given or empty...
55 16944 : myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
56 : const char* const id = myCurrentTypeID.c_str();
57 8472 : const std::string defType = myTypeCont.knows(myCurrentTypeID) ? myCurrentTypeID : "";
58 8472 : const int priority = attrs.getOpt<int>(SUMO_ATTR_PRIORITY, id, ok, myTypeCont.getEdgeTypePriority(defType));
59 8472 : const int numLanes = attrs.getOpt<int>(SUMO_ATTR_NUMLANES, id, ok, myTypeCont.getEdgeTypeNumLanes(defType));
60 8472 : const double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, id, ok, myTypeCont.getEdgeTypeSpeed(defType));
61 8472 : const std::string allowS = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id, ok, "");
62 8472 : const std::string disallowS = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id, ok, "");
63 8472 : const std::string spreadTypeS = attrs.getOpt<std::string>(SUMO_ATTR_SPREADTYPE, id, ok, "right");
64 8472 : const bool oneway = attrs.getOpt<bool>(SUMO_ATTR_ONEWAY, id, ok, myTypeCont.getEdgeTypeIsOneWay(defType));
65 8472 : const bool discard = attrs.getOpt<bool>(SUMO_ATTR_DISCARD, id, ok, false);
66 8472 : const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id, ok, myTypeCont.getEdgeTypeWidth(defType));
67 8472 : const double maxWidth = attrs.getOpt<double>(SUMO_ATTR_MAXWIDTH, id, ok, myTypeCont.getEdgeTypeMaxWidth(defType));
68 8472 : const double minWidth = attrs.getOpt<double>(SUMO_ATTR_MINWIDTH, id, ok, myTypeCont.getEdgeTypeMinWidth(defType));
69 8472 : const double widthResolution = attrs.getOpt<double>(SUMO_ATTR_WIDTHRESOLUTION, id, ok, myTypeCont.getEdgeTypeWidthResolution(defType));
70 8472 : const double sidewalkWidth = attrs.getOpt<double>(SUMO_ATTR_SIDEWALKWIDTH, id, ok, myTypeCont.getEdgeTypeSidewalkWidth(defType));
71 8472 : const double bikeLaneWidth = attrs.getOpt<double>(SUMO_ATTR_BIKELANEWIDTH, id, ok, myTypeCont.getEdgeTypeBikeLaneWidth(defType));
72 : // continue if parsing parameter was ok
73 8472 : if (ok) {
74 : // build the type
75 8471 : SVCPermissions permissions = myTypeCont.getEdgeTypePermissions(defType);
76 8471 : if (allowS != "" || disallowS != "") {
77 8355 : permissions = parseVehicleClasses(allowS, disallowS);
78 : }
79 : // get spreadType
80 : LaneSpreadFunction spreadType = LaneSpreadFunction::RIGHT;
81 : // check if spreadType is valid
82 : if (SUMOXMLDefinitions::LaneSpreadFunctions.hasString(spreadTypeS)) {
83 8470 : spreadType = SUMOXMLDefinitions::LaneSpreadFunctions.get(spreadTypeS);
84 : } else {
85 3 : WRITE_ERRORF(TL("Invalid lane spread type '%'. Using default 'right'"), spreadTypeS);
86 : }
87 : // insert edgeType in container
88 8471 : myTypeCont.insertEdgeType(myCurrentTypeID, numLanes, speed, priority, permissions, spreadType, width,
89 : oneway, sidewalkWidth, bikeLaneWidth, widthResolution, maxWidth, minWidth);
90 : // check if mark edgeType as discard
91 8471 : if (discard) {
92 486 : myTypeCont.markEdgeTypeAsToDiscard(myCurrentTypeID);
93 : }
94 : // mark attributes as set
95 8471 : SumoXMLAttr myAttrs[] = {SUMO_ATTR_PRIORITY, SUMO_ATTR_NUMLANES, SUMO_ATTR_SPEED, SUMO_ATTR_ALLOW,
96 : SUMO_ATTR_DISALLOW, SUMO_ATTR_SPREADTYPE, SUMO_ATTR_ONEWAY, SUMO_ATTR_DISCARD,
97 : SUMO_ATTR_WIDTH, SUMO_ATTR_SIDEWALKWIDTH, SUMO_ATTR_BIKELANEWIDTH
98 : };
99 101652 : for (const auto& attr : myAttrs) {
100 93181 : if (attrs.hasAttribute(attr)) {
101 42591 : myTypeCont.markEdgeTypeAsSet(myCurrentTypeID, attr);
102 : }
103 : }
104 : }
105 : break;
106 : }
107 60 : case SUMO_TAG_LANETYPE: {
108 60 : bool ok = true;
109 : // use id of last inserted edge
110 : const char* const edgeTypeId = myCurrentTypeID.c_str();
111 60 : const int index = attrs.get<int>(SUMO_ATTR_INDEX, edgeTypeId, ok);
112 60 : const std::string defType = myTypeCont.knows(myCurrentTypeID) ? myCurrentTypeID : "";
113 60 : if (index >= myTypeCont.getEdgeTypeNumLanes(defType)) {
114 3 : WRITE_ERRORF(TL("Invalid lane index % for edge type '%' with % lanes"), toString(index), defType, toString(myTypeCont.getEdgeTypeNumLanes(defType)));
115 1 : ok = false;
116 : }
117 60 : const double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, edgeTypeId, ok, myTypeCont.getEdgeTypeSpeed(edgeTypeId));
118 60 : const std::string allowS = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, edgeTypeId, ok, "");
119 60 : const std::string disallowS = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, edgeTypeId, ok, "");
120 60 : const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, edgeTypeId, ok, myTypeCont.getEdgeTypeWidth(defType));
121 : // continue if parsing parameter was ok
122 60 : if (ok) {
123 : // build the type
124 59 : SVCPermissions permissions = myTypeCont.getEdgeTypePermissions(defType);
125 59 : if (allowS != "" || disallowS != "") {
126 18 : permissions = parseVehicleClasses(allowS, disallowS);
127 : }
128 :
129 : // insert laneType in container
130 59 : myTypeCont.insertLaneType(myCurrentTypeID, index, speed, permissions, width, {});
131 : // mark attributes as set
132 59 : SumoXMLAttr myAttrs[] = {SUMO_ATTR_SPEED, SUMO_ATTR_ALLOW, SUMO_ATTR_DISALLOW, SUMO_ATTR_WIDTH};
133 295 : for (const auto& attr : myAttrs) {
134 236 : if (attrs.hasAttribute(attr)) {
135 100 : myTypeCont.markLaneTypeAsSet(myCurrentTypeID, index, attr);
136 : }
137 : }
138 : }
139 : break;
140 : }
141 2 : case SUMO_TAG_RESTRICTION: {
142 2 : bool ok = true;
143 4 : const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
144 2 : const double speed = attrs.get<double>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
145 2 : if (ok) {
146 2 : myTypeCont.addEdgeTypeRestriction(myCurrentTypeID, svc, speed);
147 : }
148 : break;
149 : }
150 : default:
151 : break;
152 : }
153 8805 : }
154 :
155 :
156 : /****************************************************************************/
|