libosmscout  1.1.1
MapService.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_MAPSERVICE_H
2 #define OSMSCOUT_MAPSERVICE_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2009 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 <memory>
25 #include <thread>
26 #include <vector>
27 
29 
30 #include <osmscout/Database.h>
31 #include <osmscout/TypeConfig.h>
32 
33 #include <osmscout/MapPainter.h>
34 #include <osmscout/StyleConfig.h>
35 
36 #include <osmscout/util/Breaker.h>
37 #include <osmscout/util/GeoBox.h>
40 
41 #include <osmscout/DataTileCache.h>
42 
43 namespace osmscout {
44 
53  {
54  private:
55  unsigned long maxAreaLevel=4;
56  bool useLowZoomOptimization=true;
57  BreakerRef breaker;
58  bool useMultithreading=false;
59  bool resolveRouteMembers=true;
60 
61  public:
62  AreaSearchParameter() = default;
63 
64  void SetMaximumAreaLevel(unsigned long maxAreaLevel);
65 
66  void SetUseLowZoomOptimization(bool useLowZoomOptimization);
67 
68  void SetUseMultithreading(bool useMultithreading);
69 
70  void SetResolveRouteMembers(bool resolveRouteMembers);
71 
72  void SetBreaker(const BreakerRef& breaker);
73 
74  unsigned long GetMaximumAreaLevel() const;
75 
76  bool GetUseLowZoomOptimization() const;
77 
78  bool GetUseMultithreading() const;
79 
80  bool GetResolveRouteMembers() const;
81 
82  bool IsAborted() const;
83  };
84 
98  {
99  public:
100  class OSMSCOUT_MAP_API TypeDefinition CLASS_FINAL
101  {
102  public:
103  TypeInfoSet nodeTypes;
104  TypeInfoSet wayTypes;
105  TypeInfoSet areaTypes;
106  TypeInfoSet routeTypes;
107  TypeInfoSet optimizedAreaTypes;
108  TypeInfoSet optimizedWayTypes;
109  };
110 
111  using TypeDefinitionRef = std::shared_ptr<TypeDefinition>;
112 
113  public:
114  using CallbackId = size_t;
115  using TileStateCallback = std::function<void (const TileRef &)>;
116 
117  private:
118  mutable std::mutex stateMutex;
119 
120  DatabaseRef database;
121  mutable DataTileCache cache;
122 
123  mutable WorkQueue<bool> nodeWorkerQueue;
124  std::thread nodeWorkerThread;
125 
126  mutable WorkQueue<bool> wayWorkerQueue;
127  std::thread wayWorkerThread;
128 
129  mutable WorkQueue<bool> wayLowZoomWorkerQueue;
130  std::thread wayLowZoomWorkerThread;
131 
132  mutable WorkQueue<bool> areaWorkerQueue;
133  std::thread areaWorkerThread;
134 
135  mutable WorkQueue<bool> areaLowZoomWorkerQueue;
136  std::thread areaLowZoomWorkerThread;
137 
138  mutable WorkQueue<bool> routeWorkerQueue;
139  std::thread routeWorkerThread;
140 
141  CallbackId nextCallbackId;
142  std::map<CallbackId,TileStateCallback> tileStateCallbacks;
143  mutable std::mutex callbackMutex; //<! Mutex to protect callback (de)registering
144 
145 
146  private:
147  TypeDefinitionRef GetTypeDefinition(const AreaSearchParameter& parameter,
148  const StyleConfig& styleConfig,
149  const Magnification& magnification) const;
150 
151  bool GetNodes(const AreaSearchParameter& parameter,
152  const TypeInfoSet& nodeTypes,
153  const GeoBox& boundingBox,
154  bool prefill,
155  const TileRef& tile) const;
156 
157  bool GetAreasLowZoom(const AreaSearchParameter& parameter,
158  const TypeInfoSet& areaTypes,
159  const Magnification& magnification,
160  const GeoBox& boundingBox,
161  bool prefill,
162  const TileRef& tile) const;
163 
164  bool GetAreas(const AreaSearchParameter& parameter,
165  const TypeInfoSet& areaTypes,
166  const Magnification& magnification,
167  const GeoBox& boundingBox,
168  bool prefill,
169  const TileRef& tile) const;
170 
171  bool GetWaysLowZoom(const AreaSearchParameter& parameter,
172  const TypeInfoSet& wayTypes,
173  const Magnification& magnification,
174  const GeoBox& boundingBox,
175  bool prefill,
176  const TileRef& tile) const;
177 
178  bool GetWays(const AreaSearchParameter& parameter,
179  const TypeInfoSet& wayTypes,
180  const GeoBox& boundingBox,
181  bool prefill,
182  const TileRef& tile) const;
183 
184  bool GetRoutes(const AreaSearchParameter& parameter,
185  const TypeInfoSet& routes,
186  const GeoBox& boundingBox,
187  bool prefill,
188  const TileRef& tile) const;
189 
190  template<typename Object, typename AreaObjectIndex, typename ObjectByOffsetFn>
191  bool GetObjects(const AreaSearchParameter& parameter,
192  const TypeInfoSet& types,
193  const GeoBox& boundingBox,
194  bool prefill,
195  const TileRef& tile,
196  TileData<Object> &tileData,
197  AreaObjectIndex areaObjectIndex,
198  ObjectByOffsetFn objectByOffsetFn,
199  const std::string_view &objectTypeName,
200  const std::string_view &objectTypeNamePl) const
201  {
202  if (!areaObjectIndex) {
203  return false;
204  }
205 
206  if (tileData.IsComplete()) {
207  return true;
208  }
209 
210  if (parameter.IsAborted()) {
211  return false;
212  }
213 
214  TypeInfoSet cachedTypes(tileData.GetTypes());
215  TypeInfoSet requestedTypes(types);
216  TypeInfoSet loadedTypes;
217  std::vector<FileOffset> offsets;
218 
219  if (!cachedTypes.Empty()) {
220  requestedTypes.Remove(cachedTypes);
221  }
222 
223  if (!requestedTypes.Empty()) {
224  if (!areaObjectIndex->GetOffsets(boundingBox,
225  requestedTypes,
226  offsets,
227  loadedTypes)) {
228  log.Error() << "Error getting " << objectTypeNamePl << " from area " << objectTypeName << " index!";
229  return false;
230  }
231 
232  if (parameter.IsAborted()) {
233  return false;
234  }
235 
236  if (!offsets.empty()) {
237  // Sort offsets before loading to optimize disk access
238  std::sort(offsets.begin(),offsets.end());
239 
240  if (parameter.IsAborted()) {
241  return false;
242  }
243 
244  std::vector<Object> objects;
245 
246  if (!objectByOffsetFn(offsets, objects)) {
247  log.Error() << "Error reading " << objectTypeNamePl << " in area!";
248  return false;
249  }
250 
251  if (parameter.IsAborted()) {
252  return false;
253  }
254 
255  if (prefill) {
256  tileData.AddPrefillData(loadedTypes, std::move(objects));
257  }
258  else {
259  if (cachedTypes.Empty()){
260  tileData.SetData(loadedTypes, std::move(objects));
261  }else{
262  tileData.AddData(loadedTypes, objects);
263  }
264  }
265  }
266  }
267 
268  if (!prefill) {
269  tileData.SetComplete();
270  }
271 
272  NotifyTileStateCallbacks(tile);
273 
274  return !parameter.IsAborted();
275  }
276 
277  void NodeWorkerLoop();
278  void WayWorkerLoop();
279  void WayLowZoomWorkerLoop();
280  void AreaWorkerLoop();
281  void AreaLowZoomWorkerLoop();
282  void RouteWorkerLoop();
283 
284  std::future<bool> PushNodeTask(const AreaSearchParameter& parameter,
285  const TypeInfoSet& nodeTypes,
286  const GeoBox& boundingBox,
287  bool prefill,
288  const TileRef& tile) const;
289 
290  std::future<bool> PushAreaLowZoomTask(const AreaSearchParameter& parameter,
291  const TypeInfoSet& areaTypes,
292  const Magnification& magnification,
293  const GeoBox& boundingBox,
294  bool prefill,
295  const TileRef& tile) const;
296 
297  std::future<bool> PushAreaTask(const AreaSearchParameter& parameter,
298  const TypeInfoSet& areaTypes,
299  const Magnification& magnification,
300  const GeoBox& boundingBox,
301  bool prefill,
302  const TileRef& tile) const;
303 
304  std::future<bool> PushWayLowZoomTask(const AreaSearchParameter& parameter,
305  const TypeInfoSet& wayTypes,
306  const Magnification& magnification,
307  const GeoBox& boundingBox,
308  bool prefill,
309  const TileRef& tile) const;
310 
311  std::future<bool> PushWayTask(const AreaSearchParameter& parameter,
312  const TypeInfoSet& wayTypes,
313  const GeoBox& boundingBox,
314  bool prefill,
315  const TileRef& tile) const;
316 
317  std::future<bool> PushRouteTask(const AreaSearchParameter& parameter,
318  const TypeInfoSet& routeTypes,
319  const GeoBox& boundingBox,
320  bool prefill,
321  const TileRef& tile) const;
322 
323  void NotifyTileStateCallbacks(const TileRef& tile) const;
324 
325  bool LoadMissingTileDataStyleSheet(const AreaSearchParameter& parameter,
326  const StyleConfig& styleConfig,
327  std::list<TileRef>& tiles,
328  bool async) const;
329 
330  bool LoadMissingTileDataTypeDefinition(const AreaSearchParameter& parameter,
331  const Magnification& magnification,
332  const TypeDefinition& typeDefinition,
333  std::list<TileRef>& tiles,
334  bool async) const;
335 
336  public:
337  explicit MapService(const DatabaseRef& database);
338  virtual ~MapService();
339 
340  void SetCacheSize(size_t cacheSize);
341  size_t GetCacheSize() const;
342  size_t GetCurrentCacheSize() const;
343 
344  void CleanupTileCache();
345  void FlushTileCache();
346  void InvalidateTileCache();
347 
348  void LookupTiles(const Magnification& magnification,
349  const GeoBox& boundingBox,
350  std::list<TileRef>& tiles) const;
351 
352  void LookupTiles(const Projection& projection,
353  std::list<TileRef>& tiles) const;
354 
355  TileRef LookupTile(const TileKey& key) const;
356 
357  bool LoadMissingTileData(const AreaSearchParameter& parameter,
358  const StyleConfig& styleConfig,
359  std::list<TileRef>& tiles) const;
360 
361  bool LoadMissingTileDataAsync(const AreaSearchParameter& parameter,
362  const StyleConfig& styleConfig,
363  std::list<TileRef>& tiles) const;
364 
365  bool LoadMissingTileData(const AreaSearchParameter& parameter,
366  const Magnification& magnification,
367  const TypeDefinition& typeDefinition,
368  std::list<TileRef>& tiles) const;
369 
370  bool LoadMissingTileDataAsync(const AreaSearchParameter& parameter,
371  const Magnification& magnification,
372  const TypeDefinition& typeDefinition,
373  std::list<TileRef>& tiles) const;
374 
375  void AddTileDataToMapData(std::list<TileRef>& route,
376  MapData& data) const;
377 
378  void AddTileDataToMapData(std::list<TileRef>& tiles,
379  const TypeDefinition& typeDefinition,
380  MapData& data) const;
381 
382  bool GetGroundTiles(const Projection& projection,
383  std::list<GroundTile>& tiles) const;
384 
385  bool GetGroundTiles(const GeoBox& boundingBox,
386  const Magnification& magnification,
387  std::list<GroundTile>& tiles) const;
388 
389  SRTMDataRef GetSRTMData(const Projection& projection) const;
390 
391  SRTMDataRef GetSRTMData(const GeoBox& boundingBox) const;
392 
393  CallbackId RegisterTileStateCallback(TileStateCallback callback);
394  void DeregisterTileStateCallback(CallbackId callbackId);
395  };
396 
399  using MapServiceRef = std::shared_ptr<MapService>;
400 }
401 
402 #endif
OSMSCOUT_API Log log
std::shared_ptr< SRTMData > SRTMDataRef
Definition: SRTM.h:58
TypeInfoSet nodeTypes
Definition: MapService.h:103
Index selectors by type and level.
Definition: StyleConfig.h:553
void SetComplete()
Definition: DataTileCache.h:187
std::function< void(const TileRef &)> TileStateCallback
Definition: MapService.h:115
std::shared_ptr< Breaker > BreakerRef
Definition: Breaker.h:65
std::shared_ptr< Tile > TileRef
Definition: DataTileCache.h:443
Definition: MapService.h:52
std::shared_ptr< TypeDefinition > TypeDefinitionRef
Definition: MapService.h:111
Log & Error(bool state)
Definition: Logger.h:469
TypeInfoSet routeTypes
Definition: MapService.h:106
std::shared_ptr< MapService > MapServiceRef
Definition: MapService.h:399
std::shared_ptr< Database > DatabaseRef
Reference counted reference to an Database instance.
Definition: Database.h:555
size_t CallbackId
Definition: MapService.h:114
Definition: MapService.h:97
Definition: Area.h:38
Definition: MapService.h:100
#define OSMSCOUT_MAP_API
Definition: MapImportExport.h:45
Definition: DataTileCache.h:456
Definition: DataTileCache.h:52
TypeInfoSet areaTypes
Definition: MapService.h:105
TypeInfoSet optimizedAreaTypes
Definition: MapService.h:107
void AddData(const TypeInfoSet &types, const std::vector< O > &data)
Definition: DataTileCache.h:144
void SetData(const TypeInfoSet &types, const std::vector< O > &data)
Definition: DataTileCache.h:158
TypeInfoSet GetTypes() const
Definition: DataTileCache.h:210
bool IsComplete() const
Definition: DataTileCache.h:197
TypeInfoSet optimizedWayTypes
Definition: MapService.h:108
TypeInfoSet wayTypes
Definition: MapService.h:104
void AddPrefillData(const TypeInfoSet &types, const std::vector< O > &data)
Definition: DataTileCache.h:91