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