Eclipse SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Binary.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2019 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 /****************************************************************************/
16 // Encapsulated xml-attributes that are retrieved from the sumo-binary-xml format (already typed)
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <cassert>
26 #include <sstream>
27 #include <utils/common/RGBColor.h>
29 #include <utils/geom/Boundary.h>
35 
36 
37 // ===========================================================================
38 // class definitions
39 // ===========================================================================
41  const std::map<int, std::string>& predefinedTagsMML,
42  const std::string& objectType,
43  BinaryInputDevice* in, const char version) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
44  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
45  int attr;
46  unsigned char attrByte;
47  *in >> attrByte;
48  attr = attrByte;
49  if (version > 1) {
51  *in >> attrByte;
52  attr += 256 * attrByte;
53  }
54  int type = in->peek();
55  switch (type) {
57  *in >> myCharValues[attr];
58  break;
60  *in >> myIntValues[attr];
61  break;
64  *in >> myFloatValues[attr];
65  break;
67  *in >> myStringValues[attr];
68  break;
70  int size;
71  *in >> size;
72  while (size > 0) {
73  const int type = in->peek();
77  throw ProcessError("Invalid binary file, only supporting position vectors.");
78  }
79  size--;
80  Position p;
81  *in >> p;
82  myPositionVectors[attr].push_back(p);
83  }
84  break;
85  }
87  *in >> myIntValues[attr];
88  break;
90  *in >> myIntValues[attr];
92  *in >> myCharValues[attr];
93  break;
98  Position p;
99  *in >> p;
100  myPositionVectors[attr].push_back(p);
101  break;
102  }
104  Position p;
105  *in >> p;
106  myPositionVectors[attr].push_back(p);
108  *in >> p;
109  myPositionVectors[attr].push_back(p);
110  break;
111  }
113  *in >> myIntValues[attr];
114  break;
116  *in >> myCharValues[attr];
117  break;
119  *in >> myCharValues[attr];
120  break;
122  std::ostringstream into(std::ios::binary);
123  int size;
124  *in >> size;
126  FileHelpers::writeInt(into, size);
127  if (size > 0) {
128  int intsToRead = size - 1;
129  int bitsOrEntry;
131  *in >> bitsOrEntry;
132  FileHelpers::writeInt(into, bitsOrEntry);
133  if (bitsOrEntry < 0) {
134  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
135  }
136  while (intsToRead > 0) {
138  *in >> bitsOrEntry;
139  FileHelpers::writeInt(into, bitsOrEntry);
140  intsToRead--;
141  }
142  }
143  myStringValues[attr] = into.str();
144  break;
145  }
146  default:
147  throw ProcessError("Binary file is invalid, attribute type is unknown.");
148  }
149  myAttrs.insert(attr);
150  }
151 }
152 
153 
155 }
156 
157 
158 bool
160  return myAttrs.find(id) != myAttrs.end();
161 }
162 
163 
164 bool
166  const std::map<int, char>::const_iterator i = myCharValues.find(id);
167  if (i == myCharValues.end()) {
168  throw EmptyData();
169  }
170  return i->second != 0;
171 }
172 
173 
174 int
176  const std::map<int, int>::const_iterator i = myIntValues.find(id);
177  if (i == myIntValues.end()) {
178  throw EmptyData();
179  }
180  return i->second;
181 }
182 
183 
184 long long int
186  return StringUtils::toLong(getString(id));
187 }
188 
189 
190 std::string
192  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
193  if (i == myStringValues.end()) {
194  throw EmptyData();
195  }
196  return i->second;
197 }
198 
199 
200 std::string
202  const std::string& str) const {
203  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
204  if (i == myStringValues.end()) {
205  return str;
206  }
207  return i->second;
208 }
209 
210 
211 double
213  const std::map<int, double>::const_iterator i = myFloatValues.find(id);
214  if (i == myFloatValues.end()) {
215  return StringUtils::toDouble(getString(id));
216  }
217  return i->second;
218 }
219 
220 
221 double
222 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
223  throw ProcessError("not implemented for binary data");
224 }
225 
226 
227 bool
228 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
229  throw ProcessError("not implemented for binary data");
230 }
231 
232 
233 std::string
235  const std::string& /* str */) const {
236  throw ProcessError("not implemented for binary data");
237 }
238 
239 
242  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
243  if (i != myCharValues.end()) {
244  const char func = i->second;
245  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
246  return (SumoXMLEdgeFunc)func;
247  }
248  ok = false;
249  }
250  return EDGEFUNC_NORMAL;
251 }
252 
253 
256  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
257  if (i != myCharValues.end()) {
258  const char type = i->second;
259  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
260  return (SumoXMLNodeType)type;
261  }
262  ok = false;
263  }
264  return NODETYPE_UNKNOWN;
265 }
266 
267 
270  try {
272  } catch (InvalidArgument) {
273  ok = false;
274  return RIGHT_OF_WAY_DEFAULT;
275  } catch (EmptyData) {
276  return RIGHT_OF_WAY_DEFAULT;
277  }
278 }
279 
280 
283  try {
285  } catch (InvalidArgument) {
286  ok = false;
287  return FRINGE_TYPE_DEFAULT;
288  } catch (EmptyData) {
289  return FRINGE_TYPE_DEFAULT;
290  }
291 }
292 
293 
294 RGBColor
296  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
297  if (i == myIntValues.end()) {
298  throw EmptyData();
299  }
300  const int val = i->second;
301  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
302 }
303 
304 
307  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
308  if (i == myPositionVectors.end() || i->second.size() == 0) {
309  throw EmptyData();
310  }
311  return i->second;
312 }
313 
314 
315 Boundary
317  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
318  if (i == myPositionVectors.end() || i->second.size() == 0) {
319  throw EmptyData();
320  }
321  if (i->second.size() != 2) {
322  throw FormatException("boundary format");
323  }
324  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
325 }
326 
327 
328 std::string
330  if (myAttrIds.find(attr) == myAttrIds.end()) {
331  return "?";
332  }
333  return myAttrIds.find(attr)->second;
334 }
335 
336 
337 void
339  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
340  os << " " << getName(*i);
341  os << "=\"" << getStringSecure(*i, "?") << "\"";
342  }
343 }
344 
345 std::vector<std::string>
347  std::vector<std::string> result;
348  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
349  result.push_back(getName(*i));
350  }
351  return result;
352 }
353 
354 
357  std::map<std::string, std::string> attrs;
358  for (std::map<int, char>::const_iterator it = myCharValues.begin(); it != myCharValues.end(); ++it) {
359  const std::string attrName = myAttrIds.find(it->first)->second;
360  attrs[attrName] = toString(it->second);
361  }
362  for (std::map<int, int>::const_iterator it = myIntValues.begin(); it != myIntValues.end(); ++it) {
363  const std::string attrName = myAttrIds.find(it->first)->second;
364  attrs[attrName] = toString(it->second);
365  }
366  for (std::map<int, double>::const_iterator it = myFloatValues.begin(); it != myFloatValues.end(); ++it) {
367  const std::string attrName = myAttrIds.find(it->first)->second;
368  attrs[attrName] = toString(it->second);
369  }
370  for (std::map<int, std::string>::const_iterator it = myStringValues.begin(); it != myStringValues.end(); ++it) {
371  const std::string attrName = myAttrIds.find(it->first)->second;
372  attrs[attrName] = it->second;
373  }
374  for (std::map<int, PositionVector>::const_iterator it = myPositionVectors.begin(); it != myPositionVectors.end(); ++it) {
375  const std::string attrName = myAttrIds.find(it->first)->second;
376  attrs[attrName] = toString(it->second);
377  }
379 }
380 
381 /****************************************************************************/
382 
SUMO_ATTR_TYPE
@ SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:381
Boundary.h
FringeType
FringeType
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1110
RIGHT_OF_WAY_DEFAULT
@ RIGHT_OF_WAY_DEFAULT
Definition: SUMOXMLDefinitions.h:1105
SUMOSAXAttributesImpl_Binary::serialize
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
Definition: SUMOSAXAttributesImpl_Binary.cpp:338
BinaryFormatter::BF_SCALED2INT_POSITION_3D
@ BF_SCALED2INT_POSITION_3D
Definition: BinaryFormatter.h:97
SUMOSAXAttributesImpl_Binary::myIntValues
std::map< int, int > myIntValues
Map of attribute ids to integers.
Definition: SUMOSAXAttributesImpl_Binary.h:297
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
SUMOSAXAttributesImpl_Binary::getFloat
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:212
SUMOSAXAttributesImpl_Binary::myCharValues
std::map< int, char > myCharValues
Map of attribute ids to char.
Definition: SUMOSAXAttributesImpl_Binary.h:294
SUMOSAXAttributesImpl_Binary::myFloatValues
std::map< int, double > myFloatValues
Map of attribute ids to floats.
Definition: SUMOSAXAttributesImpl_Binary.h:300
SUMOSAXAttributesImpl_Binary::~SUMOSAXAttributesImpl_Binary
virtual ~SUMOSAXAttributesImpl_Binary()
Destructor.
Definition: SUMOSAXAttributesImpl_Binary.cpp:154
SUMOXMLDefinitions::RightOfWayValues
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
Definition: SUMOXMLDefinitions.h:1377
FileHelpers::writeInt
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
Definition: FileHelpers.cpp:184
SUMOSAXAttributes::getObjectType
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Definition: SUMOSAXAttributes.h:396
BinaryFormatter::BF_LANE
@ BF_LANE
Definition: BinaryFormatter.h:77
SumoXMLEdgeFunc
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
Definition: SUMOXMLDefinitions.h:1079
SUMOSAXAttributesImpl_Binary::getString
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:191
SUMO_ATTR_COLOR
@ SUMO_ATTR_COLOR
A color information.
Definition: SUMOXMLDefinitions.h:704
NODETYPE_UNKNOWN
@ NODETYPE_UNKNOWN
Definition: SUMOXMLDefinitions.h:1055
EmptyData
Definition: UtilExceptions.h:68
BinaryFormatter::BF_LIST
@ BF_LIST
Definition: BinaryFormatter.h:67
BinaryFormatter::BF_NODE_TYPE
@ BF_NODE_TYPE
Definition: BinaryFormatter.h:87
BinaryFormatter::BF_BYTE
@ BF_BYTE
Definition: BinaryFormatter.h:59
SUMOSAXAttributesImpl_Binary::getColor
RGBColor getColor() const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:295
FileHelpers::writeByte
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
Definition: FileHelpers.cpp:198
PositionVector
A list of positions.
Definition: PositionVector.h:45
SUMOSAXAttributesImpl_Binary::myPositionVectors
std::map< int, PositionVector > myPositionVectors
Map of attribute ids to string.
Definition: SUMOSAXAttributesImpl_Binary.h:306
RGBColor.h
SUMOSAXAttributesImpl_Binary::myAttrs
std::set< int > myAttrs
the attributes which are set
Definition: SUMOSAXAttributesImpl_Binary.h:291
BinaryInputDevice::putback
void putback(char c)
Pushes a character back into the stream to be read by the next actual parse.
Definition: BinaryInputDevice.cpp:68
SUMO_ATTR_FUNCTION
@ SUMO_ATTR_FUNCTION
Definition: SUMOXMLDefinitions.h:659
SUMOSAXAttributesImpl_Binary.h
BinaryInputDevice.h
RGBColor
Definition: RGBColor.h:39
BinaryFormatter::BF_SCALED2INT_POSITION_2D
@ BF_SCALED2INT_POSITION_2D
Definition: BinaryFormatter.h:95
SUMOSAXAttributesImpl_Cached
Encapsulated Xerces-SAX-attributes.
Definition: SUMOSAXAttributesImpl_Cached.h:43
SUMOSAXAttributesImpl_Binary::getShape
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
Definition: SUMOSAXAttributesImpl_Binary.cpp:306
SUMOSAXAttributesImpl_Binary::getRightOfWay
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
Definition: SUMOSAXAttributesImpl_Binary.cpp:269
SumoXMLNodeType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Definition: SUMOXMLDefinitions.h:1054
StringBijection::get
T get(const std::string &str) const
Definition: StringBijection.h:97
BinaryFormatter::BF_SCALED2INT
@ BF_SCALED2INT
Definition: BinaryFormatter.h:93
SUMOSAXAttributesImpl_Binary::clone
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
Definition: SUMOSAXAttributesImpl_Binary.cpp:356
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
SUMOSAXAttributesImpl_Binary::getLong
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:185
ProcessError
Definition: UtilExceptions.h:39
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
SUMOSAXAttributesImpl_Binary::getBoundary
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
Definition: SUMOSAXAttributesImpl_Binary.cpp:316
BinaryFormatter::BF_EDGE_FUNCTION
@ BF_EDGE_FUNCTION
Definition: BinaryFormatter.h:89
BinaryFormatter::BF_ROUTE
@ BF_ROUTE
Definition: BinaryFormatter.h:91
EDGEFUNC_NORMAL
@ EDGEFUNC_NORMAL
Definition: SUMOXMLDefinitions.h:1081
FormatException
Definition: UtilExceptions.h:81
BinaryFormatter::BF_POSITION_2D
@ BF_POSITION_2D
Definition: BinaryFormatter.h:79
BinaryFormatter::BF_XML_ATTRIBUTE
@ BF_XML_ATTRIBUTE
Definition: BinaryFormatter.h:73
BinaryFormatter::BF_EDGE
@ BF_EDGE
Definition: BinaryFormatter.h:75
SUMOSAXAttributesImpl_Binary::myAttrIds
const std::map< int, std::string > & myAttrIds
Map of attribute ids to names.
Definition: SUMOSAXAttributesImpl_Binary.h:288
SUMOSAXAttributesImpl_Binary::getBool
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:165
BinaryFormatter::BF_INTEGER
@ BF_INTEGER
Definition: BinaryFormatter.h:61
SUMOSAXAttributesImpl_Binary::getInt
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:175
BinaryFormatter::BF_POSITION_3D
@ BF_POSITION_3D
Definition: BinaryFormatter.h:81
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
SUMOSAXAttributesImpl_Binary::hasAttribute
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list.
Definition: SUMOSAXAttributesImpl_Binary.cpp:159
SUMOSAXAttributesImpl_Binary::getFringeType
FringeType getFringeType(bool &ok) const
returns fringe type
Definition: SUMOSAXAttributesImpl_Binary.cpp:282
InvalidArgument
Definition: UtilExceptions.h:56
RightOfWay
RightOfWay
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1104
SUMOXMLDefinitions::EdgeFunctions
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
Definition: SUMOXMLDefinitions.h:1371
BinaryInputDevice::peek
int peek()
Returns the next character to be read by an actual parse.
Definition: BinaryInputDevice.cpp:55
SUMO_ATTR_RIGHT_OF_WAY
@ SUMO_ATTR_RIGHT_OF_WAY
How to compute right of way.
Definition: SUMOXMLDefinitions.h:698
SUMOSAXAttributesImpl_Cached.h
SUMOSAXAttributesImpl_Binary::getNodeType
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:255
BinaryFormatter::BF_STRING
@ BF_STRING
Definition: BinaryFormatter.h:65
SUMOSAXAttributesImpl_Binary::getEdgeFunc
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:241
SUMOXMLDefinitions::FringeTypeValues
static StringBijection< FringeType > FringeTypeValues
fringe types
Definition: SUMOXMLDefinitions.h:1380
BinaryFormatter::BF_BOUNDARY
@ BF_BOUNDARY
Definition: BinaryFormatter.h:83
config.h
FRINGE_TYPE_DEFAULT
@ FRINGE_TYPE_DEFAULT
Definition: SUMOXMLDefinitions.h:1113
SUMO_ATTR_FRINGE
@ SUMO_ATTR_FRINGE
Fringe type of node.
Definition: SUMOXMLDefinitions.h:700
StringUtils::toLong
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:297
BinaryFormatter::BF_FLOAT
@ BF_FLOAT
Definition: BinaryFormatter.h:63
SUMOSAXAttributesImpl_Binary::getName
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
Definition: SUMOSAXAttributesImpl_Binary.cpp:329
SUMOSAXAttributesImpl_Binary::myStringValues
std::map< int, std::string > myStringValues
Map of attribute ids to string.
Definition: SUMOSAXAttributesImpl_Binary.h:303
SUMOSAXAttributesImpl_Binary::getStringSecure
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Binary.cpp:201
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
PositionVector.h
BinaryFormatter.h
SUMOSAXAttributesImpl_Binary::SUMOSAXAttributesImpl_Binary
SUMOSAXAttributesImpl_Binary(const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType, BinaryInputDevice *in, const char version)
Constructor.
Definition: SUMOSAXAttributesImpl_Binary.cpp:40
SUMOXMLDefinitions::NodeTypes
static StringBijection< SumoXMLNodeType > NodeTypes
node types
Definition: SUMOXMLDefinitions.h:1368
BinaryInputDevice
Encapsulates binary reading operations on a file.
Definition: BinaryInputDevice.h:57
BinaryFormatter::BF_COLOR
@ BF_COLOR
Definition: BinaryFormatter.h:85
SUMOSAXAttributesImpl_Binary::getAttributeNames
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
Definition: SUMOSAXAttributesImpl_Binary.cpp:346