Eclipse SUMO - Simulation of Urban MObility
ODDistrictHandler.cpp
Go to the documentation of this file.
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 /****************************************************************************/
20 // An XML-Handler for districts
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
25 #include <utility>
26 #include <iostream>
29 #include <utils/common/ToString.h>
32 #include "ODDistrict.h"
33 #include "ODDistrictCont.h"
34 #include "ODDistrictHandler.h"
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
41  const std::string& file)
42  : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(nullptr) {}
43 
44 
46 
47 
48 void
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
69  if (element == SUMO_TAG_TAZ) {
70  closeDistrict();
71  }
72 }
73 
74 
75 void
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
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
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>
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
141  if (myCurrentDistrict != nullptr) {
143  }
144 }
145 
146 
147 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
@ SUMO_TAG_TAZ
a traffic assignment zone
@ SUMO_TAG_TAZSINK
a sink within a district (connection road)
@ SUMO_TAG_TAZSOURCE
a source within a district (connection road)
@ SUMO_ATTR_EDGES
the edges of a route
@ SUMO_ATTR_WEIGHT
@ SUMO_ATTR_ID
const std::string & getID() const
Returns the id.
Definition: Named.h:74
bool add(const std::string &id, T item)
Adds an item.
A container for districts.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called when an opening-tag occurs.
void openDistrict(const SUMOSAXAttributes &attrs)
Begins the parsing of a district.
std::pair< std::string, double > parseTAZ(const SUMOSAXAttributes &attrs)
Returns the id and weight for a taz/tazSink/tazSource.
ODDistrictHandler(ODDistrictCont &cont, const std::string &file)
Constructor.
void addSource(const SUMOSAXAttributes &attrs)
Adds a read source to the current district.
ODDistrict * myCurrentDistrict
The currently parsed district.
~ODDistrictHandler()
Destructor.
void closeDistrict()
Closes the processing of the current district.
void addSink(const SUMOSAXAttributes &attrs)
Adds a read sink to the current district.
void myEndElement(int element)
Called when a closing tag occurs.
ODDistrictCont & myContainer
The container to add read districts to.
A district (origin/destination)
Definition: ODDistrict.h:42
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:45
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:51
Encapsulated SAX-Attributes.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAX-handler base for SUMO-files.