libosmscout  1.1.1
DataAgent.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_NAVIGATION_DATA_AGENT_H
2 #define OSMSCOUT_NAVIGATION_DATA_AGENT_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2018 Lukas Karas
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 
25 
26 #include <list>
27 
28 namespace osmscout {
29 
31  {
33  std::map<FileOffset,WayRef> ways;
34  std::map<FileOffset,AreaRef> areas;
35  };
36 
38  {
39  std::map<DatabaseId, RoutableDBObjects> dbMap;
40  GeoBox bbox;
41 
42  TypeConfigRef GetTypeConfig(const DatabaseId &dbId) const;
43 
44  WayRef GetWay(const DatabaseId &dbId, const ObjectFileRef &objRef) const;
45 
46  AreaRef GetArea(const DatabaseId &dbId, const ObjectFileRef &areaRef) const;
47  };
48 
49  using RoutableObjectsRef=std::shared_ptr<RoutableObjects>;
50 
51  struct OSMSCOUT_API RoutableObjectsRequestMessage CLASS_FINAL : public NavigationMessage
52  {
53  GeoBox bbox;
54 
55  RoutableObjectsRequestMessage(const Timestamp& timestamp, const GeoBox &bbox);
56  };
57 
61  struct OSMSCOUT_API RoutableObjectsMessage CLASS_FINAL : public NavigationMessage
62  {
64 
65  RoutableObjectsMessage(const Timestamp& timestamp, const RoutableObjectsRef &data);
66  };
67 
68  using RoutableObjectsMessageRef=std::shared_ptr<RoutableObjectsMessage>;
69 
70  template <typename DataLoader>
71  class DataAgent CLASS_FINAL : public NavigationAgent
72  {
73  private:
74  std::map<std::string,DatabaseId> databaseMapping;
76  DataLoader &dataLoader;
77 
78  public:
79  explicit DataAgent(DataLoader &mapServiceProvider):
80  dataLoader(mapServiceProvider)
81  {
82 
83  }
84 
85  std::list<NavigationMessageRef> Process(const NavigationMessageRef& message) override
86  {
87  auto result=std::list<NavigationMessageRef>();
88 
89  if (auto* routeUpdateMessage = dynamic_cast<RouteUpdateMessage *>(message.get());
90  routeUpdateMessage != nullptr) {
91 
92  databaseMapping.clear();
93  // build reverse mapping
94  for (auto &e:routeUpdateMessage->routeDescription->GetDatabaseMapping()){
95  databaseMapping[e.second]=e.first;
96  }
97  vehicle=routeUpdateMessage->vehicle;
98  } else if (auto* requestMessage = dynamic_cast<RoutableObjectsRequestMessage*>(message.get());
99  requestMessage != nullptr) {
100 
101  if (databaseMapping.empty()){
102  return result; // no route yet
103  }
104 
105  if (GetSphericalDistance(requestMessage->bbox.GetMinCoord(),
106  requestMessage->bbox.GetMaxCoord()) > Kilometers(2)){
107  log.Warn() << "Requested routable data from huge region: " << requestMessage->bbox.GetDisplayText();
108  }
109 
110  auto msg=std::make_shared<RoutableObjectsMessage>(requestMessage->timestamp, std::make_shared<RoutableObjects>());
111 
112  dataLoader.loadRoutableObjects(requestMessage->bbox,
113  vehicle,
114  databaseMapping,
115  msg->data);
116 
117  // TODO: load all route objects too
118 
119  result.push_back(msg);
120  }
121  return result;
122  }
123  };
124 
125 }
126 #endif
OSMSCOUT_API Log log
std::chrono::system_clock::time_point Timestamp
Definition: Time.h:27
Definition: OSMScoutTypes.h:59
Definition: DataAgent.h:30
OSMSCOUT_API Distance GetSphericalDistance(const GeoCoord &a, const GeoCoord &b)
Definition: Area.h:86
Vehicle
Definition: OSMScoutTypes.h:55
Definition: DataAgent.h:37
std::shared_ptr< Way > WayRef
Definition: Way.h:202
Distance Kilometers(double km)
Definition: Distance.h:362
std::map< FileOffset, WayRef > ways
Definition: DataAgent.h:33
GeoBox bbox
Definition: DataAgent.h:40
std::shared_ptr< RoutableObjectsMessage > RoutableObjectsMessageRef
Definition: DataAgent.h:68
std::list< NavigationMessageRef > Process(const NavigationMessageRef &message) override
Definition: DataAgent.h:85
Definition: Area.h:38
std::shared_ptr< Area > AreaRef
Definition: Area.h:358
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
std::map< DatabaseId, RoutableDBObjects > dbMap
Definition: DataAgent.h:39
GeoBox bbox
Precomputed (cache) bounding box.
Definition: DataAgent.h:53
RoutableObjectsRef data
Definition: DataAgent.h:63
Definition: Engine.h:78
std::map< FileOffset, AreaRef > areas
Definition: DataAgent.h:34
Log & Warn(bool state)
Definition: Logger.h:462
std::shared_ptr< RoutableObjects > RoutableObjectsRef
Definition: DataAgent.h:49
DataAgent(DataLoader &mapServiceProvider)
Definition: DataAgent.h:79
std::shared_ptr< NavigationMessage > NavigationMessageRef
Definition: Engine.h:56
std::shared_ptr< TypeConfig > TypeConfigRef
Definition: TypeConfig.h:1227
TypeConfigRef typeConfig
Definition: DataAgent.h:32
Definition: Engine.h:48
uint32_t DatabaseId
Definition: DBFileOffset.h:30