Line data Source code
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 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 <utils/options/OptionsCont.h>
34 : #include <netbuild/NBEdge.h>
35 : #include <netbuild/NBTypeCont.h>
36 : #include "NIXMLTypesHandler.h"
37 :
38 :
39 : // ===========================================================================
40 : // method definitions
41 : // ===========================================================================
42 4009 : NIXMLTypesHandler::NIXMLTypesHandler(NBTypeCont& tc)
43 : : SUMOSAXHandler("xml-types - file"),
44 8018 : myTypeCont(tc) {}
45 :
46 :
47 4009 : NIXMLTypesHandler::~NIXMLTypesHandler() {}
48 :
49 :
50 : void
51 10127 : NIXMLTypesHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
52 10127 : switch (element) {
53 9773 : case SUMO_TAG_TYPE: {
54 9773 : bool ok = true;
55 : // get the id, report a warning if not given or empty...
56 19546 : myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
57 : const char* const id = myCurrentTypeID.c_str();
58 9773 : const std::string defType = myTypeCont.knows(myCurrentTypeID) ? myCurrentTypeID : "";
59 9773 : const int priority = attrs.getOpt<int>(SUMO_ATTR_PRIORITY, id, ok, myTypeCont.getEdgeTypePriority(defType));
60 9773 : const int numLanes = attrs.getOpt<int>(SUMO_ATTR_NUMLANES, id, ok, myTypeCont.getEdgeTypeNumLanes(defType));
61 9773 : const double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, id, ok, myTypeCont.getEdgeTypeSpeed(defType));
62 9773 : const std::string allowS = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id, ok, "");
63 9773 : const std::string disallowS = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id, ok, "");
64 9773 : const std::string spreadTypeS = attrs.getOpt<std::string>(SUMO_ATTR_SPREADTYPE, id, ok, OptionsCont::getOptions().getString("default.spreadtype"));
65 9773 : const bool oneway = attrs.getOpt<bool>(SUMO_ATTR_ONEWAY, id, ok, myTypeCont.getEdgeTypeIsOneWay(defType));
66 9773 : const bool discard = attrs.getOpt<bool>(SUMO_ATTR_DISCARD, id, ok, false);
67 9773 : const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id, ok, myTypeCont.getEdgeTypeWidth(defType));
68 9773 : const double maxWidth = attrs.getOpt<double>(SUMO_ATTR_MAXWIDTH, id, ok, myTypeCont.getEdgeTypeMaxWidth(defType));
69 9773 : const double minWidth = attrs.getOpt<double>(SUMO_ATTR_MINWIDTH, id, ok, myTypeCont.getEdgeTypeMinWidth(defType));
70 9773 : const double widthResolution = attrs.getOpt<double>(SUMO_ATTR_WIDTHRESOLUTION, id, ok, myTypeCont.getEdgeTypeWidthResolution(defType));
71 9773 : const double sidewalkWidth = attrs.getOpt<double>(SUMO_ATTR_SIDEWALKWIDTH, id, ok, myTypeCont.getEdgeTypeSidewalkWidth(defType));
72 9773 : const double bikeLaneWidth = attrs.getOpt<double>(SUMO_ATTR_BIKELANEWIDTH, id, ok, myTypeCont.getEdgeTypeBikeLaneWidth(defType));
73 : // continue if parsing parameter was ok
74 9773 : if (ok) {
75 : // build the type
76 9772 : SVCPermissions permissions = myTypeCont.getEdgeTypePermissions(defType);
77 9772 : if (allowS != "" || disallowS != "") {
78 9655 : permissions = parseVehicleClasses(allowS, disallowS);
79 : }
80 : // get spreadType
81 : LaneSpreadFunction spreadType = LaneSpreadFunction::RIGHT;
82 : // check if spreadType is valid
83 : if (SUMOXMLDefinitions::LaneSpreadFunctions.hasString(spreadTypeS)) {
84 9771 : spreadType = SUMOXMLDefinitions::LaneSpreadFunctions.get(spreadTypeS);
85 : } else {
86 3 : WRITE_ERRORF(TL("Invalid lane spread type '%'. Using default 'right'"), spreadTypeS);
87 : }
88 : // insert edgeType in container
89 9772 : myTypeCont.insertEdgeType(myCurrentTypeID, numLanes, speed, priority, permissions, spreadType, width,
90 : oneway, sidewalkWidth, bikeLaneWidth, widthResolution, maxWidth, minWidth);
91 : // check if mark edgeType as discard
92 9772 : if (discard) {
93 523 : myTypeCont.markEdgeTypeAsToDiscard(myCurrentTypeID);
94 : }
95 : // mark attributes as set
96 9772 : SumoXMLAttr myAttrs[] = {SUMO_ATTR_PRIORITY, SUMO_ATTR_NUMLANES, SUMO_ATTR_SPEED, SUMO_ATTR_ALLOW,
97 : SUMO_ATTR_DISALLOW, SUMO_ATTR_SPREADTYPE, SUMO_ATTR_ONEWAY, SUMO_ATTR_DISCARD,
98 : SUMO_ATTR_WIDTH, SUMO_ATTR_SIDEWALKWIDTH, SUMO_ATTR_BIKELANEWIDTH
99 : };
100 117264 : for (const auto& attr : myAttrs) {
101 107492 : if (attrs.hasAttribute(attr)) {
102 49161 : myTypeCont.markEdgeTypeAsSet(myCurrentTypeID, attr);
103 : }
104 : }
105 : }
106 : break;
107 : }
108 60 : case SUMO_TAG_LANETYPE: {
109 60 : bool ok = true;
110 : // use id of last inserted edge
111 : const char* const edgeTypeId = myCurrentTypeID.c_str();
112 60 : const int index = attrs.get<int>(SUMO_ATTR_INDEX, edgeTypeId, ok);
113 60 : const std::string defType = myTypeCont.knows(myCurrentTypeID) ? myCurrentTypeID : "";
114 60 : if (index >= myTypeCont.getEdgeTypeNumLanes(defType)) {
115 3 : WRITE_ERRORF(TL("Invalid lane index % for edge type '%' with % lanes"), toString(index), defType, toString(myTypeCont.getEdgeTypeNumLanes(defType)));
116 1 : ok = false;
117 : }
118 60 : const double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, edgeTypeId, ok, myTypeCont.getEdgeTypeSpeed(edgeTypeId));
119 60 : const std::string allowS = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, edgeTypeId, ok, "");
120 60 : const std::string disallowS = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, edgeTypeId, ok, "");
121 60 : const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, edgeTypeId, ok, myTypeCont.getEdgeTypeWidth(defType));
122 : // continue if parsing parameter was ok
123 60 : if (ok) {
124 : // build the type
125 59 : SVCPermissions permissions = myTypeCont.getEdgeTypePermissions(defType);
126 59 : if (allowS != "" || disallowS != "") {
127 18 : permissions = parseVehicleClasses(allowS, disallowS);
128 : }
129 :
130 : // insert laneType in container
131 59 : myTypeCont.insertLaneType(myCurrentTypeID, index, speed, permissions, width, {});
132 : // mark attributes as set
133 59 : SumoXMLAttr myAttrs[] = {SUMO_ATTR_SPEED, SUMO_ATTR_ALLOW, SUMO_ATTR_DISALLOW, SUMO_ATTR_WIDTH};
134 295 : for (const auto& attr : myAttrs) {
135 236 : if (attrs.hasAttribute(attr)) {
136 100 : myTypeCont.markLaneTypeAsSet(myCurrentTypeID, index, attr);
137 : }
138 : }
139 : }
140 : break;
141 : }
142 2 : case SUMO_TAG_RESTRICTION: {
143 2 : bool ok = true;
144 4 : const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
145 2 : const double speed = attrs.get<double>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
146 2 : if (ok) {
147 2 : myTypeCont.addEdgeTypeRestriction(myCurrentTypeID, svc, speed);
148 : }
149 : break;
150 : }
151 : default:
152 : break;
153 : }
154 10127 : }
155 :
156 :
157 : /****************************************************************************/
|