libosmscout  1.1.1
RouteNodeDataFile.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_ROUTENODEDATAFILE_H
2 #define OSMSCOUT_ROUTENODEDATAFILE_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2018 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 <map>
24 #include <vector>
25 
26 #include <osmscout/DataFile.h>
27 #include <osmscout/Pixel.h>
28 
29 #include <osmscout/util/TileId.h>
30 
32 
33 namespace osmscout {
37  class OSMSCOUT_API RouteNodeDataFile CLASS_FINAL
38  {
39  private:
40  struct IndexEntry
41  {
42  FileOffset fileOffset;
43  uint32_t count;
44  };
45 
46  struct IndexPage
47  {
48  FileOffset fileOffset;
49  uint32_t remaining;
50  std::unordered_map<Id,RouteNodeRef> nodeMap;
51 
52  RouteNodeRef find(FileScanner& scanner,
53  Id id);
54  };
55 
56  private:
57  using ValueCache = Cache<Id, IndexPage>;
58 
59  private:
60  std::string datafile;
61  std::string datafilename;
62 
63  TypeConfigRef typeConfig;
64 
65  std::map<Pixel,IndexEntry> index;
66 
67  mutable FileScanner scanner;
68  mutable ValueCache cache;
69  mutable std::mutex accessMutex;
70  mutable Magnification magnification;
71 
72  private:
73  bool LoadIndexPage(const osmscout::Pixel& tile,
74  ValueCache::CacheRef& cacheRef) const;
75  bool GetIndexPage(const osmscout::Pixel& tile,
76  ValueCache::CacheRef& cacheRef) const;
77 
78  public:
79  explicit RouteNodeDataFile(const std::string& datafile,
80  size_t cacheSize);
81 
82  bool Open(const TypeConfigRef& typeConfig,
83  const std::string& path,
84  bool memoryMapedData);
85  bool IsOpen() const;
86  bool Close();
87 
88  Pixel GetTile(const GeoCoord& coord) const;
89  bool IsCovered(const Pixel& tile) const;
90 
91  bool IsCovered(const GeoCoord& coord) const;
92 
93  bool Get(Id id,
94  RouteNodeRef& node) const;
95 
96  template<typename IteratorIn>
97  bool Get(IteratorIn begin, IteratorIn end, size_t size,
98  std::vector<RouteNodeRef>& data) const
99  {
100  data.reserve(size);
101 
102  for (IteratorIn idIter=begin; idIter!=end; ++idIter) {
103  Id id=*idIter;
104  ValueCache::CacheRef cacheRef;
105 
106  GeoCoord coord=Point::GetCoordFromId(id);
107  osmscout::Pixel tile=TileId::GetTile(magnification,coord).AsPixel();
108 
109  //std::cout << "Tile " << tile.GetDisplayText() << " " << tile.GetId() << "..." << std::endl;
110 
111  if (!GetIndexPage(tile,
112  cacheRef)) {
113  return false;
114  }
115 
116  auto node=cacheRef->value.find(scanner,
117  id);
118 
119  if (node==nullptr) {
120  return false;
121  }
122 
123  data.push_back(node);
124  }
125 
126  return true;
127  }
128 
129  template<typename IteratorIn>
130  bool Get(IteratorIn begin, IteratorIn end, size_t /*size*/,
131  std::unordered_map<Id,RouteNodeRef>& dataMap) const
132  {
133  for (IteratorIn idIter=begin; idIter!=end; ++idIter) {
134  Id id=*idIter;
135  ValueCache::CacheRef cacheRef;
136 
137  GeoCoord coord=Point::GetCoordFromId(id);
138  osmscout::Pixel tile=TileId::GetTile(magnification,coord).AsPixel();
139 
140  //std::cout << "Tile " << tile.GetDisplayText() << " " << tile.GetId() << "..." << std::endl;
141 
142  if (!GetIndexPage(tile,
143  cacheRef)) {
144  return false;
145  }
146 
147  auto node=cacheRef->value.find(scanner,
148  id);
149 
150  if (node==nullptr) {
151  return false;
152  }
153 
154  dataMap[id]=node;
155  }
156 
157  return true;
158  }
159  };
160 
161 }
162 
163 #endif
164 
bool Get(IteratorIn begin, IteratorIn end, size_t, std::unordered_map< Id, RouteNodeRef > &dataMap) const
Definition: RouteNodeDataFile.h:130
uint64_t Id
Definition: OSMScoutTypes.h:41
Pixel AsPixel() const
Definition: TileId.h:71
Definition: Area.h:38
#define CLASS_FINAL
Definition: Compiler.h:26
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
bool Get(IteratorIn begin, IteratorIn end, size_t size, std::vector< RouteNodeRef > &data) const
Definition: RouteNodeDataFile.h:97
std::shared_ptr< RouteNode > RouteNodeRef
Definition: RouteNode.h:160
uint64_t FileOffset
Definition: OSMScoutTypes.h:47
typename OrderList::iterator CacheRef
Definition: Cache.h:98
static TileId GetTile(const Magnification &magnification, const GeoCoord &coord)
std::shared_ptr< TypeConfig > TypeConfigRef
Definition: TypeConfig.h:1227