INET Framework for OMNeT++/OMNEST
inet::osm::OpenStreetMap Class Reference

Represents OpenStreetMap map data, as loaded from an OSM file. More...

#include <OpenStreetMap.h>

Public Member Functions

 OpenStreetMap ()
 
 OpenStreetMap (const OpenStreetMap &)=delete
 
 OpenStreetMap (OpenStreetMap &&other)
 
 ~OpenStreetMap ()
 
void operator= (const OpenStreetMap &)=delete
 
void operator= (OpenStreetMap &&)
 
const BoundsgetBounds () const
 
const std::vector< const Node * > & getNodes () const
 
const std::vector< const Way * > & getWays () const
 
const std::vector< const Relation * > & getRelations () const
 

Static Public Member Functions

static OpenStreetMap from (cXMLElement *mapRoot)
 

Private Member Functions

const char * getPooled (const char *s)
 
void releaseAllocations ()
 
void parseTags (cXMLElement *parent, Tags &intoTags)
 

Private Attributes

Bounds bounds
 
std::vector< const Node * > nodes
 
std::vector< const Way * > ways
 
std::vector< const Relation * > relations
 
std::set< std::string > * strings = new std::set<std::string>()
 

Detailed Description

Represents OpenStreetMap map data, as loaded from an OSM file.

OSM files can be obtained e.g. by exporting from http://openstreetmap.org.

Constructor & Destructor Documentation

◆ OpenStreetMap() [1/3]

inet::osm::OpenStreetMap::OpenStreetMap ( )
inline
120 {}

◆ OpenStreetMap() [2/3]

inet::osm::OpenStreetMap::OpenStreetMap ( const OpenStreetMap )
delete

◆ OpenStreetMap() [3/3]

inet::osm::OpenStreetMap::OpenStreetMap ( OpenStreetMap &&  other)
28 {
29  bounds = other.bounds;
30  nodes = std::move(other.nodes);
31  ways = std::move(other.ways);
32  relations = std::move(other.relations);
33  strings = other.strings;
34  other.strings = nullptr;
35 }

◆ ~OpenStreetMap()

inet::osm::OpenStreetMap::~OpenStreetMap ( )
23 {
25 }

Member Function Documentation

◆ from()

OpenStreetMap inet::osm::OpenStreetMap::from ( cXMLElement *  mapRoot)
static
91 {
92  OpenStreetMap map;
93  std::map<id_t, Node *> nodeById;
94  std::map<id_t, Way *> wayById;
95  std::map<id_t, Relation *> relationById;
96 
97  cXMLElement *boundsElement = mapRoot->getFirstChildWithTag("bounds");
98  Bounds& bounds = map.bounds;
100 
101  if (boundsElement) {
102  bounds.minlat = parseDouble(boundsElement->getAttribute("minlat"));
103  bounds.minlon = parseDouble(boundsElement->getAttribute("minlon"));
104  bounds.maxlat = parseDouble(boundsElement->getAttribute("maxlat"));
105  bounds.maxlon = parseDouble(boundsElement->getAttribute("maxlon"));
106  }
107 
108  for (cXMLElement *nodeElem : mapRoot->getChildrenByTagName("node")) {
109  Node *node = new Node();
110  node->id = parseId(nodeElem->getAttribute("id"));
111  node->lat = parseDouble(nodeElem->getAttribute("lat"));
112  node->lon = parseDouble(nodeElem->getAttribute("lon"));
113  map.parseTags(nodeElem, node->tags);
114  map.nodes.push_back(node);
115  nodeById[node->id] = node;
116  }
117 
118  for (cXMLElement *wayElem : mapRoot->getChildrenByTagName("way")) {
119  Way *way = new Way();
120  way->id = parseId(wayElem->getAttribute("id"));
121  for (cXMLElement *nodeElem : wayElem->getChildrenByTagName("nd")) {
122  id_t ref = parseId(nodeElem->getAttribute("ref"));
123  auto it = nodeById.find(ref);
124  if (it == nodeById.end())
125  throw cRuntimeError("Referenced node not found at %s", nodeElem->getSourceLocation());
126  Node *node = it->second;
127  way->nodes.push_back(node);
128  }
129  map.parseTags(wayElem, way->tags);
130  map.ways.push_back(way);
131  wayById[way->id] = way;
132  }
133 
134  for (cXMLElement *relationElem : mapRoot->getChildrenByTagName("relation")) {
135  Relation *relation = new Relation();
136  relation->id = parseId(relationElem->getAttribute("id"));
137  for (cXMLElement *memberElem : relationElem->getChildrenByTagName("member")) {
138  Member member;
139  const char *type = memberElem->getAttribute("type");
140  id_t ref = parseId(memberElem->getAttribute("ref"));
141  if (strcmp(type, "node") == 0) {
142  member.type = Member::NODE;
143  auto it = nodeById.find(ref);
144  member.resolved = (it != nodeById.end());
145  if (member.resolved)
146  member.node = it->second;
147  else
148  member.unresolvedId = ref;
149  }
150  else if (strcmp(type, "way") == 0) {
151  member.type = Member::WAY;
152  auto it = wayById.find(ref);
153  member.resolved = (it != wayById.end());
154  if (member.resolved)
155  member.way = it->second;
156  else
157  member.unresolvedId = ref;
158  }
159  else if (strcmp(type, "relation") == 0) {
160  member.type = Member::RELATION;
161  auto it = relationById.find(ref);
162  member.resolved = (it != relationById.end());
163  if (member.resolved)
164  member.relation = it->second;
165  else
166  member.unresolvedId = ref;
167  }
168  else {
169  throw cRuntimeError("Invalid member type '%s' at %s", type, memberElem->getSourceLocation());
170  }
171  member.role = map.getPooled(memberElem->getAttribute("role"));
172  relation->members.push_back(member);
173  }
174  map.parseTags(relationElem, relation->tags);
175  map.relations.push_back(relation);
176  relationById[relation->id] = relation;
177  }
178 
179  // resolve references to relations defined out of order
180  for (const Relation *relation : map.relations) {
181  for (Member& member : const_cast<Relation *>(relation)->members) {
182  if (member.type == Member::RELATION && !member.resolved) {
183  auto it = relationById.find(member.unresolvedId);
184  if (it != relationById.end()) {
185  member.resolved = true;
186  member.relation = it->second;
187  }
188  }
189  }
190  }
191 
192  return map;
193 }

