SUMO - Simulation of Urban MObility
PCLoaderOSM.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // A reader of pois and polygons stored in OSM-format
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2008-2014 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <map>
37 #include <fstream>
40 #include <utils/common/ToString.h>
43 #include <utils/options/Option.h>
44 #include <utils/common/StdDefs.h>
46 #include "PCLoaderOSM.h"
47 #include <utils/common/RGBColor.h>
48 #include <utils/geom/GeomHelper.h>
49 #include <utils/geom/Position.h>
51 #include <utils/xml/XMLSubSys.h>
54 
55 #ifdef CHECK_MEMORY_LEAKS
56 #include <foreign/nvwa/debug_new.h>
57 #endif // CHECK_MEMORY_LEAKS
58 // ---------------------------------------------------------------------------
59 // static members
60 // ---------------------------------------------------------------------------
62 
63 // ===========================================================================
64 // method definitions
65 // ===========================================================================
66 // ---------------------------------------------------------------------------
67 // static interface
68 // ---------------------------------------------------------------------------
69 std::set<std::string> PCLoaderOSM::initMyKeysToInclude() {
70  std::set<std::string> result;
71  result.insert("highway");
72  result.insert("waterway");
73  result.insert("aeroway");
74  result.insert("aerialway");
75  result.insert("power");
76  result.insert("man_made");
77  result.insert("building");
78  result.insert("leisure");
79  result.insert("amenity");
80  result.insert("shop");
81  result.insert("tourism");
82  result.insert("historic");
83  result.insert("landuse");
84  result.insert("natural");
85  result.insert("military");
86  result.insert("boundary");
87  result.insert("admin_level");
88  result.insert("sport");
89  result.insert("polygon");
90  result.insert("place");
91  result.insert("population");
92  result.insert("openGeoDB:population");
93  result.insert("openGeoDB:name");
94  return result;
95 }
96 
97 void
99  PCTypeMap& tm) {
100  if (!oc.isSet("osm-files")) {
101  return;
102  }
103  // parse file(s)
104  std::vector<std::string> files = oc.getStringVector("osm-files");
105  // load nodes, first
106  std::map<SUMOLong, PCOSMNode*> nodes;
107  bool withAttributes = oc.getBool("all-attributes");
109  NodesHandler nodesHandler(nodes, withAttributes, *m);
110  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
111  // nodes
112  if (!FileHelpers::isReadable(*file)) {
113  WRITE_ERROR("Could not open osm-file '" + *file + "'.");
114  return;
115  }
116  PROGRESS_BEGIN_MESSAGE("Parsing nodes from osm-file '" + *file + "'");
117  if (!XMLSubSys::runParser(nodesHandler, *file)) {
118  throw ProcessError();
119  }
121  }
122  // load edges, then
123  std::map<std::string, PCOSMEdge*> edges;
124  EdgesHandler edgesHandler(nodes, edges, withAttributes, *m);
125  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
126  // edges
127  PROGRESS_BEGIN_MESSAGE("Parsing edges from osm-file '" + *file + "'");
128  XMLSubSys::runParser(edgesHandler, *file);
130  }
131 
132  // build all
133  const bool useName = oc.getBool("osm.use-name");
134  // instatiate polygons
135  for (std::map<std::string, PCOSMEdge*>::iterator i = edges.begin(); i != edges.end(); ++i) {
136  PCOSMEdge* e = (*i).second;
137  if (e->myAttributes.size() == 0) {
138  // cannot be relevant as a polygon
139  continue;
140  }
141  if (e->myCurrentNodes.size() == 0) {
142  WRITE_ERROR("Polygon '" + e->id + "' has no shape.");
143  continue;
144  }
145  // compute shape
146  PositionVector vec;
147  for (std::vector<SUMOLong>::iterator j = e->myCurrentNodes.begin(); j != e->myCurrentNodes.end(); ++j) {
148  PCOSMNode* n = nodes.find(*j)->second;
149  Position pos(n->lon, n->lat);
150  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
151  WRITE_WARNING("Unable to project coordinates for polygon '" + e->id + "'.");
152  }
153  vec.push_back_noDoublePos(pos);
154  }
155  const bool ignorePruning = OptionsCont::getOptions().isInStringVector("prune.keep-list", toString(e->id));
156  // add as many polygons as keys match defined types
157  int index = 0;
158  std::string unknownPolyType = "";
159  for (std::map<std::string, std::string>::iterator it = e->myAttributes.begin(); it != e->myAttributes.end(); ++it) {
160  const std::string& key = it->first;
161  const std::string& value = it->second;
162  const std::string fullType = key + "." + value;
163  if (tm.has(key + "." + value)) {
164  index = addPolygon(e, vec, tm.get(fullType), fullType, index, useName, toFill, ignorePruning, withAttributes);
165  } else if (tm.has(key)) {
166  index = addPolygon(e, vec, tm.get(key), fullType, index, useName, toFill, ignorePruning, withAttributes);
167  } else if (MyKeysToInclude.count(key) > 0) {
168  unknownPolyType = fullType;
169  }
170  }
171  const PCTypeMap::TypeDef& def = tm.getDefault();
172  if (index == 0 && !def.discard && unknownPolyType != "") {
173  addPolygon(e, vec, def, unknownPolyType, index, useName, toFill, ignorePruning, withAttributes);
174  }
175  }
176 
177 
178  // instantiate pois
179  for (std::map<SUMOLong, PCOSMNode*>::iterator i = nodes.begin(); i != nodes.end(); ++i) {
180  PCOSMNode* n = (*i).second;
181  if (n->myAttributes.size() == 0) {
182  // cannot be relevant as a poi
183  continue;
184  }
185  Position pos(n->lon, n->lat);
186  if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
187  WRITE_WARNING("Unable to project coordinates for POI '" + toString(n->id) + "'.");
188  }
189  const bool ignorePruning = OptionsCont::getOptions().isInStringVector("prune.keep-list", toString(n->id));
190  // add as many POIs as keys match defined types
191  int index = 0;
192  std::string unKnownPOIType = "";
193  for (std::map<std::string, std::string>::iterator it = n->myAttributes.begin(); it != n->myAttributes.end(); ++it) {
194  const std::string& key = it->first;
195  const std::string& value = it->second;
196  const std::string fullType = key + "." + value;
197  if (tm.has(key + "." + value)) {
198  index = addPOI(n, pos, tm.get(fullType), fullType, index, toFill, ignorePruning, withAttributes);
199  } else if (tm.has(key)) {
200  index = addPOI(n, pos, tm.get(key), fullType, index, toFill, ignorePruning, withAttributes);
201  } else if (MyKeysToInclude.count(key) > 0) {
202  unKnownPOIType = fullType;
203  }
204  }
205  const PCTypeMap::TypeDef& def = tm.getDefault();
206  if (index == 0 && !def.discard && unKnownPOIType != "") {
207  addPOI(n, pos, def, unKnownPOIType, index, toFill, ignorePruning, withAttributes);
208  }
209  }
210  // delete nodes
211  for (std::map<SUMOLong, PCOSMNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
212  delete(*i).second;
213  }
214  // delete edges
215  for (std::map<std::string, PCOSMEdge*>::iterator i = edges.begin(); i != edges.end(); ++i) {
216  delete(*i).second;
217  }
218 }
219 
220 
221 int
222 PCLoaderOSM::addPolygon(const PCOSMEdge* edge, const PositionVector& vec, const PCTypeMap::TypeDef& def, const std::string& fullType, int index, bool useName, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes) {
223  if (def.discard) {
224  return index;
225  } else {
226  const bool closedShape = vec.front() == vec.back();
227  const std::string idSuffix = (index == 0 ? "" : "#" + toString(index));
228  const std::string id = def.prefix + (useName && edge->name != "" ? edge->name : edge->id) + idSuffix;
229  Polygon* poly = new Polygon(
231  StringUtils::escapeXML(OptionsCont::getOptions().getBool("osm.keep-full-type") ? fullType : def.id),
232  def.color, vec, def.allowFill && closedShape, (SUMOReal)def.layer);
233  if (withAttributes) {
234  poly->addParameter(edge->myAttributes);
235  }
236  if (!toFill.insert(id, poly, def.layer, ignorePruning)) {
237  return index;
238  } else {
239  return index + 1;
240  }
241  }
242 }
243 
244 int
245 PCLoaderOSM::addPOI(const PCOSMNode* node, const Position& pos, const PCTypeMap::TypeDef& def, const std::string& fullType,
246  int index, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes) {
247  if (def.discard) {
248  return index;
249  } else {
250  const std::string idSuffix = (index == 0 ? "" : "#" + toString(index));
251  const std::string id = def.prefix + toString(node->id) + idSuffix;
252  PointOfInterest* poi = new PointOfInterest(
254  StringUtils::escapeXML(OptionsCont::getOptions().getBool("osm.keep-full-type") ? fullType : def.id),
255  def.color, pos, (SUMOReal)def.layer);
256  if (withAttributes) {
257  poi->addParameter(node->myAttributes);
258  }
259  if (!toFill.insert(id, poi, def.layer, ignorePruning)) {
260  return index;
261  } else {
262  return index + 1;
263  }
264  }
265 }
266 
267 
268 // ---------------------------------------------------------------------------
269 // definitions of PCLoaderOSM::NodesHandler-methods
270 // ---------------------------------------------------------------------------
271 PCLoaderOSM::NodesHandler::NodesHandler(std::map<SUMOLong, PCOSMNode*>& toFill,
272  bool withAttributes, MsgHandler& errorHandler) :
273  SUMOSAXHandler("osm - file"), myWithAttributes(withAttributes), myErrorHandler(errorHandler),
274  myToFill(toFill), myLastNodeID(-1) {}
275 
276 
278 
279 
280 void
282  myParentElements.push_back(element);
283  if (element == SUMO_TAG_NODE) {
284  bool ok = true;
285  SUMOLong id = attrs.get<SUMOLong>(SUMO_ATTR_ID, 0, ok);
286  if (!ok) {
287  return;
288  }
289  myLastNodeID = -1;
290  if (myToFill.find(id) == myToFill.end()) {
291  myLastNodeID = id;
292  // assume we are loading multiple files...
293  // ... so we won't report duplicate nodes
294  PCOSMNode* toAdd = new PCOSMNode();
295  toAdd->id = id;
296  bool ok = true;
297  toAdd->lon = attrs.get<SUMOReal>(SUMO_ATTR_LON, toString(id).c_str(), ok);
298  toAdd->lat = attrs.get<SUMOReal>(SUMO_ATTR_LAT, toString(id).c_str(), ok);
299  if (!ok) {
300  delete toAdd;
301  return;
302  }
303  myToFill[toAdd->id] = toAdd;
304  }
305  }
306  if (element == SUMO_TAG_TAG && myParentElements.size() > 2 && myParentElements[myParentElements.size() - 2] == SUMO_TAG_NODE
307  && myLastNodeID != -1) {
308  bool ok = true;
309  std::string key = attrs.getOpt<std::string>(SUMO_ATTR_K, toString(myLastNodeID).c_str(), ok, "", false);
310  std::string value = attrs.getOpt<std::string>(SUMO_ATTR_V, toString(myLastNodeID).c_str(), ok, "", false);
311  if (key == "") {
312  myErrorHandler.inform("Empty key in a a tag while parsing node '" + toString(myLastNodeID) + "' occured.");
313  ok = false;
314  }
315  if (!ok) {
316  return;
317  }
318  myToFill[myLastNodeID]->myAttributes[key] = value;
319  }
320 }
321 
322 
323 void
325  if (element == SUMO_TAG_NODE) {
326  myLastNodeID = -1;
327  }
328  myParentElements.pop_back();
329 }
330 
331 
332 // ---------------------------------------------------------------------------
333 // definitions of PCLoaderOSM::EdgesHandler-methods
334 // ---------------------------------------------------------------------------
335 PCLoaderOSM::EdgesHandler::EdgesHandler(const std::map<SUMOLong, PCOSMNode*>& osmNodes,
336  std::map<std::string, PCOSMEdge*>& toFill, bool withAttributes, MsgHandler& errorHandler)
337  : SUMOSAXHandler("osm - file"), myWithAttributes(withAttributes), myErrorHandler(errorHandler),
338  myOSMNodes(osmNodes), myEdgeMap(toFill) {
339 }
340 
341 
343 }
344 
345 
346 void
348  myParentElements.push_back(element);
349  // parse "way" elements
350  if (element == SUMO_TAG_WAY) {
351  bool ok = true;
352  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
353  if (!ok) {
354  return;
355  }
356  myCurrentEdge = new PCOSMEdge();
357  myCurrentEdge->id = id;
358  myCurrentEdge->myIsClosed = false;
359  myKeep = false;
360  }
361  // parse "nd" (node) elements
362  if (element == SUMO_TAG_ND) {
363  bool ok = true;
364  SUMOLong ref = attrs.get<SUMOLong>(SUMO_ATTR_REF, 0, ok);
365  if (ok) {
366  if (myOSMNodes.find(ref) == myOSMNodes.end()) {
367  WRITE_WARNING("The referenced geometry information (ref='" + toString(ref) + "') is not known");
368  return;
369  }
370  myCurrentEdge->myCurrentNodes.push_back(ref);
371  }
372  }
373  // parse values
374  if (element == SUMO_TAG_TAG && myParentElements.size() > 2 && myParentElements[myParentElements.size() - 2] == SUMO_TAG_WAY
375  && myCurrentEdge != 0) {
376  bool ok = true;
377  std::string key = attrs.getOpt<std::string>(SUMO_ATTR_K, toString(myCurrentEdge->id).c_str(), ok, "", false);
378  std::string value = attrs.getOpt<std::string>(SUMO_ATTR_V, toString(myCurrentEdge->id).c_str(), ok, "", false);
379  if (key == "") {
380  myErrorHandler.inform("Empty key in a a tag while parsing way '" + toString(myCurrentEdge->id) + "' occured.");
381  ok = false;
382  }
383  if (!ok) {
384  return;
385  }
386  if (key == "name") {
387  myCurrentEdge->name = value;
388  } else if (MyKeysToInclude.count(key) > 0) {
389  myKeep = true;
390  }
391  myCurrentEdge->myAttributes[key] = value;
392  }
393 }
394 
395 
396 void
398  myParentElements.pop_back();
399  if (element == SUMO_TAG_WAY) {
400  if (myKeep) {
401  myEdgeMap[myCurrentEdge->id] = myCurrentEdge;
402  } else {
403  delete myCurrentEdge;
404  }
405  myCurrentEdge = 0;
406  }
407 }
408 
409 
410 /****************************************************************************/
411 
std::string id
The new type id to use.
Definition: PCTypeMap.h:68
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:71
SUMOReal lat
The latitude the node is located at.
Definition: PCLoaderOSM.h:83
An internal definition of a loaded edge.
Definition: PCLoaderOSM.h:91
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static int addPOI(const PCOSMNode *node, const Position &pos, const PCTypeMap::TypeDef &def, const std::string &fullType, int index, PCPolyContainer &toFill, bool ignorePruning, bool withAttributes)
try add the POI and return the next index on success
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as OSM-XML.
Definition: PCLoaderOSM.cpp:98
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:66
bool isInStringVector(const std::string &optionName, const std::string &itemName)
Returns the named option is a list of string values containing the specified item.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:97
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
std::vector< SUMOLong > myCurrentNodes
The list of nodes this edge is made of.
Definition: PCLoaderOSM.h:99
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
EdgesHandler(const std::map< SUMOLong, PCOSMNode * > &osmNodes, std::map< std::string, PCOSMEdge * > &toFill, bool withAttributes, MsgHandler &errorHandler)
Constructor.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
static std::set< std::string > initMyKeysToInclude()
Definition: PCLoaderOSM.cpp:69
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
SUMOReal lon
The longitude the node is located at.
Definition: PCLoaderOSM.h:81
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
std::string id
The edge's id.
Definition: PCLoaderOSM.h:93
bool discard
Information whether polygons of this type shall be discarded.
Definition: PCTypeMap.h:76
A storage for loaded polygons and pois.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
NodesHandler(std::map< SUMOLong, PCOSMNode * > &toFill, bool withAttributes, MsgHandler &errorHandler)
Contructor.
RGBColor color
The color to use.
Definition: PCTypeMap.h:70
A 2D- or 3D-polygon.
Definition: Polygon.h:50
static const std::set< std::string > MyKeysToInclude
Definition: PCLoaderOSM.h:115
std::map< std::string, std::string > myAttributes
Additional attributes.
Definition: PCLoaderOSM.h:101
A storage for type mappings.
Definition: PCTypeMap.h:52
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:79
void myEndElement(int element)
Called when a closing tag occurs.
void myEndElement(int element)
Called when a closing tag occurs.
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
std::map< std::string, std::string > myAttributes
Additional attributes.
Definition: PCLoaderOSM.h:85
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:85
SUMOLong id
The node's id.
Definition: PCLoaderOSM.h:79
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
#define SUMOLong
Definition: config.h:212
A class which extracts OSM-edges from a parsed OSM-file.
Definition: PCLoaderOSM.h:196
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
A class which extracts OSM-nodes from a parsed OSM-file.
Definition: PCLoaderOSM.h:126
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
std::string prefix
The prefix to use.
Definition: PCTypeMap.h:72
static int addPolygon(const PCOSMEdge *edge, const PositionVector &vec, const PCTypeMap::TypeDef &def, const std::string &fullType, int index, bool useName, PCPolyContainer &toFill, bool ignorePruning, bool withAttributes)
try add the polygon and return the next index on success
A storage for options typed value containers)
Definition: OptionsCont.h:108
std::string name
The edge's name (if any)
Definition: PCLoaderOSM.h:95
const TypeDef & getDefault()
get the default type according to the given options
Definition: PCTypeMap.h:115
#define SUMOReal
Definition: config.h:215
void push_back_noDoublePos(const Position &p)
A point-of-interest.
int layer
The layer to use.
Definition: PCTypeMap.h:74
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 PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
bool allowFill
Information whether polygons of this type can be filled.
Definition: PCTypeMap.h:78
An internal representation of an OSM-node.
Definition: PCLoaderOSM.h:77
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.