libosmscout  1.1.1
RoutePostprocessor.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_ROUTEPOSTPROCESSOR_H
2 #define OSMSCOUT_ROUTEPOSTPROCESSOR_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2012 Tim Teulings
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
23 #include <list>
24 #include <map>
25 #include <memory>
26 #include <unordered_map>
27 
28 #include <osmscout/CoreFeatures.h>
29 
30 #include <osmscout/TypeConfig.h>
31 #include <osmscout/TypeFeatures.h>
32 
33 #include <osmscout/util/Time.h>
34 
35 // Database
36 #include <osmscout/Database.h>
37 
38 // Routing
42 
43 namespace osmscout {
44 
56  {
57  public:
64  {
65  public:
66  virtual ~Postprocessor() = default;
67 
68  virtual bool Process(const RoutePostprocessor& postprocessor,
69  RouteDescription& description) = 0;
70  };
71 
72  using PostprocessorRef = std::shared_ptr<Postprocessor>;
73 
79  {
80  private:
81  std::string startDescription;
82 
83  public:
84  explicit StartPostprocessor(const std::string& startDescription);
85 
86  bool Process(const RoutePostprocessor& postprocessor,
87  RouteDescription& description) override;
88  };
89 
95  {
96  private:
97  std::string targetDescription;
98 
99  public:
100  explicit TargetPostprocessor(const std::string& targetDescription);
101 
102  bool Process(const RoutePostprocessor& postprocessor,
103  RouteDescription& description) override;
104  };
105 
111  {
112  public:
113  DistanceAndTimePostprocessor() = default;
114 
115  bool Process(const RoutePostprocessor& postprocessor,
116  RouteDescription& description) override;
117  };
118 
124  {
125  public:
126  WayNamePostprocessor() = default;
127 
128  bool Process(const RoutePostprocessor& postprocessor,
129  RouteDescription& description) override;
130  };
131 
137  {
138  public:
139  WayTypePostprocessor() = default;
140 
141  bool Process(const RoutePostprocessor& postprocessor,
142  RouteDescription& description) override;
143  };
144 
150  {
151  private:
152  void AddCrossingWaysDescriptions(const RoutePostprocessor& postprocessor,
154  const RouteDescription::Node& node,
155  const ObjectFileRef& originObject,
156  const ObjectFileRef& targetObject);
157 
158  public:
159  CrossingWaysPostprocessor() = default;
160 
161  bool Process(const RoutePostprocessor& postprocessor,
162  RouteDescription& description) override;
163  };
164 
170  {
171  private:
172  static const double curveMinInitialAngle;
173  static const double curveMaxInitialAngle;
174  static const Distance curveMaxNodeDistance;
175  static const Distance curveMaxDistance;
176  static const double curveMinAngle;
177 
178  public:
179  DirectionPostprocessor() = default;
180 
181  bool Process(const RoutePostprocessor& postprocessor,
182  RouteDescription& description) override;
183  };
184 
190  {
191  public:
192  MotorwayJunctionPostprocessor() = default;
193 
194  bool Process(const RoutePostprocessor& postprocessor,
195  RouteDescription& description) override;
196  };
197 
203  {
204  public:
205  DestinationPostprocessor() = default;
206 
207  bool Process(const RoutePostprocessor& postprocessor,
208  RouteDescription& description) override;
209  };
210 
216  {
217  public:
218  MaxSpeedPostprocessor() = default;
219 
220  bool Process(const RoutePostprocessor& postprocessor,
221  RouteDescription& description) override;
222  };
223 
229  {
230  private:
231  enum State {
232  street,
233  roundabout,
234  motorway,
235  link
236  };
237 
238  private:
239 
240  bool inRoundabout;
241  size_t roundaboutCrossingCounter;
242  bool roundaboutClockwise{false};
243 
244  private:
245  State GetInitialState(const RoutePostprocessor& postprocessor,
246  RouteDescription::Node& node);
247 
248  void HandleRoundaboutEnter(const RoutePostprocessor& postprocessor, RouteDescription::Node& node);
249  void HandleRoundaboutNode(RouteDescription::Node& node);
250  void HandleRoundaboutLeave(RouteDescription::Node& node);
251  void HandleDirectMotorwayEnter(RouteDescription::Node& node,
253  void HandleDirectMotorwayLeave(RouteDescription::Node& node,
254  const RouteDescription::NameDescriptionRef& fromName);
255  bool HandleNameChange(const std::list<RouteDescription::Node>& path,
256  std::list<RouteDescription::Node>::const_iterator& lastNode,
257  std::list<RouteDescription::Node>::iterator& node);
258  bool HandleDirectionChange(const std::list<RouteDescription::Node>& path,
259  std::list<RouteDescription::Node>::iterator& node);
260 
261  public:
262  bool Process(const RoutePostprocessor& postprocessor,
263  RouteDescription& description) override;
264 
265  };
266 
267  using InstructionPostprocessorRef = std::shared_ptr<InstructionPostprocessor>;
268 
274  {
275  private:
276  struct POIAtRoute
277  {
278  ObjectFileRef object;
280  Distance distance;
281  std::list<RouteDescription::Node>::iterator node;
282  };
283 
284  private:
285  std::set<ObjectFileRef> CollectPaths(const std::list<RouteDescription::Node>& nodes) const;
286  std::list<WayRef> CollectWays(const RoutePostprocessor& postprocessor,
287  const std::list<RouteDescription::Node>& nodes) const;
288  std::list<AreaRef> CollectAreas(const RoutePostprocessor& postprocessor,
289  const std::list<RouteDescription::Node>& nodes) const;
290  std::map<ObjectFileRef,std::set<ObjectFileRef>> CollectPOICandidates(const Database& database,
291  const std::set<ObjectFileRef>& paths,
292  const std::list<WayRef>& ways,
293  const std::list<AreaRef>& areas);
294  std::map<ObjectFileRef,POIAtRoute> AnalysePOICandidates(const RoutePostprocessor& postprocessor,
295  const DatabaseId& databaseId,
296  std::list<RouteDescription::Node>& nodes,
297  const TypeInfoSet& nodeTypes,
298  const TypeInfoSet& areaTypes,
299  const std::unordered_map<FileOffset,NodeRef>& nodeMap,
300  const std::unordered_map<FileOffset,AreaRef>& areaMap,
301  const std::map<ObjectFileRef,std::set<ObjectFileRef>>& poiCandidates);
302  void SortInCollectedPOIs(const DatabaseId& databaseId,
303  const std::map<ObjectFileRef,POIAtRoute>& pois);
304 
305  public:
306  POIsPostprocessor() = default;
307 
308  bool Process(const RoutePostprocessor& postprocessor,
309  RouteDescription& description) override;
310  };
311 
312  using POIsPostprocessorRef = std::shared_ptr<POIsPostprocessor>;
313 
319  {
320  public:
321  LanesPostprocessor() = default;
322 
323  bool Process(const RoutePostprocessor& postprocessor,
324  RouteDescription& description) override;
325  };
326 
327  using LanesPostprocessorRef = std::shared_ptr<LanesPostprocessor>;
328 
335  {
336  public:
337  SuggestedLanesPostprocessor(const Distance &distanceBefore=Meters(500)) :
338  Postprocessor(), distanceBefore(distanceBefore) {};
339 
340  bool Process(const RoutePostprocessor& postprocessor,
341  RouteDescription& description) override;
342  private:
343  Distance distanceBefore;
344  };
345 
346  using SuggestedLanesPostprocessorRef = std::shared_ptr<SuggestedLanesPostprocessor>;
347 
348  private:
349  std::vector<RoutingProfileRef> profiles;
350  std::vector<DatabaseRef> databases;
351 
352  std::unordered_map<DBFileOffset,AreaRef> areaMap;
353  std::unordered_map<DBFileOffset,WayRef> wayMap;
354 
355  std::unordered_map<DatabaseId,NameFeatureValueReader*> nameReaders;
356  std::unordered_map<DatabaseId,RefFeatureValueReader*> refReaders;
357  std::unordered_map<DatabaseId,BridgeFeatureReader*> bridgeReaders;
358  std::unordered_map<DatabaseId,RoundaboutFeatureReader*> roundaboutReaders;
359  std::unordered_map<DatabaseId,DestinationFeatureValueReader*> destinationReaders;
360  std::unordered_map<DatabaseId,MaxSpeedFeatureValueReader*> maxSpeedReaders;
361  std::unordered_map<DatabaseId,LanesFeatureValueReader*> lanesReaders;
362  std::unordered_map<DatabaseId,AccessFeatureValueReader*> accessReaders;
363 
364  std::unordered_map<DatabaseId,TypeInfoSet> motorwayTypes;
365  std::unordered_map<DatabaseId,TypeInfoSet> motorwayLinkTypes;
366  std::unordered_map<DatabaseId,TypeInfoSet> junctionTypes;
367 
368  private:
369  bool ResolveAllAreasAndWays(const RouteDescription& description,
370  DatabaseId dbId,
371  Database& database);
372  void Cleanup();
373 
374  private:
375  AreaRef GetArea(const DBFileOffset &offset) const;
376  WayRef GetWay(const DBFileOffset &offset) const;
377 
378  Duration GetTime(DatabaseId dbId,const Area& area,const Distance &deltaDistance) const;
379  Duration GetTime(DatabaseId dbId,const Way& way,const Distance &deltaDistance) const;
380 
381  RouteDescription::NameDescriptionRef GetNameDescription(const RouteDescription::Node& node) const;
382  RouteDescription::NameDescriptionRef GetNameDescription(DatabaseId dbId,
383  const ObjectFileRef& object) const;
384  RouteDescription::NameDescriptionRef GetNameDescription(DatabaseId dbId,
385  const Node& node) const;
386  RouteDescription::NameDescriptionRef GetNameDescription(DatabaseId dbId,
387  const Area& area) const;
388  RouteDescription::NameDescriptionRef GetNameDescription(DatabaseId dbId,
389  const Way& way) const;
390 
391  bool LoadJunction(DatabaseId database,
392  GeoCoord coord,
393  std::string junctionRef,
394  std::string junctionName) const;
395 
396  bool IsMotorwayLink(const RouteDescription::Node& node) const;
397  bool IsMotorway(const RouteDescription::Node& node) const;
398 
399  bool IsRoundabout(const RouteDescription::Node& node) const;
400  bool IsBridge(const RouteDescription::Node& node) const;
401 
403 
404  uint8_t GetMaxSpeed(const RouteDescription::Node& node) const;
405 
407 
408  Id GetNodeId(const RouteDescription::Node& node) const;
409 
410  size_t GetNodeIndex(const RouteDescription::Node& node,
411  Id nodeId) const;
412 
413  bool CanUseBackward(const DatabaseId& dbId,
414  Id fromNodeId,
415  const ObjectFileRef& object) const;
416 
417  bool CanUseForward(const DatabaseId& dbId,
418  Id fromNodeId,
419  const ObjectFileRef& object) const;
420 
421  bool IsBackwardPath(const ObjectFileRef& object,
422  size_t fromNodeIndex,
423  size_t toNodeIndex) const;
424 
425  bool IsForwardPath(const ObjectFileRef& object,
426  size_t fromNodeIndex,
427  size_t toNodeIndex) const;
428 
429  bool IsNodeStartOrEndOfObject(const RouteDescription::Node& node,
430  const ObjectFileRef& object) const;
431 
432  GeoCoord GetCoordinates(const RouteDescription::Node& node,
433  size_t nodeIndex) const;
434 
435  public:
436  /*
437  * TODO:
438  * All Postprocessors are allowed to use our internal methods currently.
439  * We should fix this by moving helper methods to a separate
440  * PostprocessorContext object that gets passed to the postprocessors explicitely.
441  * This would also move state out of the RoutePostprocessor itself.
442  */
444 
445  bool PostprocessRouteDescription(RouteDescription& description,
446  const std::vector<RoutingProfileRef>& profiles,
447  const std::vector<DatabaseRef>& databases,
448  const std::list<PostprocessorRef>& processors,
449  const std::set<std::string>& motorwayTypeNames=std::set<std::string>(),
450  const std::set<std::string>& motorwayLinkTypeNames=std::set<std::string>(),
451  const std::set<std::string>& junctionTypeNames=std::set<std::string>());
452  };
453 }
454 
455 #endif
SuggestedLanesPostprocessor(const Distance &distanceBefore=Meters(500))
Definition: RoutePostprocessor.h:337
uint64_t Id
Definition: OSMScoutTypes.h:41
std::shared_ptr< InstructionPostprocessor > InstructionPostprocessorRef
Definition: RoutePostprocessor.h:267
Definition: RoutePostprocessor.h:273
std::shared_ptr< DestinationDescription > DestinationDescriptionRef
Definition: RouteDescription.h:492
std::shared_ptr< SuggestedLanesPostprocessor > SuggestedLanesPostprocessorRef
Definition: RoutePostprocessor.h:346
Definition: RouteDescription.h:667
std::shared_ptr< LanesPostprocessor > LanesPostprocessorRef
Definition: RoutePostprocessor.h:327
Definition: RoutePostprocessor.h:202
Definition: RouteDescription.h:53
Definition: RoutePostprocessor.h:123
std::shared_ptr< Way > WayRef
Definition: Way.h:202
std::shared_ptr< Postprocessor > PostprocessorRef
Definition: RoutePostprocessor.h:72
Definition: RoutePostprocessor.h:55
std::shared_ptr< NameDescription > NameDescriptionRef
Definition: RouteDescription.h:175
std::shared_ptr< POIsPostprocessor > POIsPostprocessorRef
Definition: RoutePostprocessor.h:312
Definition: Area.h:38
Definition: RoutePostprocessor.h:94
friend Postprocessor
Definition: RoutePostprocessor.h:443
Definition: RoutePostprocessor.h:136
std::shared_ptr< Area > AreaRef
Definition: Area.h:358
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
std::shared_ptr< CrossingWaysDescription > CrossingWaysDescriptionRef
Definition: RouteDescription.h:253
std::shared_ptr< LaneDescription > LaneDescriptionRef
Definition: RouteDescription.h:631
Definition: RoutePostprocessor.h:228
Definition: RoutePostprocessor.h:318
Distance Meters(double m)
Definition: Distance.h:358
Definition: RoutePostprocessor.h:63
Timestamp::duration Duration
Definition: Time.h:29
Definition: DBFileOffset.h:92
Definition: RoutePostprocessor.h:169
Definition: RoutePostprocessor.h:78
Definition: RoutePostprocessor.h:215
uint32_t DatabaseId
Definition: DBFileOffset.h:30