/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file PCLoaderOSM.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @author Melanie Knocke18/// @date Wed, 19.11.200819///20// A reader of pois and polygons stored in OSM-format21/****************************************************************************/22#pragma once23#include <config.h>2425#include <string>26#include "PCPolyContainer.h"27#include "PCTypeMap.h"28#include <utils/xml/SUMOSAXHandler.h>293031// ===========================================================================32// class definitions33// ===========================================================================34class OptionsCont;353637// ===========================================================================38// class declarations39// ===========================================================================40/**41* @class PCLoaderOSM42* @brief A reader of pois and polygons stored in OSM-format43*44* Reads pois stored as XML definition as given by the OpenStreetMap-API.45*/46class PCLoaderOSM : public SUMOSAXHandler {47public:48/** @brief Loads pois/polygons assumed to be stored as OSM-XML49*50* If the option "osm-files" is set within the given options container,51* an instance of PCLoaderOSM is built and used as a handler for the52* files given in this option.53*54* @param[in] oc The options container to get further options from55* @param[in] toFill The poly/pois container to add loaded polys/pois to56* @param[in] tm The type map to use for setting values of loaded polys/pois57* @exception ProcessError if something fails58*/59static void loadIfSet(OptionsCont& oc, PCPolyContainer& toFill,60PCTypeMap& tm);616263protected:6465/** @brief An internal representation of an OSM-node66*/67struct PCOSMNode {68/// @brief The node's id69long long int id;70/// @brief The longitude the node is located at71double lon;72/// @brief The latitude the node is located at73double lat;74/// @brief The nodes name (if any)75std::string name;76/// @brief Additional attributes77std::map<std::string, std::string> myAttributes;78};798081/** @brief An internal definition of a loaded relation82*/83struct PCOSMRelation {84/// @brief The relation's id85long long int id;86/// @brief The relation's name (if any)87std::string name;88/// @brief The list of ways this relation is made of89std::vector<long long int> myWays;90/// @brief Additional attributes91std::map<std::string, std::string> myAttributes;92/// @brief whether this relation is a valid polygon93bool keep;94};959697/** @brief An internal definition of a loaded edge98*/99struct PCOSMEdge {100/// @brief The edge's id101long long int id;102/// @brief The edge's name (if any)103std::string name;104/// @brief Information whether this area is closed105bool myIsClosed;106/// @brief The list of nodes this edge is made of107std::vector<long long int> myCurrentNodes;108/// @brief Additional attributes109std::map<std::string, std::string> myAttributes;110// @brief Wether this way constitutes a complete polygon object111bool standalone;112};113114typedef std::vector<PCOSMRelation*> Relations;115typedef std::map<long long int, PCOSMRelation*> RelationsMap;116typedef std::map<long long int, PCOSMEdge*> EdgeMap;117118protected:119/// @brief try add the polygon and return the next index on success120static int addPolygon(const PCOSMEdge* edge, const PositionVector& vec, const PCTypeMap::TypeDef& def,121const std::string& fullType, int index, bool useName, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes);122123/// @brief try add the POI and return the next index on success124static int addPOI(const PCOSMNode* node, const Position& pos, const PCTypeMap::TypeDef& def,125const std::string& fullType, int index, bool useName, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes);126127128protected:129static const std::set<std::string> MyKeysToInclude;130131private:132static std::set<std::string> initMyKeysToInclude();133134/// @brief retrieve cartesian coordinate for given node135static Position convertNodePosition(PCOSMNode* n);136137static double mergeClosest(const std::map<long long int, PCOSMNode*>& nodes, std::vector<std::vector<long long int> >& snippets);138139protected:140/**141* @class NodesHandler142* @brief A class which extracts OSM-nodes from a parsed OSM-file143*/144class NodesHandler : public SUMOSAXHandler {145public:146/** @brief Contructor147* @param[in] toFill The nodes container to fill148* @param[in] withAttributes Whether all attributes shall be stored149* @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)150*/151NodesHandler(std::map<long long int, PCOSMNode*>& toFill, bool withAttributes,152MsgHandler& errorHandler);153154155/// @brief Destructor156~NodesHandler();157158159protected:160/// @name inherited from GenericSAXHandler161//@{162163/** @brief Called on the opening of a tag;164*165* @param[in] element ID of the currently opened element166* @param[in] attrs Attributes within the currently opened element167* @exception ProcessError If something fails168* @see GenericSAXHandler::myStartElement169*/170void myStartElement(int element, const SUMOSAXAttributes& attrs);171172173/** @brief Called when a closing tag occurs174*175* @param[in] element ID of the currently opened element176* @exception ProcessError If something fails177* @see GenericSAXHandler::myEndElement178*/179void myEndElement(int element);180//@}181182183private:184/// @brief Whether all attributes shall be stored185bool myWithAttributes;186187/// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)188MsgHandler& myErrorHandler;189190/// @brief The nodes container to fill191std::map<long long int, PCOSMNode*>& myToFill;192193/// @brief Current path in order to know to what occuring values belong194std::vector<int> myParentElements;195196/// @brief The id of the last parsed node197long long int myLastNodeID;198199private:200/// @brief Invalidated copy constructor201NodesHandler(const NodesHandler& s);202203/// @brief Invalidated assignment operator204NodesHandler& operator=(const NodesHandler& s);205206};207208/**209* @class RelationsHandler210* @brief A class which extracts relevant way-ids from relations in a parsed OSM-file211*/212class RelationsHandler : public SUMOSAXHandler {213public:214/** @brief Constructor215*216* @param[in] osmNodes The previously parsed (osm-)nodes217* @param[in] toFill The edges container to fill with read edges218* @param[in] withAttributes Whether all attributes shall be stored219* @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)220*/221RelationsHandler(RelationsMap& additionalWays,222Relations& relations,223std::set<long long int>& innerEdges,224bool withAttributes,225MsgHandler& errorHandler);226227228/// @brief Destructor229~RelationsHandler();230231232protected:233/// @name inherited from GenericSAXHandler234//@{235236/** @brief Called on the opening of a tag;237*238* @param[in] element ID of the currently opened element239* @param[in] attrs Attributes within the currently opened element240* @exception ProcessError If something fails241* @see GenericSAXHandler::myStartElement242*/243void myStartElement(int element, const SUMOSAXAttributes& attrs);244245246/** @brief Called when a closing tag occurs247*248* @param[in] element ID of the currently opened element249* @exception ProcessError If something fails250* @see GenericSAXHandler::myEndElement251*/252void myEndElement(int element);253//@}254255256private:257/// @brief additional ways which are reference by relations258RelationsMap& myAdditionalWays;259260/// @brief the loaded relations261Relations& myRelations;262263/// @brief the loaded edges264std::set<long long int>& myInnerEdges;265266/// @brief Whether all attributes shall be stored267bool myWithAttributes;268269/// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)270MsgHandler& myErrorHandler;271272/// @brief The currently parsed relation273PCOSMRelation* myCurrentRelation;274275/// @brief the ways within the current relation276std::vector<long long int> myCurrentWays;277278/// @brief Current path in order to know to what occuring values belong279std::vector<long long int> myParentElements;280281/// @brief whether the last edge (way) should be kept because it had a key from the inclusion list282bool myKeep;283284private:285/// @brief Invalidated copy constructor286RelationsHandler(const RelationsHandler& s);287288/// @brief Invalidated assignment operator289RelationsHandler& operator=(const RelationsHandler& s);290291};292293294/**295* @class EdgesHandler296* @brief A class which extracts OSM-edges from a parsed OSM-file297*/298class EdgesHandler : public SUMOSAXHandler {299public:300/** @brief Constructor301*302* @param[in] osmNodes The previously parsed (osm-)nodes303* @param[in] toFill The edges container to fill with read edges304* @param[in] withAttributes Whether all attributes shall be stored305* @param[in] additionalWays Additional ways which were identified as polygons to import306* @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)307*/308EdgesHandler(const std::map<long long int, PCOSMNode*>& osmNodes,309EdgeMap& toFill,310const RelationsMap& additionalWays,311bool withAttributes,312MsgHandler& errorHandler);313314315/// @brief Destructor316~EdgesHandler();317318319protected:320/// @name inherited from GenericSAXHandler321//@{322323/** @brief Called on the opening of a tag;324*325* @param[in] element ID of the currently opened element326* @param[in] attrs Attributes within the currently opened element327* @exception ProcessError If something fails328* @see GenericSAXHandler::myStartElement329*/330void myStartElement(int element, const SUMOSAXAttributes& attrs);331332333/** @brief Called when a closing tag occurs334*335* @param[in] element ID of the currently opened element336* @exception ProcessError If something fails337* @see GenericSAXHandler::myEndElement338*/339void myEndElement(int element);340//@}341342343private:344/// @brief Whether all attributes shall be stored345bool myWithAttributes;346347/// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)348MsgHandler& myErrorHandler;349350/// @brief The previously parsed nodes351const std::map<long long int, PCOSMNode*>& myOSMNodes;352353/// @brief A map of built edges354EdgeMap& myEdgeMap;355356/// @brief additional ways which are reference by relations357const RelationsMap& myAdditionalWays;358359/// @brief The currently built edge360PCOSMEdge* myCurrentEdge;361362/// @brief Current path in order to know to what occuring values belong363std::vector<int> myParentElements;364365/// @brief whether the last edge (way) should be kept because it had a key from the inclusion list366bool myKeep;367368private:369/// @brief Invalidated copy constructor370EdgesHandler(const EdgesHandler& s);371372/// @brief Invalidated assignment operator373EdgesHandler& operator=(const EdgesHandler& s);374375};376377};378379380