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.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // An XML-Handler for districts
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <utility>
28 #include <iostream>
31 #include <utils/common/ToString.h>
34 #include "ODDistrict.h"
35 #include "ODDistrictCont.h"
36 #include "ODDistrictHandler.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
43  const std::string& file)
44  : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(nullptr) {}
45 
46 
48 
49 
50 void
52  const SUMOSAXAttributes& attrs) {
53  switch (element) {
54  case SUMO_TAG_TAZ:
55  openDistrict(attrs);
56  break;
57  case SUMO_TAG_TAZSOURCE:
58  addSource(attrs);
59  break;
60  case SUMO_TAG_TAZSINK:
61  addSink(attrs);
62  break;
63  default:
64  break;
65  }
66 }
67 
68 
69 void
71  if (element == SUMO_TAG_TAZ) {
72  closeDistrict();
73  }
74 }
75 
76 
77 void
79  myCurrentDistrict = nullptr;
80  // get the id, report an error if not given or empty...
81  bool ok = true;
82  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
83  if (!ok) {
84  return;
85  }
86  myCurrentDistrict = new ODDistrict(id);
87  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
88  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
89  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
91  myCurrentDistrict->addSink(*i, 1.);
92  }
93  }
94 }
95 
96 
97 void
99  std::pair<std::string, double> vals = parseTAZ(attrs);
100  if (vals.second >= 0) {
101  myCurrentDistrict->addSource(vals.first, vals.second);
102  }
103 }
104 
105 
106 void
108  std::pair<std::string, double> vals = parseTAZ(attrs);
109  if (vals.second >= 0) {
110  myCurrentDistrict->addSink(vals.first, vals.second);
111  }
112 }
113 
114 
115 
116 std::pair<std::string, double>
118  // check the current district first
119  if (myCurrentDistrict == nullptr) {
120  return std::pair<std::string, double>("", -1);
121  }
122  // get the id, report an error if not given or empty...
123  bool ok = true;
124  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
125  if (!ok) {
126  return std::pair<std::string, double>("", -1);
127  }
128  // get the weight
129  double weight = attrs.get<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
130  if (ok) {
131  if (weight < 0) {
132  WRITE_ERROR("'probability' must be positive (in definition of " + attrs.getObjectType() + " '" + id + "').");
133  } else {
134  return std::pair<std::string, double>(id, weight);
135  }
136  }
137  return std::pair<std::string, double>("", -1);
138 }
139 
140 
141 void
143  if (myCurrentDistrict != nullptr) {
145  }
146 }
147 
148 
149 
150 /****************************************************************************/
151 
a source within a district (connection road)
~ODDistrictHandler()
Destructor.
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:53
a traffic assignment zone
void addSource(const SUMOSAXAttributes &attrs)
Adds a read source to the current district.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
const std::string & getID() const
Returns the id.
Definition: Named.h:78
std::pair< std::string, double > parseTAZ(const SUMOSAXAttributes &attrs)
Returns the id and weight for a taz/tazSink/tazSource.
SAX-handler base for SUMO-files.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called when an opening-tag occurs.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
bool add(const std::string &id, T item)
Adds an item.
the edges of a route
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
void myEndElement(int element)
Called when a closing tag occurs.
A container for districts.
void addSink(const SUMOSAXAttributes &attrs)
Adds a read sink to the current district.
ODDistrictHandler(ODDistrictCont &cont, const std::string &file)
Constructor.
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
a sink within a district (connection road)
void closeDistrict()
Closes the processing of the current district.
void openDistrict(const SUMOSAXAttributes &attrs)
Begins the parsing of a district.
ODDistrict * myCurrentDistrict
The currently parsed district.
ODDistrictCont & myContainer
The container to add read districts to.
A district (origin/destination)
Definition: ODDistrict.h:45
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:47