SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 ShapeHandler::ShapeHandler(const std::string& file, ShapeContainer& sc) :
56  SUMOSAXHandler(file), myShapeContainer(sc),
57  myPrefix(""), myDefaultColor(RGBColor::RED), myDefaultLayer(), myDefaultFill(false) {}
58 
59 
61 
62 
63 void
65  const SUMOSAXAttributes& attrs) {
66  try {
67  switch (element) {
68  case SUMO_TAG_POLY:
70  addPoly(attrs, false, false);
71  break;
72  case SUMO_TAG_POI:
74  addPOI(attrs, false, false);
75  break;
76  default:
77  break;
78  }
79  } catch (InvalidArgument& e) {
80  WRITE_ERROR(e.what());
81  }
82 }
83 
84 
85 
86 void
87 ShapeHandler::addPOI(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
88  bool ok = true;
89  const SUMOReal INVALID_POSITION(-1000000);
90  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
91  SUMOReal x = attrs.getOpt<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
92  const SUMOReal y = attrs.getOpt<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
93  SUMOReal lon = attrs.getOpt<SUMOReal>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
94  SUMOReal lat = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
95  const SUMOReal lanePos = attrs.getOpt<SUMOReal>(SUMO_ATTR_POSITION, id.c_str(), ok, INVALID_POSITION);
96  const SUMOReal layer = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
97  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
98  const std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
99  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
100  const SUMOReal angle = attrs.getOpt<SUMOReal>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
101  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
102  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
104  }
105  const SUMOReal width = attrs.getOpt<SUMOReal>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
106  const SUMOReal height = attrs.getOpt<SUMOReal>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
107  if (!ok) {
108  return;
109  }
110  const GeoConvHelper& gch = useProcessing ? GeoConvHelper::getProcessing() : GeoConvHelper::getFinal();
111  if (useProcessing && gch.usingGeoProjection()) {
112  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
113  lon = x;
114  lat = y;
115  x = INVALID_POSITION;
116  }
117  }
118  Position pos(x, y);
119  if (x == INVALID_POSITION || y == INVALID_POSITION) {
120  // try computing x,y from lane,pos
121  if (laneID != "") {
122  pos = getLanePos(id, laneID, lanePos);
123  } else {
124  // try computing x,y from lon,lat
125  if (lat == INVALID_POSITION || lon == INVALID_POSITION) {
126  WRITE_ERROR("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '" + id + "'.");
127  return;
128  } else if (!gch.usingGeoProjection()) {
129  WRITE_ERROR("(lon, lat) is specified for PoI '" + id + "' but no geo-conversion is specified for the network.");
130  return;
131  }
132  pos.set(lon, lat);
133  bool success = true;
134  if (useProcessing) {
135  success = GeoConvHelper::getProcessing().x2cartesian(pos);
136  } else {
138  }
139  if (!success) {
140  WRITE_ERROR("Unable to project coordinates for PoI '" + id + "'.");
141  return;
142  }
143  }
144  }
145  if (!myShapeContainer.addPOI(id, type, color, layer, angle, imgFile, pos, width, height, ignorePruning)) {
146  WRITE_ERROR("PoI '" + id + "' already exists.");
147  }
148 }
149 
150 
151 void
152 ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
153  bool ok = true;
154  const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
155  // get the id, report an error if not given or empty...
156  if (!ok) {
157  return;
158  }
159  const SUMOReal layer = attrs.getOpt<SUMOReal>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
160  const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
161  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
162  const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
163  PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
164  if (attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false)) {
165  bool success = true;
166  for (int i = 0; i < (int)shape.size(); i++) {
167  if (useProcessing) {
168  success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
169  } else {
170  success &= GeoConvHelper::getFinal().x2cartesian_const(shape[i]);
171  }
172  }
173  if (!success) {
174  WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'.");
175  return;
176  }
177  }
178  const SUMOReal angle = attrs.getOpt<SUMOReal>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
179  std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
180  if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
182  }
183  if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, shape, fill, ignorePruning)) {
184  WRITE_ERROR("Polygon '" + id + "' already exists.");
185  }
186 }
187 
188 
189 
190 bool
191 ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
192  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
193  if (!XMLSubSys::runParser(sh, *fileIt, false)) {
194  WRITE_MESSAGE("Loading of shapes from " + *fileIt + " failed.");
195  return false;
196  }
197  }
198  return true;
199 }
200 
201 
202 void
203 ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const SUMOReal layer, const bool fill) {
204  myPrefix = prefix;
205  myDefaultColor = color;
206  myDefaultLayer = layer;
207  myDefaultFill = fill;
208 }
209 
210 
211 
212 /****************************************************************************/
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:86
ShapeHandler(const std::string &file, ShapeContainer &sc)
Constructor.
bool x2cartesian_const(Position &from) const
Converts the given coordinate into a cartesian using the previous initialisation. ...
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
SUMOReal myDefaultLayer
The default layer to use.
Definition: ShapeHandler.h:118
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
bool x2cartesian(Position &from, bool includeInBoundary=true)
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Storage for geometrical objects.
RGBColor myDefaultColor
The default color to use.
Definition: ShapeHandler.h:116
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's ok.
Definition: XMLSubSys.cpp:114
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 const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:151
const std::string & getFileName() const
returns the current file name
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:60
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, SUMOReal layer, SUMOReal angle, const std::string &imgFile, const Position &pos, SUMOReal width, SUMOReal height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
Encapsulated SAX-Attributes.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:154
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
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:120
virtual Position getLanePos(const std::string &poiID, const std::string &laneID, SUMOReal lanePos)=0
get position for a given laneID
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, SUMOReal layer, SUMOReal 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 addPoly(const SUMOSAXAttributes &attrs, const bool ignorePruning, const bool useProcessing)
adds a polygon
static const SUMOReal DEFAULT_LAYER
Definition: Shape.h:150
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
virtual ~ShapeHandler()
Destructor.
void setDefaults(const std::string &prefix, const RGBColor &color, const SUMOReal layer, const bool fill=false)
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
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 SUMOReal
Definition: config.h:214
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
A color information.
Fill the polygon.
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:153