Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2008-2024 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 : protected:
141 : /**
142 : * @class NodesHandler
143 : * @brief A class which extracts OSM-nodes from a parsed OSM-file
144 : */
145 : class NodesHandler : public SUMOSAXHandler {
146 : public:
147 : /** @brief Contructor
148 : * @param[in] toFill The nodes container to fill
149 : * @param[in] withAttributes Whether all attributes shall be stored
150 : * @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)
151 : */
152 : NodesHandler(std::map<long long int, PCOSMNode*>& toFill, bool withAttributes,
153 : MsgHandler& errorHandler);
154 :
155 :
156 : /// @brief Destructor
157 : ~NodesHandler();
158 :
159 :
160 : protected:
161 : /// @name inherited from GenericSAXHandler
162 : //@{
163 :
164 : /** @brief Called on the opening of a tag;
165 : *
166 : * @param[in] element ID of the currently opened element
167 : * @param[in] attrs Attributes within the currently opened element
168 : * @exception ProcessError If something fails
169 : * @see GenericSAXHandler::myStartElement
170 : */
171 : void myStartElement(int element, const SUMOSAXAttributes& attrs);
172 :
173 :
174 : /** @brief Called when a closing tag occurs
175 : *
176 : * @param[in] element ID of the currently opened element
177 : * @exception ProcessError If something fails
178 : * @see GenericSAXHandler::myEndElement
179 : */
180 : void myEndElement(int element);
181 : //@}
182 :
183 :
184 : private:
185 : /// @brief Whether all attributes shall be stored
186 : bool myWithAttributes;
187 :
188 : /// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)
189 : MsgHandler& myErrorHandler;
190 :
191 : /// @brief The nodes container to fill
192 : std::map<long long int, PCOSMNode*>& myToFill;
193 :
194 : /// @brief Current path in order to know to what occuring values belong
195 : std::vector<int> myParentElements;
196 :
197 : /// @brief The id of the last parsed node
198 : long long int myLastNodeID;
199 :
200 : private:
201 : /// @brief Invalidated copy constructor
202 : NodesHandler(const NodesHandler& s);
203 :
204 : /// @brief Invalidated assignment operator
205 : NodesHandler& operator=(const NodesHandler& s);
206 :
207 : };
208 :
209 : /**
210 : * @class RelationsHandler
211 : * @brief A class which extracts relevant way-ids from relations in a parsed OSM-file
212 : */
213 : class RelationsHandler : public SUMOSAXHandler {
214 : public:
215 : /** @brief Constructor
216 : *
217 : * @param[in] osmNodes The previously parsed (osm-)nodes
218 : * @param[in] toFill The edges container to fill with read edges
219 : * @param[in] withAttributes Whether all attributes shall be stored
220 : * @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)
221 : */
222 : RelationsHandler(RelationsMap& additionalWays,
223 : Relations& relations,
224 : std::set<long long int>& innerEdges,
225 : bool withAttributes,
226 : MsgHandler& errorHandler);
227 :
228 :
229 : /// @brief Destructor
230 : ~RelationsHandler();
231 :
232 :
233 : protected:
234 : /// @name inherited from GenericSAXHandler
235 : //@{
236 :
237 : /** @brief Called on the opening of a tag;
238 : *
239 : * @param[in] element ID of the currently opened element
240 : * @param[in] attrs Attributes within the currently opened element
241 : * @exception ProcessError If something fails
242 : * @see GenericSAXHandler::myStartElement
243 : */
244 : void myStartElement(int element, const SUMOSAXAttributes& attrs);
245 :
246 :
247 : /** @brief Called when a closing tag occurs
248 : *
249 : * @param[in] element ID of the currently opened element
250 : * @exception ProcessError If something fails
251 : * @see GenericSAXHandler::myEndElement
252 : */
253 : void myEndElement(int element);
254 : //@}
255 :
256 :
257 : private:
258 : /// @brief additional ways which are reference by relations
259 : RelationsMap& myAdditionalWays;
260 :
261 : /// @brief the loaded relations
262 : Relations& myRelations;
263 :
264 : /// @brief the loaded edges
265 : std::set<long long int>& myInnerEdges;
266 :
267 : /// @brief Whether all attributes shall be stored
268 : bool myWithAttributes;
269 :
270 : /// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)
271 : MsgHandler& myErrorHandler;
272 :
273 : /// @brief The currently parsed relation
274 : PCOSMRelation* myCurrentRelation;
275 :
276 : /// @brief the ways within the current relation
277 : std::vector<long long int> myCurrentWays;
278 :
279 : /// @brief Current path in order to know to what occuring values belong
280 : std::vector<long long int> myParentElements;
281 :
282 : /// @brief whether the last edge (way) should be kept because it had a key from the inclusion list
283 : bool myKeep;
284 :
285 : private:
286 : /// @brief Invalidated copy constructor
287 : RelationsHandler(const RelationsHandler& s);
288 :
289 : /// @brief Invalidated assignment operator
290 : RelationsHandler& operator=(const RelationsHandler& s);
291 :
292 : };
293 :
294 :
295 : /**
296 : * @class EdgesHandler
297 : * @brief A class which extracts OSM-edges from a parsed OSM-file
298 : */
299 : class EdgesHandler : public SUMOSAXHandler {
300 : public:
301 : /** @brief Constructor
302 : *
303 : * @param[in] osmNodes The previously parsed (osm-)nodes
304 : * @param[in] toFill The edges container to fill with read edges
305 : * @param[in] withAttributes Whether all attributes shall be stored
306 : * @param[in] additionalWays Additional ways which were identified as polygons to import
307 : * @param[in] errorHandler The handler to report errors to (WarningHandler for ignoring errors)
308 : */
309 : EdgesHandler(const std::map<long long int, PCOSMNode*>& osmNodes,
310 : EdgeMap& toFill,
311 : const RelationsMap& additionalWays,
312 : bool withAttributes,
313 : MsgHandler& errorHandler);
314 :
315 :
316 : /// @brief Destructor
317 : ~EdgesHandler();
318 :
319 :
320 : protected:
321 : /// @name inherited from GenericSAXHandler
322 : //@{
323 :
324 : /** @brief Called on the opening of a tag;
325 : *
326 : * @param[in] element ID of the currently opened element
327 : * @param[in] attrs Attributes within the currently opened element
328 : * @exception ProcessError If something fails
329 : * @see GenericSAXHandler::myStartElement
330 : */
331 : void myStartElement(int element, const SUMOSAXAttributes& attrs);
332 :
333 :
334 : /** @brief Called when a closing tag occurs
335 : *
336 : * @param[in] element ID of the currently opened element
337 : * @exception ProcessError If something fails
338 : * @see GenericSAXHandler::myEndElement
339 : */
340 : void myEndElement(int element);
341 : //@}
342 :
343 :
344 : private:
345 : /// @brief Whether all attributes shall be stored
346 : bool myWithAttributes;
347 :
348 : /// @brief The handler to report errors to (will be the WarningsHandler if --ignore-errors was set)
349 : MsgHandler& myErrorHandler;
350 :
351 : /// @brief The previously parsed nodes
352 : const std::map<long long int, PCOSMNode*>& myOSMNodes;
353 :
354 : /// @brief A map of built edges
355 : EdgeMap& myEdgeMap;
356 :
357 : /// @brief additional ways which are reference by relations
358 : const RelationsMap& myAdditionalWays;
359 :
360 : /// @brief The currently built edge
361 : PCOSMEdge* myCurrentEdge;
362 :
363 : /// @brief Current path in order to know to what occuring values belong
364 : std::vector<int> myParentElements;
365 :
366 : /// @brief whether the last edge (way) should be kept because it had a key from the inclusion list
367 : bool myKeep;
368 :
369 : private:
370 : /// @brief Invalidated copy constructor
371 : EdgesHandler(const EdgesHandler& s);
372 :
373 : /// @brief Invalidated assignment operator
374 : EdgesHandler& operator=(const EdgesHandler& s);
375 :
376 : };
377 :
378 : };
|