SUMO - Simulation of Urban MObility
ShapeHandler.cpp
Go to the documentation of this file.
1 
2 /****************************************************************************/
8 // The XML-Handler for network loading
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <string>
33 #include <utils/xml/XMLSubSys.h>
37 #include <utils/common/RGBColor.h>
43 #include "Shape.h"
44 #include "ShapeContainer.h"
45 #include "ShapeHandler.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 ShapeHandler::ShapeHandler(const std::string& file, ShapeContainer& sc) :
52  SUMOSAXHandler(file), myShapeContainer(sc),
53  myPrefix(""), myDefaultColor(RGBColor::RED), myDefaultLayer(), myDefaultFill(false) {}
54 
55 
57 
58 
59 void
61  const SUMOSAXAttributes& attrs) {
62  try {
63  switch (element) {
64  case SUMO_TAG_POLY:
66  addPoly(attrs, false, false);
67  break;
68  case SUMO_TAG_POI:
69  myDefaultLayer = (double)GLO_POI;
70  addPOI(attrs, false, false);
71  break;
72  default:
73  break;
74  }
75  } catch (InvalidArgument& e) {
76  WRITE_ERROR(e.what());
77  }
78 }
79 
80 
81 
82 void
83 ShapeHandler::addPOI(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
84  bool ok = true;
85  const double INVALID_POSITION(-1000000);
86  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
87  double x = attrs.getOpt<double>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
88  const double y = attrs.getOpt<double>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
89  double lon = attrs.getOpt<double>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
90  double lat = attrs.getOpt<double>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
91  const double lanePos = attrs.getOpt<double>(SUMO_ATTR_POSITION, id.c_str(), ok, INVALID_POSITION);
92  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
93  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
94  const std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
95  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
96  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
97  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
98  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
100  }
101  const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
102  const double height = attrs.getOpt<double>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
103  if (!ok) {
104  return;
105  }
106  const GeoConvHelper& gch = useProcessing ? GeoConvHelper::getProcessing() : GeoConvHelper::getFinal();
107  if (useProcessing && gch.usingGeoProjection()) {
108  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
109  lon = x;
110  lat = y;
111  x = INVALID_POSITION;
112  }
113  }
114  Position pos(x, y);
115  if (x == INVALID_POSITION || y == INVALID_POSITION) {
116  // try computing x,y from lane,pos
117  if (laneID != "") {
118  pos = getLanePos(id, laneID, lanePos);
119  } else {
120  // try computing x,y from lon,lat
121  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
122  WRITE_ERROR("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '" + id + "'.");
123  return;
124  } else if (!gch.usingGeoProjection()) {
125  WRITE_ERROR("(lon, lat) is specified for PoI '" + id + "' but no geo-conversion is specified for the network.");
126  return;
127  }
128  pos.set(lon, lat);
129  bool success = true;
130  if (useProcessing) {
131  success = GeoConvHelper::getProcessing().x2cartesian(pos);
132  } else {
134  }
135  if (!success) {
136  WRITE_ERROR("Unable to project coordinates for PoI '" + id + "'.");
137  return;
138  }
139  }
140  }
141  if (!myShapeContainer.addPOI(id, type, color, layer, angle, imgFile, pos, width, height, ignorePruning)) {
142  WRITE_ERROR("PoI '" + id + "' already exists.");
143  }
144 }
145 
146 
147 void
148 ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
149  bool ok = true;
150  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
151  // get the id, report an error if not given or empty...
152  if (!ok) {
153  return;
154  }
155  const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
156  const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
157  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
158  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
159  PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
160  if (attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false)) {
161  bool success = true;
162  for (int i = 0; i < (int)shape.size(); i++) {
163  if (useProcessing) {
164  success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
165  } else {
166  success &= GeoConvHelper::getFinal().x2cartesian_const(shape[i]);
167  }
168  }
169  if (!success) {
170  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
171  return;
172  }
173  }
174  const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
175  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
176  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
178  }
179  if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, shape, fill, ignorePruning)) {
180  WRITE_ERROR("Polygon '" + id + "' already exists.");
181  }
182 }
183 
184 
185 
186 bool
187 ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
188  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
189  if (!XMLSubSys::runParser(sh, *fileIt, false)) {
190  WRITE_MESSAGE("Loading of shapes from " + *fileIt + " failed.");
191  return false;
192  }
193  }
194  return true;
195 }
196 
197 
198 void
199 ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const double layer, const bool fill) {
200  myPrefix = prefix;
201  myDefaultColor = color;
202  myDefaultLayer = layer;
203  myDefaultFill = fill;
204 }
205 
206 
207 
208 /****************************************************************************/
double myDefaultLayer
The default layer to use.
Definition: ShapeHandler.h:118
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:82
ShapeHandler(const std::string &file, ShapeContainer &sc)
Constructor.
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
void setDefaults(const std::string &prefix, const RGBColor &color, const double layer, const bool fill=false)
virtual Position getLanePos(const std::string &poiID, const std::string &laneID, double lanePos)=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:98
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)
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:154
Storage for geometrical objects.
begin/end of the description of a polygon
RGBColor myDefaultColor
The default color to use.
Definition: ShapeHandler.h:116
void set(double x, double y)
set positions x and y
Definition: Position.h:93
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:110
static const std::string DEFAULT_TYPE
Definition: Shape.h:149
std::string myPrefix
The prefix to use.
Definition: ShapeHandler.h:114
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:200
The XML-Handler for network loading.
Definition: ShapeHandler.h:56
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:60
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.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
Definition: FileHelpers.cpp:97
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
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 fill, bool ignorePruning=false)
Builds a polygon using the given values and adds it to the container.
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:120
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:206
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.
ShapeContainer & myShapeContainer
Definition: ShapeHandler.h:110
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:153
bool x2cartesian_const(Position &from) const
Converts the given coordinate into a cartesian using the previous initialisation. ...
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, double layer, double angle, const std::string &imgFile, const Position &pos, double width, double height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
A color information.
Fill the polygon.
static const double DEFAULT_ANGLE
Definition: Shape.h:151
begin/end of the description of a polygon
static const double DEFAULT_LAYER
Definition: Shape.h:150