SUMO - Simulation of Urban MObility
ODDistrictHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // An XML-Handler for districts
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <utility>
35 #include <iostream>
38 #include <utils/common/ToString.h>
41 #include "ODDistrict.h"
42 #include "ODDistrictCont.h"
43 #include "ODDistrictHandler.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50  const std::string& file)
51  : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(0) {}
52 
53 
55 
56 
57 void
59  const SUMOSAXAttributes& attrs) {
60  switch (element) {
61  case SUMO_TAG_TAZ:
62  openDistrict(attrs);
63  break;
64  case SUMO_TAG_TAZSOURCE:
65  addSource(attrs);
66  break;
67  case SUMO_TAG_TAZSINK:
68  addSink(attrs);
69  break;
70  default:
71  break;
72  }
73 }
74 
75 
76 void
78  if (element == SUMO_TAG_TAZ) {
79  closeDistrict();
80  }
81 }
82 
83 
84 void
87  // get the id, report an error if not given or empty...
88  bool ok = true;
89  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
90  if (!ok) {
91  return;
92  }
93  myCurrentDistrict = new ODDistrict(id);
94  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
95  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
96  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
98  myCurrentDistrict->addSink(*i, 1.);
99  }
100  }
101 }
102 
103 
104 void
106  std::pair<std::string, double> vals = parseTAZ(attrs);
107  if (vals.second >= 0) {
108  myCurrentDistrict->addSource(vals.first, vals.second);
109  }
110 }
111 
112 
113 void
115  std::pair<std::string, double> vals = parseTAZ(attrs);
116  if (vals.second >= 0) {
117  myCurrentDistrict->addSink(vals.first, vals.second);
118  }
119 }
120 
121 
122 
123 std::pair<std::string, double>
125  // check the current district first
126  if (myCurrentDistrict == 0) {
127  return std::pair<std::string, double>("", -1);
128  }
129  // get the id, report an error if not given or empty...
130  bool ok = true;
131  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
132  if (!ok) {
133  return std::pair<std::string, double>("", -1);
134  }
135  // get the weight
136  double weight = attrs.get<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
137  if (ok) {
138  if (weight < 0) {
139  WRITE_ERROR("'probability' must be positive (in definition of " + attrs.getObjectType() + " '" + id + "').");
140  } else {
141  return std::pair<std::string, double>(id, weight);
142  }
143  }
144  return std::pair<std::string, double>("", -1);
145 }
146 
147 
148 void
150  if (myCurrentDistrict != 0) {
152  }
153 }
154 
155 
156 
157 /****************************************************************************/
158 
a source within a district (connection road)
~ODDistrictHandler()
Destructor.
virtual bool add(const std::string &id, T item)
Adds an item.
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:60
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:66
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...
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:206
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:52
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:54