Eclipse SUMO - Simulation of Urban MObility
GNETAZSourceSink.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-2020 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 /****************************************************************************/
18 //
19 /****************************************************************************/
20 #include <config.h>
21 
23 #include <netedit/GNEUndoList.h>
24 #include <netedit/GNENet.h>
25 
26 #include "GNETAZSourceSink.h"
27 
28 
29 // ===========================================================================
30 // member method definitions
31 // ===========================================================================
32 
33 GNETAZSourceSink::GNETAZSourceSink(SumoXMLTag sourceSinkTag, GNETAZElement* TAZParent, GNEEdge* edge, double departWeight) :
34  GNETAZElement(TAZParent, TAZParent->getNet(), GLO_TAZ, sourceSinkTag, false,
35 {}, {edge}, {}, {}, {}, {TAZParent}, {}, {}),
36 myDepartWeight(departWeight) {
37  //check that this is a TAZ Source OR a TAZ Sink
38  if ((sourceSinkTag != SUMO_TAG_TAZSOURCE) && (sourceSinkTag != SUMO_TAG_TAZSINK)) {
39  throw InvalidArgument("Invalid TAZ Child Tag");
40  }
41 }
42 
43 
45 
46 
47 const PositionVector&
49  return getParentTAZElements().front()->getTAZElementShape();
50 }
51 
52 
53 void
55  // open source/sink tag
56  device.openTag(myTagProperty.getTag());
57  // write source/sink attributes
58  device.writeAttr(SUMO_ATTR_ID, getParentEdges().front()->getID());
60  // close tag
61  device.closeTag();
62 }
63 
64 
65 double
67  return myDepartWeight;
68 }
69 
70 
71 void
73  // This TAZElement doesn't own a geometry
74 }
75 
76 
79  return getParentTAZElements().at(0)->getPositionInView();
80 }
81 
82 
85  return getParentEdges().front()->getCenteringBoundary();
86 }
87 
88 
89 void
90 GNETAZSourceSink::splitEdgeGeometry(const double /*splitPosition*/, const GNENetworkElement* /*originalElement*/, const GNENetworkElement* /*newElement*/, GNEUndoList* /*undoList*/) {
91  // geometry of this element cannot be splitted
92 }
93 
94 
95 std::string
97  return getParentTAZElements().at(0)->getID();
98 }
99 
100 
101 void
103  // Currently This TAZElement isn't drawn
104 }
105 
106 
107 std::string
109  switch (key) {
110  case SUMO_ATTR_ID:
111  return getID();
112  case SUMO_ATTR_EDGE:
113  return getParentEdges().front()->getID();
114  case SUMO_ATTR_WEIGHT:
115  return toString(myDepartWeight);
116  case GNE_ATTR_PARENT:
117  return getParentTAZElements().at(0)->getID();
118  case GNE_ATTR_PARAMETERS:
119  return getParametersStr();
120  case GNE_ATTR_TAZCOLOR: {
121  // obtain max and min weight source
122  double maxWeightSource = getParentTAZElements().at(0)->getAttributeDouble(GNE_ATTR_MAX_SOURCE);
123  double minWeightSource = getParentTAZElements().at(0)->getAttributeDouble(GNE_ATTR_MIN_SOURCE);
124  // avoid division between zero
125  if ((maxWeightSource - minWeightSource) == 0) {
126  return "0";
127  } else {
128  // calculate percentage relative to the max and min weight
129  double percentage = (myDepartWeight - minWeightSource) / (maxWeightSource - minWeightSource);
130  // convert percentage to a value between [0-9] (because we have only 10 colors)
131  if (percentage >= 1) {
132  return "9";
133  } else if (percentage < 0) {
134  return "0";
135  } else {
136  return toString((int)(percentage * 10));
137  }
138  }
139  }
140  default:
141  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
142  }
143 }
144 
145 double
147  switch (key) {
148  case SUMO_ATTR_WEIGHT:
149  return myDepartWeight;
150  default:
151  throw InvalidArgument(getTagStr() + " doesn't have a double attribute of type '" + toString(key) + "'");
152  }
153 }
154 
155 
156 void
157 GNETAZSourceSink::setAttribute(SumoXMLAttr key, const std::string& value, GNEUndoList* undoList) {
158  // this TAZElement is the only that can edit a variable directly, see GNEAdditionalHandler::buildTAZEdge(...)
159  if (undoList == nullptr) {
160  setAttribute(key, value);
161  } else {
162  if (value == getAttribute(key)) {
163  return; //avoid needless changes, later logic relies on the fact that attributes have changed
164  }
165  switch (key) {
166  case SUMO_ATTR_ID:
167  case SUMO_ATTR_WEIGHT:
168  case GNE_ATTR_PARAMETERS:
169  undoList->p_add(new GNEChange_Attribute(this, key, value));
170  break;
171  default:
172  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
173  }
174  }
175 }
176 
177 
178 bool
179 GNETAZSourceSink::isValid(SumoXMLAttr key, const std::string& value) {
180  switch (key) {
181  case SUMO_ATTR_ID:
182  return isValidTAZElementID(value);
183  case SUMO_ATTR_WEIGHT:
184  return canParse<double>(value) && (parse<double>(value) >= 0);
185  case GNE_ATTR_PARAMETERS:
186  return Parameterised::areParametersValid(value);
187  default:
188  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
189  }
190 }
191 
192 
193 
194 bool
196  switch (key) {
197  case SUMO_ATTR_EDGE:
198  return false;
199  default:
200  return true;
201  }
202 }
203 
204 
205 std::string
207  return getTagStr();
208 }
209 
210 
211 std::string
213  return getTagStr() + ": " + getAttribute(SUMO_ATTR_WEIGHT);
214 }
215 
216 // ===========================================================================
217 // private
218 // ===========================================================================
219 
220 void
221 GNETAZSourceSink::setAttribute(SumoXMLAttr key, const std::string& value) {
222  switch (key) {
223  case SUMO_ATTR_ID:
224  myNet->getAttributeCarriers()->updateID(this, value);
225  break;
226  case SUMO_ATTR_WEIGHT:
227  myDepartWeight = parse<double>(value);
228  // update statictis of TAZ parent
229  getParentTAZElements().at(0)->updateParentAdditional();
230  break;
231  case GNE_ATTR_PARAMETERS:
232  setParametersStr(value);
233  break;
234  default:
235  throw InvalidArgument(getTagStr() + " doesn't have an attribute of type '" + toString(key) + "'");
236  }
237 }
238 
239 
240 /****************************************************************************/
@ GLO_TAZ
Traffic Assignment Zones (TAZs)
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_TAZSINK
a sink within a district (connection road)
@ SUMO_TAG_TAZSOURCE
a source within a district (connection road)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
@ GNE_ATTR_MAX_SOURCE
max source (used only by TAZs)
@ SUMO_ATTR_EDGE
@ GNE_ATTR_TAZCOLOR
Color of TAZSources/TAZSinks.
@ GNE_ATTR_PARENT
parent of an additional element
@ GNE_ATTR_PARAMETERS
parameters "key1=value1|key2=value2|...|keyN=valueN"
@ SUMO_ATTR_WEIGHT
@ SUMO_ATTR_ID
@ GNE_ATTR_MIN_SOURCE
min source (used only by TAZs)
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
friend class GNEChange_Attribute
declare friend class
const std::string & getTagStr() const
get tag assigned to this object in string format
GNENet * myNet
pointer to net
const GNETagProperties & myTagProperty
the xml tag to which this attribute carrier corresponds
A road/street connecting two junctions (netedit-version)
Definition: GNEEdge.h:49
const std::vector< GNEEdge * > & getParentEdges() const
get parent edges
const std::vector< GNETAZElement * > & getParentTAZElements() const
get parent TAZElements
void updateID(GNEAttributeCarrier *AC, const std::string newID)
update ID
GNENetHelper::AttributeCarriers * getAttributeCarriers() const
retrieve all attribute carriers of Net
Definition: GNENet.cpp:130
An Element which don't belongs to GNENet but has influency in the simulation.
Definition: GNETAZElement.h:45
bool isValidTAZElementID(const std::string &newID) const
check if a new TAZElement ID is valid
const std::string & getID() const
get ID
bool isValid(SumoXMLAttr key, const std::string &value)
method for checking if the key and their conrrespond attribute are valids
void updateGeometry()
update pre-computed geometry information
void setAttribute(SumoXMLAttr key, const std::string &value, GNEUndoList *undoList)
method for setting the attribute and letting the object perform TAZElement changes
void splitEdgeGeometry(const double splitPosition, const GNENetworkElement *originalElement, const GNENetworkElement *newElement, GNEUndoList *undoList)
split geometry
void writeTAZElement(OutputDevice &device) const
writte TAZElement element into a xml file
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
std::string getAttribute(SumoXMLAttr key) const
inherited from GNEAttributeCarrier
double getDepartWeight() const
get depart weight
~GNETAZSourceSink()
destructor
double myDepartWeight
depart Weight
std::string getPopUpID() const
get PopPup ID (Used in AC Hierarchy)
std::string getHierarchyName() const
get Hierarchy Name (Used in AC Hierarchy)
std::string getParentName() const
Returns the name of the parent object.
GNETAZSourceSink(SumoXMLTag sourceSinkTag, GNETAZElement *TAZParent, GNEEdge *edge, double departWeight)
Constructor.
double getAttributeDouble(SumoXMLAttr key) const
const PositionVector & getTAZElementShape() const
get TAZ Shape
bool isAttributeEnabled(SumoXMLAttr key) const
void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Position getPositionInView() const
Returns position of additional in view.
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
void p_add(GNEChange_Attribute *cmd)
special method, avoid empty changes, always execute
Stores the information about how to visualize structures.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:239
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
static bool areParametersValid(const std::string &value, bool report=false, ParameterisedAttrType attrType=ParameterisedAttrType::STRING, const std::string kvsep="=", const std::string sep="|")
check if given string can be parsed to a parameters map "key1=value1|key2=value2|....
void setParametersStr(const std::string &paramsString, const std::string kvsep="=", const std::string sep="|")
set the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN"
std::string getParametersStr(const std::string kvsep="=", const std::string sep="|") const
Returns the inner key/value map in string format "key1=value1|key2=value2|...|keyN=valueN".
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
A list of positions.