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-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 // Encapsulated xml-attributes that are retrieved from the sumo-binary-xml format (already typed)
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cassert>
27 #include <sstream>
28 #include <utils/common/RGBColor.h>
30 #include <utils/geom/Boundary.h>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
42  const std::map<int, std::string>& predefinedTagsMML,
43  const std::string& objectType,
44  BinaryInputDevice* in, const char version) : SUMOSAXAttributes(objectType), myAttrIds(predefinedTagsMML) {
45  while (in->peek() == BinaryFormatter::BF_XML_ATTRIBUTE) {
46  int attr;
47  unsigned char attrByte;
48  *in >> attrByte;
49  attr = attrByte;
50  if (version > 1) {
52  *in >> attrByte;
53  attr += 256 * attrByte;
54  }
55  int type = in->peek();
56  switch (type) {
58  *in >> myCharValues[attr];
59  break;
61  *in >> myIntValues[attr];
62  break;
65  *in >> myFloatValues[attr];
66  break;
68  *in >> myStringValues[attr];
69  break;
71  int size;
72  *in >> size;
73  while (size > 0) {
74  const int type = in->peek();
78  throw ProcessError("Invalid binary file, only supporting position vectors.");
79  }
80  size--;
81  Position p;
82  *in >> p;
83  myPositionVectors[attr].push_back(p);
84  }
85  break;
86  }
88  *in >> myIntValues[attr];
89  break;
91  *in >> myIntValues[attr];
93  *in >> myCharValues[attr];
94  break;
99  Position p;
100  *in >> p;
101  myPositionVectors[attr].push_back(p);
102  break;
103  }
105  Position p;
106  *in >> p;
107  myPositionVectors[attr].push_back(p);
109  *in >> p;
110  myPositionVectors[attr].push_back(p);
111  break;
112  }
114  *in >> myIntValues[attr];
115  break;
117  *in >> myCharValues[attr];
118  break;
120  *in >> myCharValues[attr];
121  break;
123  std::ostringstream into(std::ios::binary);
124  int size;
125  *in >> size;
127  FileHelpers::writeInt(into, size);
128  if (size > 0) {
129  int intsToRead = size - 1;
130  int bitsOrEntry;
132  *in >> bitsOrEntry;
133  FileHelpers::writeInt(into, bitsOrEntry);
134  if (bitsOrEntry < 0) {
135  intsToRead = (-bitsOrEntry * (size - 1) - 1) / sizeof(int) / 8 + 2;
136  }
137  while (intsToRead > 0) {
139  *in >> bitsOrEntry;
140  FileHelpers::writeInt(into, bitsOrEntry);
141  intsToRead--;
142  }
143  }
144  myStringValues[attr] = into.str();
145  break;
146  }
147  default:
148  throw ProcessError("Binary file is invalid, attribute type is unknown.");
149  }
150  myAttrs.insert(attr);
151  }
152 }
153 
154 
156 }
157 
158 
159 bool
161  return myAttrs.find(id) != myAttrs.end();
162 }
163 
164 
165 bool
167  const std::map<int, char>::const_iterator i = myCharValues.find(id);
168  if (i == myCharValues.end()) {
169  throw EmptyData();
170  }
171  return i->second != 0;
172 }
173 
174 
175 int
177  const std::map<int, int>::const_iterator i = myIntValues.find(id);
178  if (i == myIntValues.end()) {
179  throw EmptyData();
180  }
181  return i->second;
182 }
183 
184 
185 long long int
187  return StringUtils::toLong(getString(id));
188 }
189 
190 
191 std::string
193  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
194  if (i == myStringValues.end()) {
195  throw EmptyData();
196  }
197  return i->second;
198 }
199 
200 
201 std::string
203  const std::string& str) const {
204  const std::map<int, std::string>::const_iterator i = myStringValues.find(id);
205  if (i == myStringValues.end()) {
206  return str;
207  }
208  return i->second;
209 }
210 
211 
212 double
214  const std::map<int, double>::const_iterator i = myFloatValues.find(id);
215  if (i == myFloatValues.end()) {
216  return StringUtils::toDouble(getString(id));
217  }
218  return i->second;
219 }
220 
221 
222 double
223 SUMOSAXAttributesImpl_Binary::getFloat(const std::string& /* id */) const {
224  throw ProcessError("not implemented for binary data");
225 }
226 
227 
228 bool
229 SUMOSAXAttributesImpl_Binary::hasAttribute(const std::string& /* id */) const {
230  throw ProcessError("not implemented for binary data");
231 }
232 
233 
234 std::string
236  const std::string& /* str */) const {
237  throw ProcessError("not implemented for binary data");
238 }
239 
240 
243  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_FUNCTION);
244  if (i != myCharValues.end()) {
245  const char func = i->second;
246  if (func < (char)SUMOXMLDefinitions::EdgeFunctions.size()) {
247  return (SumoXMLEdgeFunc)func;
248  }
249  ok = false;
250  }
251  return EDGEFUNC_NORMAL;
252 }
253 
254 
257  const std::map<int, char>::const_iterator i = myCharValues.find(SUMO_ATTR_TYPE);
258  if (i != myCharValues.end()) {
259  const char type = i->second;
260  if (type < (char)SUMOXMLDefinitions::NodeTypes.size()) {
261  return (SumoXMLNodeType)type;
262  }
263  ok = false;
264  }
265  return NODETYPE_UNKNOWN;
266 }
267 
268 
271  try {
273  } catch (InvalidArgument) {
274  ok = false;
275  return RIGHT_OF_WAY_DEFAULT;
276  } catch (EmptyData) {
277  return RIGHT_OF_WAY_DEFAULT;
278  }
279 }
280 
281 
282 RGBColor
284  const std::map<int, int>::const_iterator i = myIntValues.find(SUMO_ATTR_COLOR);
285  if (i == myIntValues.end()) {
286  throw EmptyData();
287  }
288  const int val = i->second;
289  return RGBColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
290 }
291 
292 
295  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
296  if (i == myPositionVectors.end() || i->second.size() == 0) {
297  throw EmptyData();
298  }
299  return i->second;
300 }
301 
302 
303 Boundary
305  const std::map<int, PositionVector>::const_iterator i = myPositionVectors.find(attr);
306  if (i == myPositionVectors.end() || i->second.size() == 0) {
307  throw EmptyData();
308  }
309  if (i->second.size() != 2) {
310  throw FormatException("boundary format");
311  }
312  return Boundary(i->second[0].x(), i->second[0].y(), i->second[1].x(), i->second[1].y());
313 }
314 
315 
316 std::vector<std::string>
318  std::string def = getString(attr);
319  std::vector<std::string> ret;
320  parseStringVector(def, ret);
321  return ret;
322 }
323 
324 
325 std::string
327  if (myAttrIds.find(attr) == myAttrIds.end()) {
328  return "?";
329  }
330  return myAttrIds.find(attr)->second;
331 }
332 
333 
334 void
336  for (std::set<int>::const_iterator i = myAttrs.begin(); i != myAttrs.end(); ++i) {
337  os << " " << getName(*i);
338  os << "=\"" << getStringSecure(*i, "?") << "\"";
339  }
340 }
341 
342 
345  std::map<std::string, std::string> attrs;
346  for (std::map<int, char>::const_iterator it = myCharValues.begin(); it != myCharValues.end(); ++it) {
347  const std::string attrName = myAttrIds.find(it->first)->second;
348  attrs[attrName] = toString(it->second);
349  }
350  for (std::map<int, int>::const_iterator it = myIntValues.begin(); it != myIntValues.end(); ++it) {
351  const std::string attrName = myAttrIds.find(it->first)->second;
352  attrs[attrName] = toString(it->second);
353  }
354  for (std::map<int, double>::const_iterator it = myFloatValues.begin(); it != myFloatValues.end(); ++it) {
355  const std::string attrName = myAttrIds.find(it->first)->second;
356  attrs[attrName] = toString(it->second);
357  }
358  for (std::map<int, std::string>::const_iterator it = myStringValues.begin(); it != myStringValues.end(); ++it) {
359  const std::string attrName = myAttrIds.find(it->first)->second;
360  attrs[attrName] = it->second;
361  }
362  for (std::map<int, PositionVector>::const_iterator it = myPositionVectors.begin(); it != myPositionVectors.end(); ++it) {
363  const std::string attrName = myAttrIds.find(it->first)->second;
364  attrs[attrName] = toString(it->second);
365  }
367 }
368 
369 /****************************************************************************/
370 
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
static StringBijection< RightOfWay > RightOfWayValues
lane spread functions
int peek()
Returns the next character to be read by an actual parse.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
std::map< int, char > myCharValues
Map of attribute ids to char.
void putback(char c)
Pushes a character back into the stream to be read by the next actual parse.
std::map< int, double > myFloatValues
Map of attribute ids to floats.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...
const std::string & getObjectType() const
return the objecttype to which these attributes belong
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
Encapsulated Xerces-SAX-attributes.
std::map< int, std::string > myStringValues
Map of attribute ids to string.
RightOfWay
algorithms for computing right of way
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
int size() const
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
How to compute right of way.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
Encapsulated SAX-Attributes.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
const std::map< int, std::string > & myAttrIds
Map of attribute ids to names.
A list of positions.
T get(const std::string &str) const
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
RGBColor getColor() const
Returns the value of the named attribute.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
std::map< int, int > myIntValues
Map of attribute ids to integers.
std::map< int, PositionVector > myPositionVectors
Map of attribute ids to string.
std::set< int > myAttrs
the attributes which are set
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
SUMOSAXAttributesImpl_Binary(const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType, BinaryInputDevice *in, const char version)
Constructor.
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, which
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
A color information.
Encapsulates binary reading operations on a file.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.