Line data Source code
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 : /****************************************************************************/
14 : /// @file PCLoaderOSM.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @author Melanie Knocke
19 : /// @date Wed, 19.11.2008
20 : ///
21 : // A reader of pois and polygons stored in OSM-format
22 : /****************************************************************************/
23 : #pragma once
24 : #include <config.h>
25 :
26 : #include <string>
27 : #include "PCPolyContainer.h"
28 : #include "PCTypeMap.h"
29 : #include <utils/xml/SUMOSAXHandler.h>
30 :
31 :
32 : // ===========================================================================
33 : // class definitions
34 : // ===========================================================================
35 : class OptionsCont;
36 :
37 :
38 : // ===========================================================================
39 : // class declarations
40 : // ===========================================================================
41 : /**
42 : * @class PCLoaderOSM
43 : * @brief A reader of pois and polygons stored in OSM-format
44 : *
45 : * Reads pois stored as XML definition as given by the OpenStreetMap-API.
46 : */
47 : class PCLoaderOSM : public SUMOSAXHandler {
48 : public:
49 : /** @brief Loads pois/polygons assumed to be stored as OSM-XML
50 : *
51 : * If the option "osm-files" is set within the given options container,
52 : * an instance of PCLoaderOSM is built and used as a handler for the
53 : * files given in this option.
54 : *
55 : * @param[in] oc The options container to get further options from
56 : * @param[in] toFill The poly/pois container to add loaded polys/pois to
57 : * @param[in] tm The type map to use for setting values of loaded polys/pois
58 : * @exception ProcessError if something fails
59 : */
60 : static void loadIfSet(OptionsCont& oc, PCPolyContainer& toFill,
61 : PCTypeMap& tm);
62 :
63 :
64 : protected:
65 :
66 : /** @brief An internal representation of an OSM-node
67 : */
68 41718 : struct PCOSMNode {
69 : /// @brief The node's id
70 : long long int id;
71 : /// @brief The longitude the node is located at
72 : double lon;
73 : /// @brief The latitude the node is located at
74 : double lat;
75 : /// @brief The nodes name (if any)
76 : std::string name;
77 : /// @brief Additional attributes
78 : std::map<std::string, std::string> myAttributes;
79 : };
80 :
81 :
82 : /** @brief An internal definition of a loaded relation
83 : */
84 : struct PCOSMRelation {
85 : /// @brief The relation's id
86 : long long int id;
87 : /// @brief The relation's name (if any)
88 : std::string name;
89 : /// @brief The list of ways this relation is made of
90 : std::vector<long long int> myWays;
91 : /// @brief Additional attributes
92 : std::map<std::string, std::string> myAttributes;
93 : /// @brief whether this relation is a valid polygon
94 : bool keep;
95 : };
96 :
97 :
98 : /** @brief An internal definition of a loaded edge
99 : */
100 : struct PCOSMEdge {
101 : /// @brief The edge's id
102 : long long int id;
103 : /// @brief The edge's name (if any)
104 : std::string name;
105 : /// @brief Information whether this area is closed
106 : bool myIsClosed;
107 : /// @brief The list of nodes this edge is made of
108 : std::vector<long long int> myCurrentNodes;
109 : /// @brief Additional attributes
110 : std::map<std::string, std::string> myAttributes;
111 : // @brief Wether this way constitutes a complete polygon object
112 : bool standalone;
113 : };
114 :
115 : typedef std::vector<PCOSMRelation*> Relations;
116 : typedef std::map<long long int, PCOSMRelation*> RelationsMap;
117 : typedef std::map<long long int, PCOSMEdge*> EdgeMap;
118 :
119 : protected:
120 : /// @brief try add the polygon and return the next index on success
121 : static int addPolygon(const PCOSMEdge* edge, const PositionVector& vec, const PCTypeMap::TypeDef& def,
122 : const std::string& fullType, int index, bool useName, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes);
123 :
124 : /// @brief try add the POI and return the next index on success
125 : static int addPOI(const PCOSMNode* node, const Position& pos, const PCTypeMap::TypeDef& def,
126 : const std::string& fullType, int index, bool useName, PCPolyContainer& toFill, bool ignorePruning, bool withAttributes);
127 :
128 :
129 : protected:
130 : static const std::set<std::string> MyKeysToInclude;
131 :
132 : private:
133 : static std::set<std::string> initMyKeysToInclude();
134 :
135 : /// @brief retrieve cartesian coordinate for given node
136 : static Position convertNodePosition(PCOSMNode* n);
137 :
138 : static double mergeClosest(const std::map<long long int, PCOSMNode*>& nodes, std::vector<std::vector<long long int> >& snippets);
139 :
140 : static double parseHeight(const std::map<std::string, std::string>& attrs, long long int id);
141 : protected:
142 : /**
143 : * @class NodesHandler
144 : * @brief A class which extracts OSM-nodes from a parsed OSM-file
145 : */
146 : class NodesHandler : public SUMOSAXHandler {
147 : public:
148 : /** @brief Contructor
149 : * @param[in] toFill The nodes container to fill
150 : * @param[in] withAttributes Whether all attributes shall be stored
151 : * @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)
152 : */
153 : NodesHandler(std::map<long long int, PCOSMNode*>& toFill, bool withAttributes,
154 : MsgHandler& errorHandler);
155 :
156 :
157 : /// @brief Destructor
158 : ~NodesHandler();
159 :
160 :
161 : protected:
162 : /// @name inherited from GenericSAXHandler
163 : //@{
164 :
165 : /** @brief Called on the opening of a tag;
166 : *
167 : * @param[in] element ID of the currently opened element
168 : * @param[in] attrs Attributes within the currently opened element
169 : * @exception ProcessError If something fails
170 : * @see GenericSAXHandler::myStartElement
171 : */
172 : void myStartElement(int element, const SUMOSAXAttributes& attrs);
173 :
174 :
175 : /** @brief Called when a closing tag occurs
176 : *
177 : * @param[in] element ID of the currently opened element
178 : * @exception ProcessError If something fails
179 : * @see GenericSAXHandler::myEndElement
180 : */
181 : void myEndElement(int element);
182 : //@}
183 :
184 :
185 : private:
186 : /// @brief Whether all attributes shall be stored
187 : bool myWithAttributes;
188 :
189 : /// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)
190 : MsgHandler& myErrorHandler;
191 :
192 : /// @brief The nodes container to fill
193 : std::map<long long int, PCOSMNode*>& myToFill;
194 :
195 : /// @brief Current path in order to know to what occuring values belong
196 : std::vector<int> myParentElements;
197 :
198 : /// @brief The id of the last parsed node
199 : long long int myLastNodeID;
200 :
201 : private:
202 : /// @brief Invalidated copy constructor
203 : NodesHandler(const NodesHandler& s);
204 :
205 : /// @brief Invalidated assignment operator
206 : NodesHandler& operator=(const NodesHandler& s);
207 :
208 : };
209 :
210 : /**
211 : * @class RelationsHandler
212 : * @brief A class which extracts relevant way-ids from relations in a parsed OSM-file
213 : */
214 : class RelationsHandler : public SUMOSAXHandler {
215 : public:
216 : /** @brief Constructor
217 : *
218 : * @param[in] osmNodes The previously parsed (osm-)nodes
219 : * @param[in] toFill The edges container to fill with read edges
220 : * @param[in] withAttributes Whether all attributes shall be stored
221 : * @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)
222 : */
223 : RelationsHandler(RelationsMap& additionalWays,
224 : Relations& relations,
225 : std::set<long long int>& innerEdges,
226 : bool withAttributes,
227 : MsgHandler& errorHandler);
228 :
229 :
230 : /// @brief Destructor
231 : ~RelationsHandler();
232 :
233 :
234 : protected:
235 : /// @name inherited from GenericSAXHandler
236 : //@{
237 :
238 : /** @brief Called on the opening of a tag;
239 : *
240 : * @param[in] element ID of the currently opened element
241 : * @param[in] attrs Attributes within the currently opened element
242 : * @exception ProcessError If something fails
243 : * @see GenericSAXHandler::myStartElement
244 : */
245 : void myStartElement(int element, const SUMOSAXAttributes& attrs);
246 :
247 :
248 : /** @brief Called when a closing tag occurs
249 : *
250 : * @param[in] element ID of the currently opened element
251 : * @exception ProcessError If something fails
252 : * @see GenericSAXHandler::myEndElement
253 : */
254 : void myEndElement(int element);
255 : //@}
256 :
257 :
258 : private:
259 : /// @brief additional ways which are reference by relations
260 : RelationsMap& myAdditionalWays;
261 :
262 : /// @brief the loaded relations
263 : Relations& myRelations;
264 :
265 : /// @brief the loaded edges
266 : std::set<long long int>& myInnerEdges;
267 :
268 : /// @brief Whether all attributes shall be stored
269 : bool myWithAttributes;
270 :
271 : /// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)
272 : MsgHandler& myErrorHandler;
273 :
274 : /// @brief The currently parsed relation
275 : PCOSMRelation* myCurrentRelation;
276 :
277 : /// @brief the ways within the current relation
278 : std::vector<long long int> myCurrentWays;
279 :
280 : /// @brief Current path in order to know to what occuring values belong
281 : std::vector<long long int> myParentElements;
282 :
283 : /// @brief whether the last edge (way) should be kept because it had a key from the inclusion list
284 : bool myKeep;
285 :
286 : private:
287 : /// @brief Invalidated copy constructor
288 : RelationsHandler(const RelationsHandler& s);
289 :
290 : /// @brief Invalidated assignment operator
291 : RelationsHandler& operator=(const RelationsHandler& s);
292 :
293 : };
294 :
295 :
296 : /**
297 : * @class EdgesHandler
298 : * @brief A class which extracts OSM-edges from a parsed OSM-file
299 : */
300 : class EdgesHandler : public SUMOSAXHandler {
301 : public:
302 : /** @brief Constructor
303 : *
304 : * @param[in] osmNodes The previously parsed (osm-)nodes
305 : * @param[in] toFill The edges container to fill with read edges
306 : * @param[in] withAttributes Whether all attributes shall be stored
307 : * @param[in] additionalWays Additional ways which were identified as polygons to import
308 : * @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)
309 : */
310 : EdgesHandler(const std::map<long long int, PCOSMNode*>& osmNodes,
311 : EdgeMap& toFill,
312 : const RelationsMap& additionalWays,
313 : bool withAttributes,
314 : MsgHandler& errorHandler);
315 :
316 :
317 : /// @brief Destructor
318 : ~EdgesHandler();
319 :
320 :
321 : protected:
322 : /// @name inherited from GenericSAXHandler
323 : //@{
324 :
325 : /** @brief Called on the opening of a tag;
326 : *
327 : * @param[in] element ID of the currently opened element
328 : * @param[in] attrs Attributes within the currently opened element
329 : * @exception ProcessError If something fails
330 : * @see GenericSAXHandler::myStartElement
331 : */
332 : void myStartElement(int element, const SUMOSAXAttributes& attrs);
333 :
334 :
335 : /** @brief Called when a closing tag occurs
336 : *
337 : * @param[in] element ID of the currently opened element
338 : * @exception ProcessError If something fails
339 : * @see GenericSAXHandler::myEndElement
340 : */
341 : void myEndElement(int element);
342 : //@}
343 :
344 :
345 : private:
346 : /// @brief Whether all attributes shall be stored
347 : bool myWithAttributes;
348 :
349 : /// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)
350 : MsgHandler& myErrorHandler;
351 :
352 : /// @brief The previously parsed nodes
353 : const std::map<long long int, PCOSMNode*>& myOSMNodes;
354 :
355 : /// @brief A map of built edges
356 : EdgeMap& myEdgeMap;
357 :
358 : /// @brief additional ways which are reference by relations
359 : const RelationsMap& myAdditionalWays;
360 :
361 : /// @brief The currently built edge
362 : PCOSMEdge* myCurrentEdge;
363 :
364 : /// @brief Current path in order to know to what occuring values belong
365 : std::vector<int> myParentElements;
366 :
367 : /// @brief whether the last edge (way) should be kept because it had a key from the inclusion list
368 : bool myKeep;
369 :
370 : private:
371 : /// @brief Invalidated copy constructor
372 : EdgesHandler(const EdgesHandler& s);
373 :
374 : /// @brief Invalidated assignment operator
375 : EdgesHandler& operator=(const EdgesHandler& s);
376 :
377 : };
378 :
379 : };
|