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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // An XML-Handler for districts
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <utility>
34 #include <iostream>
37 #include <utils/common/ToString.h>
40 #include "ODDistrict.h"
41 #include "ODDistrictCont.h"
42 #include "ODDistrictHandler.h"
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49  const std::string& file)
50  : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(0) {}
51 
52 
54 
55 
56 void
58  const SUMOSAXAttributes& attrs) {
59  switch (element) {
60  case SUMO_TAG_TAZ:
61  openDistrict(attrs);
62  break;
63  case SUMO_TAG_TAZSOURCE:
64  addSource(attrs);
65  break;
66  case SUMO_TAG_TAZSINK:
67  addSink(attrs);
68  break;
69  default:
70  break;
71  }
72 }
73 
74 
75 void
77  if (element == SUMO_TAG_TAZ) {
78  closeDistrict();
79  }
80 }
81 
82 
83 void
86  // get the id, report an error if not given or empty...
87  bool ok = true;
88  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
89  if (!ok) {
90  return;
91  }
92  myCurrentDistrict = new ODDistrict(id);
93  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
94  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
95  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
97  myCurrentDistrict->addSink(*i, 1.);
98  }
99  }
100 }
101 
102 
103 void
105  std::pair<std::string, double> vals = parseTAZ(attrs);
106  if (vals.second >= 0) {
107  myCurrentDistrict->addSource(vals.first, vals.second);
108  }
109 }
110 
111 
112 void
114  std::pair<std::string, double> vals = parseTAZ(attrs);
115  if (vals.second >= 0) {
116  myCurrentDistrict->addSink(vals.first, vals.second);
117  }
118 }
119 
120 
121 
122 std::pair<std::string, double>
124  // check the current district first
125  if (myCurrentDistrict == 0) {
126  return std::pair<std::string, double>("", -1);
127  }
128  // get the id, report an error if not given or empty...
129  bool ok = true;
130  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
131  if (!ok) {
132  return std::pair<std::string, double>("", -1);
133  }
134  // get the weight
135  double weight = attrs.get<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
136  if (ok) {
137  if (weight < 0) {
138  WRITE_ERROR("'probability' must be positive (in definition of " + attrs.getObjectType() + " '" + id + "').");
139  } else {
140  return std::pair<std::string, double>(id, weight);
141  }
142  }
143  return std::pair<std::string, double>("", -1);
144 }
145 
146 
147 void
149  if (myCurrentDistrict != 0) {
151  }
152 }
153 
154 
155 
156 /****************************************************************************/
157 
a source within a district (connection road)
~ODDistrictHandler()
Destructor.
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:59
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:65
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:205
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:51
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:53