Referenced by inet::visualizer::OpenStreetMapSceneCanvasVisualizer::initialize().

◆ getBounds()

const Bounds& inet::osm::OpenStreetMap::getBounds ( ) const
inline

◆ getNodes()

const std::vector<const Node *>& inet::osm::OpenStreetMap::getNodes ( ) const
inline

◆ getPooled()

const char * inet::osm::OpenStreetMap::getPooled ( const char *  s)
private
72 {
73  if (s == nullptr)
74  return nullptr;
75  auto it = strings->find(s);
76  if (it == strings->end())
77  it = strings->insert(s).first;
78  return it->c_str();
79 }

Referenced by from(), and parseTags().

◆ getRelations()

const std::vector<const Relation *>& inet::osm::OpenStreetMap::getRelations ( ) const
inline
129 { return relations; }

◆ getWays()

const std::vector<const Way *>& inet::osm::OpenStreetMap::getWays ( ) const
inline

◆ operator=() [1/2]

void inet::osm::OpenStreetMap::operator= ( const OpenStreetMap )
delete

◆ operator=() [2/2]

void inet::osm::OpenStreetMap::operator= ( OpenStreetMap &&  other)
49 {
50  if (this != &other) {
52  bounds = other.bounds;
53  nodes = std::move(other.nodes);
54  ways = std::move(other.ways);
55  relations = std::move(other.relations);
56  strings = other.strings;
57  other.strings = nullptr;
58  }
59 }

◆ parseTags()

void inet::osm::OpenStreetMap::parseTags ( cXMLElement *  parent,
Tags intoTags 
)
private
82 {
83  for (cXMLElement *child : parent->getChildrenByTagName("tag")) {
84  const char *k = child->getAttribute("k");
85  const char *v = child->getAttribute("v");
86  tags.add(getPooled(k), getPooled(v));
87  }
88 }

Referenced by from().

◆ releaseAllocations()

void inet::osm::OpenStreetMap::releaseAllocations ( )
private
38 {
39  for (const Way *way : ways)
40  delete way;
41  for (const Node *node : nodes)
42  delete node;
43  for (const Relation *relation : relations)
44  delete relation;
45  delete strings;
46 }

Referenced by operator=(), and ~OpenStreetMap().

Member Data Documentation

◆ bounds

Bounds inet::osm::OpenStreetMap::bounds
private

Referenced by from(), OpenStreetMap(), and operator=().

◆ nodes

std::vector<const Node *> inet::osm::OpenStreetMap::nodes
private

◆ relations

std::vector<const Relation *> inet::osm::OpenStreetMap::relations
private

◆ strings

std::set<std::string>* inet::osm::OpenStreetMap::strings = new std::set<std::string>()
private

◆ ways

std::vector<const Way *> inet::osm::OpenStreetMap::ways
private

The documentation for this class was generated from the following files:
inet::osm::Member::RELATION
@ RELATION
Definition: OpenStreetMap.h:64
inet::osm::Bounds::maxlon
double maxlon
Definition: OpenStreetMap.h:100
inet::osm::OpenStreetMap::nodes
std::vector< const Node * > nodes
Definition: OpenStreetMap.h:111
inet::osm::OpenStreetMap::ways
std::vector< const Way * > ways
Definition: OpenStreetMap.h:112
inet::osm::Member::NODE
@ NODE
Definition: OpenStreetMap.h:64
inet::osm::OpenStreetMap::relations
std::vector< const Relation * > relations
Definition: OpenStreetMap.h:113
inet::osm::Bounds::maxlat
double maxlat
Definition: OpenStreetMap.h:100
inet::osm::parseId
id_t parseId(const char *s)
Definition: OpenStreetMap.cc:66
inet::osm::OpenStreetMap::bounds
Bounds bounds
Definition: OpenStreetMap.h:110
inet::units::values::s
value< double, units::s > s
Definition: Units.h:1235
type
removed type
Definition: IUdp-gates.txt:7
inet::osm::Bounds::minlat
double minlat
Definition: OpenStreetMap.h:100
inet::physicallayer::k
const double k
Definition: Qam1024Modulation.cc:14
inet::osm::OpenStreetMap::OpenStreetMap
OpenStreetMap()
Definition: OpenStreetMap.h:120
tags
* tags
Definition: IUdp-gates.txt:3
inet::osm::Member::WAY
@ WAY
Definition: OpenStreetMap.h:64
inet::osm::OpenStreetMap::getPooled
const char * getPooled(const char *s)
Definition: OpenStreetMap.cc:71
inet::osm::OpenStreetMap::releaseAllocations
void releaseAllocations()
Definition: OpenStreetMap.cc:37
inet::osm::id_t
int64_t id_t
Definition: OpenStreetMap.h:17
inet::osm::parseDouble
double parseDouble(const char *s)
Definition: OpenStreetMap.cc:61
inet::osm::Bounds::minlon
double minlon
Definition: OpenStreetMap.h:100
inet::osm::OpenStreetMap::strings
std::set< std::string > * strings
Definition: OpenStreetMap.h:114