SUMO - Simulation of Urban MObility
ShapeHandler.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 /****************************************************************************/
17 // The XML-Handler for network loading
18 /****************************************************************************/
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #ifdef _MSC_VER
23 #include <windows_config.h>
24 #else
25 #include <config.h>
26 #endif
27 
28 #include <string>
31 #include <utils/xml/XMLSubSys.h>
35 #include <utils/common/RGBColor.h>
41 #include "Shape.h"
42 #include "ShapeContainer.h"
43 #include "ShapeHandler.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 ShapeHandler::ShapeHandler(const std::string& file, ShapeContainer& sc) :
50  SUMOSAXHandler(file), myShapeContainer(sc),
51  myPrefix(""), myDefaultColor(RGBColor::RED), myDefaultLayer(), myDefaultFill(false),
52  myLastParameterised(0) {
53 }
54 
55 
57 
58 
59 void
60 ShapeHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
61  try {
62  switch (element) {
63  case SUMO_TAG_POLY:
65  addPoly(attrs, false, false);
66  break;
67  case SUMO_TAG_POI:
68  myDefaultLayer = (double)GLO_POI;
69  addPOI(attrs, false, false);
70  break;
71  case SUMO_TAG_PARAM:
72  if (myLastParameterised != 0) {
73  bool ok = true;
74  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
75  // circumventing empty string test
76  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
78  }
79  default:
80  break;
81  }
82  } catch (InvalidArgument& e) {
83  WRITE_ERROR(e.what());
84  }
85 }
86 
87 
88 void
90  if (element != SUMO_TAG_PARAM) {
92  }
93 }
94 
95 void
96 ShapeHandler::addPOI(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
97  bool ok = true;
98  const double INVALID_POSITION(-1000000);
99  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
100  double x = attrs.getOpt<double>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
101  const double y = attrs.getOpt<double>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
102  double lon = attrs.getOpt<double>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
103  double lat = attrs.getOpt<double>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
104  const double lanePos = attrs.getOpt<double>(SUMO_ATTR_POSITION, id.c_str(), ok, 0);
105  const double lanePosLat = attrs.getOpt<double>(SUMO_ATTR_POSITION_LAT, id.c_str(), ok, 0);
106  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
107  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
108  const std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
109  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
110  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
111  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
112  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
114  }
115  const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
116  const double height = attrs.getOpt<double>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
117  if (!ok) {
118  return;
119  }
120  const GeoConvHelper& gch = useProcessing ? GeoConvHelper::getProcessing() : GeoConvHelper::getFinal();
121  if (useProcessing && gch.usingGeoProjection()) {
122  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
123  lon = x;
124  lat = y;
125  x = INVALID_POSITION;
126  }
127  }
128  Position pos(x, y);
129  if (x == INVALID_POSITION || y == INVALID_POSITION) {
130  // try computing x,y from lane,pos
131  if (laneID != "") {
132  pos = getLanePos(id, laneID, lanePos, lanePosLat);
133  } else {
134  // try computing x,y from lon,lat
135  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
136  WRITE_ERROR("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '" + id + "'.");
137  return;
138  } else if (!gch.usingGeoProjection()) {
139  WRITE_ERROR("(lon, lat) is specified for PoI '" + id + "' but no geo-conversion is specified for the network.");
140  return;
141  }
142  pos.set(lon, lat);
143  bool success = true;
144  if (useProcessing) {
145  success = GeoConvHelper::getProcessing().x2cartesian(pos);
146  } else {
148  }
149  if (!success) {
150  WRITE_ERROR("Unable to project coordinates for PoI '" + id + "'.");
151  return;
152  }
153  }
154  }
155  if (!myShapeContainer.addPOI(id, type, color, pos, gch.usingGeoProjection(), laneID, lanePos, lanePosLat, layer, angle, imgFile, width, height, ignorePruning)) {
156  WRITE_ERROR("PoI '" + id + "' already exists.");
157  }
159  if (laneID != "" && addLanePosParams()) {
160  myLastParameterised->setParameter(toString(SUMO_ATTR_LANE), laneID);
161  myLastParameterised->setParameter(toString(SUMO_ATTR_POSITION), toString(lanePos));
162  myLastParameterised->setParameter(toString(SUMO_ATTR_POSITION_LAT), toString(lanePosLat));
163  }
164 }
165 
166 
167 void
168 ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
169  bool ok = true;
170  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
171  // get the id, report an error if not given or empty...
172  if (!ok) {
173  return;
174  }
175  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
176  const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
177  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
178  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
179  PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
180  bool geo = false;
181  if (attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false)) {
182  geo = true;
183  bool success = true;
184  for (int i = 0; i < (int)shape.size(); i++) {
185  if (useProcessing) {
186  success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
187  } else {
188  success &= GeoConvHelper::getFinal().x2cartesian_const(shape[i]);
189  }
190  }
191  if (!success) {
192  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
193  return;
194  }
195  }
196  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
197  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
198  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
200  }
201  // check that shape's size is valid
202  if (shape.size() == 0) {
203  WRITE_ERROR("Polygon's shape cannot be empty.");
204  return;
205  }
206  if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, shape, geo, fill, ignorePruning)) {
207  WRITE_ERROR("Polygon '" + id + "' already exists.");
208  }
210 }
211 
212 
213 
214 bool
215 ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
216  for (auto fileIt : files) {
217  if (!XMLSubSys::runParser(sh, fileIt, false)) {
218  WRITE_MESSAGE("Loading of shapes from " + fileIt + " failed.");
219  return false;
220  }
221  }
222  return true;
223 }
224 
225 
226 void
227 ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const double layer, const bool fill) {
228  myPrefix = prefix;
229  myDefaultColor = color;
230  myDefaultLayer = layer;
231  myDefaultFill = fill;
232 }
233 
234 
235 
236 /****************************************************************************/
double myDefaultLayer
The default layer to use.
Definition: ShapeHandler.h:126
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:81
ShapeHandler(const std::string &file, ShapeContainer &sc)
Constructor.
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:151
void setDefaults(const std::string &prefix, const RGBColor &color, const double layer, const bool fill=false)
set default values
virtual void myEndElement(int element)
Called when a closing tag occurs.
const Polygons & getPolygons() const
Returns all polygons.
virtual Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos, double lanePosLat)=0
get position for a given laneID
A layer number.
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:90
static bool loadFiles(const std::vector< std::string > &files, ShapeHandler &sh)
loads all of the given files
const std::string & getFileName() const
returns the current file name
bool x2cartesian(Position &from, bool includeInBoundary=true)
Converts the given coordinate into a cartesian and optionally update myConvBoundary.
T get(const std::string &id) const
Retrieves an item.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
static const double DEFAULT_IMG_HEIGHT
Definition: Shape.h:153
Storage for geometrical objects.
begin/end of the description of a Point of interest
RGBColor myDefaultColor
The default color to use.
Definition: ShapeHandler.h:123
Parameterised * myLastParameterised
element to receive parameters
Definition: ShapeHandler.h:135
void set(double x, double y)
set positions x and y
Definition: Position.h:92
SAX-handler base for SUMO-files.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:109
static const std::string DEFAULT_TYPE
Definition: Shape.h:148
std::string myPrefix
The prefix to use.
Definition: ShapeHandler.h:120
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
The XML-Handler for network loading.
Definition: ShapeHandler.h:55
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, double layer, double angle, const std::string &imgFile, const PositionVector &shape, bool geo, bool fill, bool ignorePruning=false)
Builds a polygon using the given values and adds it to the container.
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:59
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
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.
virtual bool addLanePosParams()
Whether some input attributes shall be automatically added as params.
Definition: ShapeHandler.h:101
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Definition: FileHelpers.cpp:96
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
parameter associated to a certain key
A list of positions.
void addPOI(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a POI
bool myDefaultFill
Information whether polygons should be filled.
Definition: ShapeHandler.h:129
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
edge: the shape in xml-definition
void addPoly(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a polygon
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual ~ShapeHandler()
Destructor.
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, const Position &pos, bool geo, const std::string &lane, double posOverLane, double posLat, double layer, double angle, const std::string &imgFile, double width, double height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
ShapeContainer & myShapeContainer
Definition: ShapeHandler.h:116
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
#define INVALID_POSITION
static const double DEFAULT_IMG_WIDTH
Definition: Shape.h:152
bool x2cartesian_const(Position &from) const
Converts the given coordinate into a cartesian using the previous initialisation. ...
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:200
A color information.
Fill the polygon.
static const double DEFAULT_ANGLE
Definition: Shape.h:150
begin/end of the description of a polygon
static const double DEFAULT_LAYER
Definition: Shape.h:149
const POIs & getPOIs() const
Returns all pois.