Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
PCLoaderOSM.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2008-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
22// A reader of pois and polygons stored in OSM-format
23/****************************************************************************/
24#include <config.h>
25
26#include <string>
27#include <map>
28#include <fstream>
37#include <utils/geom/Position.h>
39#include <utils/xml/XMLSubSys.h>
45#include "PCLoaderOSM.h"
46
47// static members
48// ---------------------------------------------------------------------------
50std::set<std::string> PCLoaderOSM::MyDiscardOverrides;
51bool PCLoaderOSM::myFullType(false);
52
53// ===========================================================================
54// method definitions
55// ===========================================================================
56// ---------------------------------------------------------------------------
57// static interface
58// ---------------------------------------------------------------------------
59std::set<std::string> PCLoaderOSM::initMyKeysToInclude() {
60 std::set<std::string> result;
61 result.insert("highway");
62 result.insert("railway");
63 result.insert("railway:position");
64 result.insert("railway:position:exact");
65 result.insert("waterway");
66 result.insert("aeroway");
67 result.insert("aerialway");
68 result.insert("power");
69 result.insert("man_made");
70 result.insert("building");
71 result.insert("leisure");
72 result.insert("amenity");
73 result.insert("shop");
74 result.insert("tourism");
75 result.insert("historic");
76 result.insert("landuse");
77 result.insert("natural");
78 result.insert("military");
79 result.insert("boundary");
80 result.insert("admin_level");
81 result.insert("sport");
82 result.insert("polygon");
83 result.insert("place");
84 result.insert("population");
85 result.insert("barrier");
86 result.insert("openGeoDB:population");
87 result.insert("openGeoDB:name");
88 return result;
89}
90
91void
93 if (!oc.isSet("osm-files")) {
94 return;
95 }
96 const auto& overrides = oc.getStringVector("discard-overrides");
97 MyDiscardOverrides.insert(overrides.begin(), overrides.end());
98
99 // parse file(s)
100 std::vector<std::string> files = oc.getStringVector("osm-files");
101 // load nodes, first
102 std::map<long long int, PCOSMNode*> nodes;
103 bool withAttributes = oc.getBool("all-attributes");
104 myFullType = oc.getBool("osm.keep-full-type");
106 NodesHandler nodesHandler(nodes, withAttributes, *m);
107 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
108 // nodes
109 if (!FileHelpers::isReadable(*file)) {
110 WRITE_ERRORF(TL("Could not open osm-file '%'."), *file);
111 return;
112 }
113 const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing nodes from osm-file '" + *file + "'");
114 if (!XMLSubSys::runParser(nodesHandler, *file)) {
115 for (std::map<long long int, PCOSMNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
116 delete (*i).second;
117 }
118 throw ProcessError();
119 }
120 PROGRESS_TIME_MESSAGE(before);
121 }
122 // load relations to see which additional ways may be relevant
123 Relations relations;
124 RelationsMap additionalWays;
125 std::set<long long int> innerEdges;
126 RelationsHandler relationsHandler(additionalWays, relations, innerEdges, withAttributes, *m);
127 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
128 // edges
129 const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing relations from osm-file '" + *file + "'");
130 XMLSubSys::runParser(relationsHandler, *file);
131 PROGRESS_TIME_MESSAGE(before);
132 }
133
134 // load ways
135 EdgeMap edges;
136 EdgesHandler edgesHandler(nodes, edges, additionalWays, withAttributes, *m);
137 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
138 // edges
139 const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing edges from osm-file '" + *file + "'");
140 XMLSubSys::runParser(edgesHandler, *file);
141 PROGRESS_TIME_MESSAGE(before);
142 }
143
144 // build all
145 const bool useName = oc.getBool("osm.use-name");
146 const double mergeRelationsThreshold = OptionsCont::getOptions().getFloat("osm.merge-relations");
147 // create polygons from relations
148 if (mergeRelationsThreshold >= 0) {
149 for (PCOSMRelation* rel : relations) {
150 if (!rel->keep || rel->myWays.empty()) {
151 continue;
152 }
153 // filter unknown and empty ways
154 int numNodes = 0;
155 for (auto it = rel->myWays.begin(); it != rel->myWays.end();) {
156 if (edges.count(*it) == 0 || edges[*it]->myCurrentNodes.empty()) {
157 it = rel->myWays.erase(it);
158 } else if (innerEdges.count(*it) > 0) {
159 // @note: it would be a good idea to merge inner shapes but
160 // it's difficult since there may be more than one
161 // independent inner shape
162 edges[*it]->standalone = true;
163 it = rel->myWays.erase(it);
164 } else {
165 numNodes += (int)edges[*it]->myCurrentNodes.size();
166 it++;
167 }
168 }
169 if (numNodes == 0) {
170 WRITE_WARNINGF(TL("Could not import polygon from relation '%' (missing ways)"), rel->id);
171 continue;
172 }
173 PCOSMEdge* e = new PCOSMEdge();
174 e->id = rel->id;
175 e->name = rel->name;
176 e->myAttributes = rel->myAttributes;
177 e->myIsClosed = false;
178 e->standalone = true;
179
180 std::vector<std::vector<long long int> > snippets;
181 for (const long long int wayID : rel->myWays) {
182 PCOSMEdge* edge = edges[wayID];
183 snippets.push_back(edge->myCurrentNodes);
184 }
185 double maxDist = 0.;
186 bool ok = true;
187 while (snippets.size() > 1) {
188 maxDist = MAX2(maxDist, mergeClosest(nodes, snippets));
189 if (maxDist > mergeRelationsThreshold) {
190 ok = false;
191 break;
192 }
193 }
194 if (ok) {
195 e->myCurrentNodes = snippets.front();
196 edges[e->id] = e;
197 double frontBackDist = 0;
198 if (e->myCurrentNodes.front() != e->myCurrentNodes.back()) {
199 // should be filled
200 const Position posFront = convertNodePosition(nodes[e->myCurrentNodes.front()]);
201 const Position posBack = convertNodePosition(nodes[e->myCurrentNodes.back()]);
202 frontBackDist = posFront.distanceTo2D(posBack);
203 if (frontBackDist < mergeRelationsThreshold) {
204 e->myCurrentNodes.push_back(e->myCurrentNodes.front());
205 frontBackDist = 0;
206 }
207 }
208 std::string frontBackMsg = "";
209 if (frontBackDist > 0) {
210 frontBackMsg = TLF(", (front-to-back dist: %)", frontBackDist);
211 }
212 WRITE_MESSAGEF(TL("Assembled polygon from relation '%' (name:%)%"), toString(rel->id), e->name, frontBackMsg);
213 } else {
214 WRITE_WARNINGF(TL("Could not import polygon from relation '%' (name:% reason: found gap of %m)."), rel->id, rel->name, maxDist)
215 delete e;
216 // export ways by themselves
217 for (long long int wayID : rel->myWays) {
218 PCOSMEdge* part = edges[wayID];
219 part->standalone = true;
220 }
221 }
222 }
223 }
224
225 // instantiate polygons
226 for (EdgeMap::iterator i = edges.begin(); i != edges.end(); ++i) {
227 PCOSMEdge* e = (*i).second;
228 if (e->myAttributes.size() == 0) {
229 // cannot be relevant as a polygon
230 continue;
231 }
232 if (!e->standalone && mergeRelationsThreshold >= 0) {
233 // part of a relation
234 continue;
235 }
236 if (e->myCurrentNodes.size() == 0) {
237 WRITE_WARNINGF(TL("Polygon '%' has no shape."), toString(e->id));
238 continue;
239 }
240 // compute shape
241 PositionVector vec;
242 for (std::vector<long long int>::iterator j = e->myCurrentNodes.begin(); j != e->myCurrentNodes.end(); ++j) {
243 PCOSMNode* n = nodes.find(*j)->second;
244 Position pos(n->lon, n->lat);
245 if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
246 WRITE_WARNINGF(TL("Unable to project coordinates for polygon '%'."), e->id);
247 }
248 vec.push_back_noDoublePos(pos);
249 }
250 const bool ignorePruning = OptionsCont::getOptions().isInStringVector("prune.keep-list", toString(e->id));
251 // first check whether any discarded key is matching this way
252 std::string toDiscard;
253 for (std::map<std::string, std::string>::iterator it = e->myAttributes.begin(); it != e->myAttributes.end(); ++it) {
254 const std::string& key = it->first;
255 // only ensure discardation (even when other tags match) for configured keys
256 if (MyDiscardOverrides.count(key) != 0) {
257 const std::string& value = it->second;
258 const std::string fullType = key + "." + value;
259 if (tm.has(fullType) && tm.get(fullType).discard) {
260 toDiscard = fullType;
261 break;
262 } else if (tm.has(key) && tm.get(key).discard) {
263 toDiscard = key;
264 break;
265 }
266 }
267 }
268 // add as many polygons as keys match defined types
269 int index = 0;
270 std::string unknownPolyType = "";
271 bool isInner = mergeRelationsThreshold >= 0 && innerEdges.count(e->id) != 0 && tm.has("inner");
272 for (std::map<std::string, std::string>::iterator it = e->myAttributes.begin(); it != e->myAttributes.end(); ++it) {
273 const std::string& key = it->first;
274 const std::string& value = it->second;
275 const std::string fullType = key + "." + value;
276 if (tm.has(key + "." + value)) {
277 auto def = tm.get(isInner ? "inner" : fullType);
278 index = addPolygon(e, vec, def, fullType, index, useName, toFill, ignorePruning, withAttributes, toDiscard);
279 } else if (tm.has(key)) {
280 auto def = tm.get(isInner ? "inner" : key);
281 index = addPolygon(e, vec, def, fullType, index, useName, toFill, ignorePruning, withAttributes, toDiscard);
282 } else if (MyKeysToInclude.count(key) > 0) {
283 unknownPolyType = fullType;
284 }
285 }
286 const PCTypeMap::TypeDef& def = tm.getDefault();
287 if (index == 0 && !def.discard && unknownPolyType != "") {
288 addPolygon(e, vec, def, unknownPolyType, index, useName, toFill, ignorePruning, withAttributes, toDiscard);
289 }
290 }
291
292
293 // instantiate pois
294 for (std::map<long long int, PCOSMNode*>::iterator i = nodes.begin(); i != nodes.end(); ++i) {
295 PCOSMNode* n = (*i).second;
296 if (n->myAttributes.size() == 0) {
297 // cannot be relevant as a poi
298 continue;
299 }
300 Position pos(n->lon, n->lat);
301 if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
302 WRITE_WARNINGF(TL("Unable to project coordinates for POI '%'."), n->id);
303 }
304 const bool ignorePruning = OptionsCont::getOptions().isInStringVector("prune.keep-list", toString(n->id));
305 // add as many POIs as keys match defined types
306 int index = 0;
307 std::string unKnownPOIType = "";
308 for (std::map<std::string, std::string>::iterator it = n->myAttributes.begin(); it != n->myAttributes.end(); ++it) {
309 const std::string& key = it->first;
310 const std::string& value = it->second;
311 const std::string fullType = key + "." + value;
312 if (tm.has(key + "." + value)) {
313 index = addPOI(n, pos, tm.get(fullType), fullType, index, useName, toFill, ignorePruning, withAttributes);
314 } else if (tm.has(key)) {
315 index = addPOI(n, pos, tm.get(key), fullType, index, useName, toFill, ignorePruning, withAttributes);
316 } else if (MyKeysToInclude.count(key) > 0) {
317 unKnownPOIType = fullType;
318 }
319 }
320 const PCTypeMap::TypeDef& def = tm.getDefault();
321 if (index == 0 && !def.discard && unKnownPOIType != "") {
322 addPOI(n, pos, def, unKnownPOIType, index, useName, toFill, ignorePruning, withAttributes);
323 }
324 }
325 // delete nodes
326 for (std::map<long long int, PCOSMNode*>::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
327 delete (*i).second;
328 }
329 // delete edges
330 for (EdgeMap::iterator i = edges.begin(); i != edges.end(); ++i) {
331 delete (*i).second;
332 }
333 // delete relations
334 for (Relations::iterator i = relations.begin(); i != relations.end(); ++i) {
335 delete (*i);
336 }
337}
338
339
346
347
348double
349PCLoaderOSM::parseHeight(const std::map<std::string, std::string>& attrs, long long int id) {
350 auto itH = attrs.find("height");
351 if (itH != attrs.end()) {
352 try {
353 return StringUtils::parseDist(itH->second);
354 } catch (...) {
355 WRITE_WARNINGF(TL("Value of key '%' is not numeric ('%') in way '%'."), "height", itH->second, id);
356 }
357 }
359}
360
361
362int
363PCLoaderOSM::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, const std::string& toDiscard) {
364 if (def.discard) {
365 return index;
366 } else if (toDiscard != "") {
367 WRITE_WARNINGF(TL("Discarding polygon '%' (%) because it was combined with discarded type '%'."), toString(edge->id), fullType, toDiscard);
368 return index;
369 } else {
370 const bool closedShape = vec.front() == vec.back();
371 const std::string idSuffix = (index == 0 ? "" : "#" + toString(index));
372 const std::string id = def.prefix + (useName && edge->name != "" ? edge->name : toString(edge->id)) + idSuffix;
373 bool fill = def.allowFill == PCTypeMap::Filltype::FORCE || (def.allowFill == PCTypeMap::Filltype::FILL && closedShape);
374 SUMOPolygon* poly = new SUMOPolygon(
376 StringUtils::escapeXML(myFullType ? fullType : def.id),
377 def.color, vec, false, fill, 1, def.layer,
381 parseHeight(edge->myAttributes, edge->id));
382 if (withAttributes) {
383 poly->updateParameters(edge->myAttributes);
384 }
385 if (!toFill.add(poly, ignorePruning)) {
386 return index;
387 } else {
388 return index + 1;
389 }
390 }
391}
392
393
394int
395PCLoaderOSM::addPOI(const PCOSMNode* node, const Position& pos, const PCTypeMap::TypeDef& def, const std::string& fullType,
396 int index, bool useName, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes) {
397 if (def.discard) {
398 return index;
399 } else {
400 const std::string idSuffix = (index == 0 ? "" : "#" + toString(index));
401 const std::string id = def.prefix + (useName && node->name != "" ? node->name : toString(node->id)) + idSuffix;
404 StringUtils::escapeXML(myFullType ? fullType : def.id),
405 def.color, pos, false, "", 0, false, 0, def.icon, def.layer);
406 if (withAttributes) {
407 poi->updateParameters(node->myAttributes);
408 }
409 if (!toFill.add(poi, ignorePruning)) {
410 return index;
411 } else {
412 return index + 1;
413 }
414 }
415}
416
417
418// ---------------------------------------------------------------------------
419// definitions of PCLoaderOSM::NodesHandler-methods
420// ---------------------------------------------------------------------------
421PCLoaderOSM::NodesHandler::NodesHandler(std::map<long long int, PCOSMNode*>& toFill,
422 bool withAttributes, MsgHandler& errorHandler) :
423 SUMOSAXHandler("osm - file"),
424 myWithAttributes(withAttributes),
425 myErrorHandler(errorHandler),
426 myToFill(toFill), myLastNodeID(-1) {}
427
428
430
431
432void
434 myParentElements.push_back(element);
435 if (element == SUMO_TAG_NODE) {
436 bool ok = true;
437 long long int id = attrs.get<long long int>(SUMO_ATTR_ID, nullptr, ok);
438 if (!ok) {
439 return;
440 }
441 myLastNodeID = -1;
442 if (myToFill.find(id) == myToFill.end()) {
443 myLastNodeID = id;
444 // assume we are loading multiple files...
445 // ... so we won't report duplicate nodes
446 PCOSMNode* toAdd = new PCOSMNode();
447 toAdd->id = id;
448 toAdd->lon = attrs.get<double>(SUMO_ATTR_LON, toString(id).c_str(), ok);
449 toAdd->lat = attrs.get<double>(SUMO_ATTR_LAT, toString(id).c_str(), ok);
450 if (!ok) {
451 delete toAdd;
452 return;
453 }
454 myToFill[toAdd->id] = toAdd;
455 }
456 }
457 if (element == SUMO_TAG_TAG && myParentElements.size() > 2 && myParentElements[myParentElements.size() - 2] == SUMO_TAG_NODE
458 && myLastNodeID != -1) {
459 bool ok = true;
460 std::string key = attrs.getOpt<std::string>(SUMO_ATTR_K, toString(myLastNodeID).c_str(), ok, "", false);
461 std::string value = attrs.getOpt<std::string>(SUMO_ATTR_V, toString(myLastNodeID).c_str(), ok, "", false);
462 if (key == "name") {
463 myToFill[myLastNodeID]->name = value;
464 } else if (key == "") {
465 myErrorHandler.inform("Empty key in a a tag while parsing node '" + toString(myLastNodeID) + "' occurred.");
466 ok = false;
467 }
468 if (!ok) {
469 return;
470 }
471 myToFill[myLastNodeID]->myAttributes[key] = value;
472 }
473}
474
475
476void
478 if (element == SUMO_TAG_NODE) {
479 myLastNodeID = -1;
480 }
481 myParentElements.pop_back();
482}
483
484
485// ---------------------------------------------------------------------------
486// definitions of PCLoaderOSM::RelationsHandler-methods
487// ---------------------------------------------------------------------------
489 Relations& relations,
490 std::set<long long int>& innerEdges,
491 bool withAttributes,
492 MsgHandler& errorHandler) :
493 SUMOSAXHandler("osm - file"),
494 myAdditionalWays(additionalWays),
495 myRelations(relations),
496 myInnerEdges(innerEdges),
497 myWithAttributes(withAttributes),
498 myErrorHandler(errorHandler),
499 myCurrentRelation(nullptr) {
500}
501
502
505
506
507void
509 myParentElements.push_back(element);
510 // parse "relation" elements
511 if (element == SUMO_TAG_RELATION) {
512 myCurrentWays.clear();
513 bool ok = true;
514 const std::string& action = attrs.getOpt<std::string>(SUMO_ATTR_ACTION, nullptr, ok);
515 if (action == "delete" || !ok) {
516 myCurrentRelation = nullptr;
517 } else {
518 myCurrentRelation = new PCOSMRelation();
519 myCurrentRelation->keep = false;
520 myCurrentRelation->id = attrs.get<long long int>(SUMO_ATTR_ID, nullptr, ok);
521 myRelations.push_back(myCurrentRelation);
522 }
523 return;
524 } else if (myCurrentRelation == nullptr) {
525 return;
526 }
527 // parse member elements
528 if (element == SUMO_TAG_MEMBER) {
529 bool ok = true;
530 std::string role = attrs.hasAttribute("role") ? attrs.getStringSecure("role", "") : "";
531 long long int ref = attrs.get<long long int>(SUMO_ATTR_REF, nullptr, ok);
532 if (role == "outer" || role == "inner") {
533 std::string memberType = attrs.get<std::string>(SUMO_ATTR_TYPE, nullptr, ok);
534 if (memberType == "way") {
535 myCurrentWays.push_back(ref);
536 if (role == "inner") {
537 myInnerEdges.insert(ref);
538 }
539 }
540 }
541 return;
542 }
543 // parse values
544 if (element == SUMO_TAG_TAG && myParentElements.size() > 2 && myParentElements[myParentElements.size() - 2] == SUMO_TAG_RELATION
545 && myCurrentRelation != nullptr) {
546 bool ok = true;
547 std::string key = attrs.getOpt<std::string>(SUMO_ATTR_K, toString(myCurrentRelation).c_str(), ok, "", false);
548 std::string value = attrs.getOpt<std::string>(SUMO_ATTR_V, toString(myCurrentRelation).c_str(), ok, "", false);
549 if (key == "") {
550 myErrorHandler.inform("Empty key in a a tag while parsing way '" + toString(myCurrentRelation) + "' occurred.");
551 ok = false;
552 }
553 if (!ok) {
554 return;
555 }
556 if (key == "name") {
557 myCurrentRelation->name = value;
558 } else if (MyKeysToInclude.count(key) > 0) {
559 myCurrentRelation->keep = true;
560 for (std::vector<long long int>::iterator it = myCurrentWays.begin(); it != myCurrentWays.end(); ++it) {
561 myAdditionalWays[*it] = myCurrentRelation;
562 }
563 }
564 myCurrentRelation->myAttributes[key] = value;
565 }
566}
567
568
569void
571 myParentElements.pop_back();
572 if (element == SUMO_TAG_RELATION) {
573 myCurrentRelation->myWays = myCurrentWays;
574 myCurrentRelation = nullptr;
575 myCurrentWays.clear();
576 }
577}
578
579
580// ---------------------------------------------------------------------------
581// definitions of PCLoaderOSM::EdgesHandler-methods
582// ---------------------------------------------------------------------------
583PCLoaderOSM::EdgesHandler::EdgesHandler(const std::map<long long int, PCOSMNode*>& osmNodes,
584 EdgeMap& toFill,
585 const RelationsMap& additionalWays,
586 bool withAttributes, MsgHandler& errorHandler) :
587 SUMOSAXHandler("osm - file"),
588 myWithAttributes(withAttributes),
589 myErrorHandler(errorHandler),
590 myOSMNodes(osmNodes),
591 myEdgeMap(toFill),
592 myAdditionalWays(additionalWays) {
593}
594
595
598
599
600void
602 myParentElements.push_back(element);
603 // parse "way" elements
604 if (element == SUMO_TAG_WAY) {
605 bool ok = true;
606 const long long int id = attrs.get<long long int>(SUMO_ATTR_ID, nullptr, ok);
607 const std::string& action = attrs.getOpt<std::string>(SUMO_ATTR_ACTION, nullptr, ok);
608 if (action == "delete" || !ok) {
609 myCurrentEdge = nullptr;
610 return;
611 }
612 myCurrentEdge = new PCOSMEdge();
613 myCurrentEdge->id = id;
614 myCurrentEdge->myIsClosed = false;
615 myCurrentEdge->standalone = false;
616 myKeep = (myAdditionalWays.find(id) != myAdditionalWays.end());
617 }
618 // parse "nd" (node) elements
619 if (element == SUMO_TAG_ND && myCurrentEdge != nullptr) {
620 bool ok = true;
621 const long long int ref = attrs.get<long long int>(SUMO_ATTR_REF, nullptr, ok);
622 if (ok) {
623 if (myOSMNodes.find(ref) == myOSMNodes.end()) {
624 WRITE_WARNINGF(TL("The referenced geometry information (ref='%') is not known"), ref);
625 return;
626 }
627 myCurrentEdge->myCurrentNodes.push_back(ref);
628 }
629 }
630 // parse values
631 if (element == SUMO_TAG_TAG && myParentElements.size() > 2 && myParentElements[myParentElements.size() - 2] == SUMO_TAG_WAY
632 && myCurrentEdge != nullptr) {
633 bool ok = true;
634 std::string key = attrs.getOpt<std::string>(SUMO_ATTR_K, toString(myCurrentEdge->id).c_str(), ok, "", false);
635 std::string value = attrs.getOpt<std::string>(SUMO_ATTR_V, toString(myCurrentEdge->id).c_str(), ok, "", false);
636 if (key == "") {
637 myErrorHandler.inform("Empty key in a a tag while parsing way '" + toString(myCurrentEdge->id) + "' occurred.");
638 ok = false;
639 }
640 if (!ok) {
641 return;
642 }
643 if (key == "name") {
644 myCurrentEdge->name = value;
645 } else if (MyKeysToInclude.count(key) > 0) {
646 myKeep = true;
647 myCurrentEdge->standalone = true;
648 }
649 myCurrentEdge->myAttributes[key] = value;
650 }
651}
652
653
654void
656 myParentElements.pop_back();
657 if (element == SUMO_TAG_WAY && myCurrentEdge != nullptr) {
658 if (myKeep) {
659 RelationsMap::const_iterator it = myAdditionalWays.find(myCurrentEdge->id);
660 if (it != myAdditionalWays.end()) {
661 myCurrentEdge->myAttributes.insert((*it).second->myAttributes.begin(), (*it).second->myAttributes.end());
662 }
663 myEdgeMap[myCurrentEdge->id] = myCurrentEdge;
664 } else {
665 delete myCurrentEdge;
666 }
667 myCurrentEdge = nullptr;
668 }
669}
670
671
672double
673PCLoaderOSM::mergeClosest(const std::map<long long int, PCOSMNode*>& nodes, std::vector<std::vector<long long int> >& snippets) {
674 double best = std::numeric_limits<double>::max();
675 int best_i = 0;
676 int best_j = 1;
677 bool iFW = true;
678 bool jFW = true;
679
680 for (int i = 0; i < (int)snippets.size(); i++) {
681 for (int j = i + 1; j < (int)snippets.size(); j++) {
682 Position front1(convertNodePosition(nodes.find(snippets[i].front())->second));
683 Position back1(convertNodePosition(nodes.find(snippets[i].back())->second));
684 Position front2(convertNodePosition(nodes.find(snippets[j].front())->second));
685 Position back2(convertNodePosition(nodes.find(snippets[j].back())->second));
686 double dist1 = front1.distanceTo2D(front2);
687 double dist2 = front1.distanceTo2D(back2);
688 double dist3 = back1.distanceTo2D(front2);
689 double dist4 = back1.distanceTo2D(back2);
690 if (dist1 < best) {
691 best = dist1;
692 best_i = i;
693 best_j = j;
694 iFW = false;
695 jFW = true;
696 }
697 if (dist2 < best) {
698 best = dist2;
699 best_i = i;
700 best_j = j;
701 iFW = false;
702 jFW = false;
703 }
704 if (dist3 < best) {
705 best = dist3;
706 best_i = i;
707 best_j = j;
708 iFW = true;
709 jFW = true;
710 }
711 if (dist4 < best) {
712 best = dist4;
713 best_i = i;
714 best_j = j;
715 iFW = true;
716 jFW = false;
717 }
718 }
719 }
720 std::vector<long long int> merged;
721 if (iFW) {
722 merged.insert(merged.end(), snippets[best_i].begin(), snippets[best_i].end());
723 } else {
724 merged.insert(merged.end(), snippets[best_i].rbegin(), snippets[best_i].rend());
725 }
726 if (jFW) {
727 merged.insert(merged.end(), snippets[best_j].begin(), snippets[best_j].end());
728 } else {
729 merged.insert(merged.end(), snippets[best_j].rbegin(), snippets[best_j].rend());
730 }
731 snippets.erase(snippets.begin() + best_j);
732 snippets.erase(snippets.begin() + best_i);
733 snippets.push_back(merged);
734 return best;
735}
736
737/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define WRITE_MESSAGEF(...)
Definition MsgHandler.h:301
#define WRITE_ERRORF(...)
Definition MsgHandler.h:308
#define PROGRESS_BEGIN_TIME_MESSAGE(msg)
Definition MsgHandler.h:304
#define TL(string)
Definition MsgHandler.h:316
#define PROGRESS_TIME_MESSAGE(before)
Definition MsgHandler.h:305
#define TLF(string,...)
Definition MsgHandler.h:318
@ SUMO_TAG_MEMBER
@ SUMO_TAG_ND
@ SUMO_TAG_WAY
@ SUMO_TAG_NODE
alternative definition for junction
@ SUMO_TAG_RELATION
@ SUMO_TAG_TAG
@ SUMO_ATTR_LON
@ SUMO_ATTR_V
@ SUMO_ATTR_REF
@ SUMO_ATTR_LAT
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_ID
@ SUMO_ATTR_ACTION
@ SUMO_ATTR_K
T MAX2(T a, T b)
Definition StdDefs.h:86
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
static bool isReadable(std::string path)
Checks whether the given file is readable.
bool x2cartesian(Position &from, bool includeInBoundary=true)
Converts the given coordinate into a cartesian and optionally update myConvBoundary.
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
A storage for options typed value containers)
Definition OptionsCont.h:92
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
bool isInStringVector(const std::string &optionName, const std::string &itemName) const
Returns the named option is a list of string values containing the specified item.
A class which extracts OSM-edges from a parsed OSM-file.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void myEndElement(int element)
Called when a closing tag occurs.
EdgesHandler(const std::map< long long int, PCOSMNode * > &osmNodes, EdgeMap &toFill, const RelationsMap &additionalWays, bool withAttributes, MsgHandler &errorHandler)
Constructor.
A class which extracts OSM-nodes from a parsed OSM-file.
NodesHandler(std::map< long long int, PCOSMNode * > &toFill, bool withAttributes, MsgHandler &errorHandler)
Contructor.
void myEndElement(int element)
Called when a closing tag occurs.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
A class which extracts relevant way-ids from relations in a parsed OSM-file.
void myEndElement(int element)
Called when a closing tag occurs.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
RelationsHandler(RelationsMap &additionalWays, Relations &relations, std::set< long long int > &innerEdges, bool withAttributes, MsgHandler &errorHandler)
Constructor.
std::map< long long int, PCOSMEdge * > EdgeMap
static Position convertNodePosition(PCOSMNode *n)
retrieve cartesian coordinate for given node
static int addPOI(const PCOSMNode *node, const Position &pos, const PCTypeMap::TypeDef &def, const std::string &fullType, int index, bool useName, PCPolyContainer &toFill, bool ignorePruning, bool withAttributes)
try add the POI and return the next index on success
static double mergeClosest(const std::map< long long int, PCOSMNode * > &nodes, std::vector< std::vector< long long int > > &snippets)
static std::set< std::string > MyDiscardOverrides
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as OSM-XML.
std::vector< PCOSMRelation * > Relations
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, const std::string &toDiscard)
try add the polygon and return the next index on success
static const std::set< std::string > MyKeysToInclude
static bool myFullType
Whether the type id or full type should be used.
std::map< long long int, PCOSMRelation * > RelationsMap
static double parseHeight(const std::map< std::string, std::string > &attrs, long long int id)
static std::set< std::string > initMyKeysToInclude()
A storage for loaded polygons and pois.
bool add(SUMOPolygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
A storage for type mappings.
Definition PCTypeMap.h:42
const TypeDef & getDefault()
get the default type according to the given options
Definition PCTypeMap.h:116
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition PCTypeMap.cpp:70
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition PCTypeMap.cpp:76
void updateParameters(const Parameterised::Map &mapArg)
Adds or updates all given parameters from the map.
A point-of-interest.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition Position.h:273
A list of positions.
void push_back_noDoublePos(const Position &p)
insert in back a non double position
Encapsulated SAX-Attributes.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
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 hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAX-handler base for SUMO-files.
static const double DEFAULT_HEIGHT
Definition Shape.h:56
static const std::string DEFAULT_IMG_FILE
Definition Shape.h:53
static const double DEFAULT_ANGLE
Definition Shape.h:52
static const std::string DEFAULT_NAME
Definition Shape.h:57
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static double parseDist(const std::string &sData)
parse a distance, length or width value with a unit
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
unsigned node
An internal definition of a loaded edge.
bool myIsClosed
Information whether this area is closed.
long long int id
The edge's id.
std::map< std::string, std::string > myAttributes
Additional attributes.
std::vector< long long int > myCurrentNodes
The list of nodes this edge is made of.
std::string name
The edge's name (if any)
An internal representation of an OSM-node.
Definition PCLoaderOSM.h:68
double lat
The latitude the node is located at.
Definition PCLoaderOSM.h:74
double lon
The longitude the node is located at.
Definition PCLoaderOSM.h:72
long long int id
The node's id.
Definition PCLoaderOSM.h:70
std::map< std::string, std::string > myAttributes
Additional attributes.
Definition PCLoaderOSM.h:78
An internal definition of a loaded relation.
Definition PCLoaderOSM.h:84
A single definition of values that shall be used for a given type.
Definition PCTypeMap.h:61
std::string icon
the icon to use
Definition PCTypeMap.h:69
bool discard
Information whether polygons of this type shall be discarded.
Definition PCTypeMap.h:77
std::string prefix
The prefix to use.
Definition PCTypeMap.h:67
double layer
The layer to use.
Definition PCTypeMap.h:71
std::string id
The new type id to use.
Definition PCTypeMap.h:63
RGBColor color
The color to use.
Definition PCTypeMap.h:65
Filltype allowFill
Information whether polygons of this type can be filled.
Definition PCTypeMap.h:79