Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/od/ODDistrictHandler.cpp
169666 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 ODDistrictHandler.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Sept 2002
19
///
20
// An XML-Handler for districts
21
/****************************************************************************/
22
#include <config.h>
23
24
#include <string>
25
#include <utility>
26
#include <iostream>
27
#include <utils/common/UtilExceptions.h>
28
#include <utils/common/MsgHandler.h>
29
#include <utils/common/ToString.h>
30
#include <utils/xml/SUMOSAXHandler.h>
31
#include <utils/xml/SUMOXMLDefinitions.h>
32
#include "ODDistrict.h"
33
#include "ODDistrictCont.h"
34
#include "ODDistrictHandler.h"
35
36
37
// ===========================================================================
38
// method definitions
39
// ===========================================================================
40
ODDistrictHandler::ODDistrictHandler(ODDistrictCont& cont,
41
const std::string& file)
42
: SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(nullptr) {}
43
44
45
ODDistrictHandler::~ODDistrictHandler() {}
46
47
48
void
49
ODDistrictHandler::myStartElement(int element,
50
const SUMOSAXAttributes& attrs) {
51
switch (element) {
52
case SUMO_TAG_TAZ:
53
openDistrict(attrs);
54
break;
55
case SUMO_TAG_TAZSOURCE:
56
addSource(attrs);
57
break;
58
case SUMO_TAG_TAZSINK:
59
addSink(attrs);
60
break;
61
default:
62
break;
63
}
64
}
65
66
67
void
68
ODDistrictHandler::myEndElement(int element) {
69
if (element == SUMO_TAG_TAZ) {
70
closeDistrict();
71
}
72
}
73
74
75
void
76
ODDistrictHandler::openDistrict(const SUMOSAXAttributes& attrs) {
77
myCurrentDistrict = nullptr;
78
// get the id, report an error if not given or empty...
79
bool ok = true;
80
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
81
if (!ok) {
82
return;
83
}
84
myCurrentDistrict = new ODDistrict(id);
85
if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
86
const std::vector<std::string>& desc = attrs.get<std::vector<std::string> >(SUMO_ATTR_EDGES, id.c_str(), ok);
87
for (const std::string& eID : desc) {
88
myCurrentDistrict->addSource(eID, 1.);
89
myCurrentDistrict->addSink(eID, 1.);
90
}
91
}
92
}
93
94
95
void
96
ODDistrictHandler::addSource(const SUMOSAXAttributes& attrs) {
97
std::pair<std::string, double> vals = parseTAZ(attrs);
98
if (vals.second >= 0) {
99
myCurrentDistrict->addSource(vals.first, vals.second);
100
}
101
}
102
103
104
void
105
ODDistrictHandler::addSink(const SUMOSAXAttributes& attrs) {
106
std::pair<std::string, double> vals = parseTAZ(attrs);
107
if (vals.second >= 0) {
108
myCurrentDistrict->addSink(vals.first, vals.second);
109
}
110
}
111
112
113
114
std::pair<std::string, double>
115
ODDistrictHandler::parseTAZ(const SUMOSAXAttributes& attrs) {
116
// check the current district first
117
if (myCurrentDistrict == nullptr) {
118
return std::pair<std::string, double>("", -1);
119
}
120
// get the id, report an error if not given or empty...
121
bool ok = true;
122
std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
123
if (!ok) {
124
return std::pair<std::string, double>("", -1);
125
}
126
// get the weight
127
double weight = attrs.get<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
128
if (ok) {
129
if (weight < 0) {
130
WRITE_ERRORF(TL("'probability' must be positive (in definition of % '%')."), attrs.getObjectType(), id);
131
} else {
132
return std::pair<std::string, double>(id, weight);
133
}
134
}
135
return std::pair<std::string, double>("", -1);
136
}
137
138
139
void
140
ODDistrictHandler::closeDistrict() {
141
if (myCurrentDistrict != nullptr) {
142
myContainer.add(myCurrentDistrict->getID(), myCurrentDistrict);
143
}
144
}
145
146
147
/****************************************************************************/
148
149