libosmscout  1.1.1
TileId.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_TILEID_H
2 #define OSMSCOUT_TILEID_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2016 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 <memory>
24 #include <tuple>
25 
27 
28 #include <osmscout/Pixel.h>
29 
30 #include <osmscout/util/GeoBox.h>
32 
33 namespace osmscout {
34 
46  {
47  private:
48  uint32_t x;
49  uint32_t y;
50 
51  public:
52  TileId(uint32_t x,
53  uint32_t y);
54 
58  uint32_t GetX() const
59  {
60  return x;
61  }
62 
66  uint32_t GetY() const
67  {
68  return y;
69  }
70 
71  Pixel AsPixel() const
72  {
73  return {x,y};
74  }
75 
76  std::string GetDisplayText() const;
77 
81  bool operator==(const TileId& other) const
82  {
83  return y==other.y &&
84  x==other.x;
85  }
86 
90  bool operator!=(const TileId& other) const
91  {
92  return y!=other.y ||
93  x!=other.x;
94  }
95 
100  bool operator<(const TileId& other) const
101  {
102  return std::tie(y, x) < std::tie(other.y, other.x);
103  }
104 
105  GeoCoord GetTopLeftCoord(const Magnification& magnification) const;
106  GeoBox GetBoundingBox(const MagnificationLevel& level) const;
107  GeoBox GetBoundingBox(const Magnification& magnification) const;
108 
109  static TileId GetTile(const Magnification& magnification,
110  const GeoCoord& coord);
111 
112  static TileId GetTile(const MagnificationLevel& level,
113  const GeoCoord& coord);
114  };
115 
120  {
121  std::size_t operator()(const TileId& id) const noexcept
122  {
123  auto h1 = static_cast<size_t>(id.GetX());
124  auto h2 = static_cast<size_t>(id.GetY());
125  return h1 ^ (h2 << 16u);
126  }
127  };
128 
130  {
131  private:
132  uint32_t level;
133  TileId id;
134 
135  public:
136  TileKey(const Magnification& magnification,
137  const TileId& id);
138 
139  uint32_t GetLevel() const
140  {
141  return level;
142  }
143 
144  TileId GetId() const
145  {
146  return id;
147  }
148 
152  GeoBox GetBoundingBox() const;
153 
154  std::string GetDisplayText() const;
155 
156  TileKey GetParent() const;
157 
158  bool operator==(const TileKey& other) const;
159  bool operator!=(const TileKey& other) const;
160  bool operator<(const TileKey& other) const;
161  };
162 
163  class OSMSCOUT_API TileIdBoxConstIterator CLASS_FINAL
164  {
165  public:
166  using self_type = TileIdBoxConstIterator;
168  using reference = const TileId&;
169  using pointer = TileId;
170  using iterator_category = std::input_iterator_tag;
171 
172  private:
173  TileId currentTile;
174  TileId minTile;
175  TileId maxTile;
176 
177  public:
178  TileIdBoxConstIterator(const TileId& currentTile,
179  const TileId& minTile,
180  const TileId& maxTile)
181  : currentTile(currentTile),
182  minTile(minTile),
183  maxTile(maxTile)
184  {
185  // no code
186  }
187 
188  TileIdBoxConstIterator(const TileIdBoxConstIterator& other) = default;
189 
190  TileIdBoxConstIterator& operator++()
191  {
192  if (currentTile.GetX()>=maxTile.GetX()) {
193  currentTile=TileId(minTile.GetX(),
194  currentTile.GetY()+1);
195  }
196  else {
197  currentTile=TileId(currentTile.GetX()+1,
198  currentTile.GetY());
199  }
200 
201  return *this;
202  }
203 
204  TileIdBoxConstIterator operator++(int)
205  {
206  TileIdBoxConstIterator tmp(*this);
207 
208  operator++();
209 
210  return tmp;
211  }
212 
213  bool operator==(const TileIdBoxConstIterator& other) const
214  {
215  return currentTile==other.currentTile;
216  }
217 
218  bool operator!=(const TileIdBoxConstIterator& other) const
219  {
220  return currentTile!=other.currentTile;
221  }
222 
223  const TileId& operator*() const
224  {
225  return currentTile;
226  }
227 
229  {
230  return currentTile;
231  }
232  };
233 
240  class OSMSCOUT_API TileIdBox CLASS_FINAL
241  {
242  private:
243  TileId minTile;
244  TileId maxTile;
245 
246  public:
247  TileIdBox(const TileId& a,
248  const TileId& b);
249 
250  TileIdBox(const Magnification& magnification,
251  const GeoBox& boundingBox)
252  : TileIdBox(TileId::GetTile(magnification,
253  boundingBox.GetMinCoord()),
254  TileId::GetTile(magnification,
255  boundingBox.GetMaxCoord()))
256  {
257  }
258 
259  TileId GetMin() const
260  {
261  return minTile;
262  }
263 
264  TileId GetMax() const
265  {
266  return maxTile;
267  }
268 
269  uint32_t GetMinX() const
270  {
271  return minTile.GetX();
272  }
273 
274  uint32_t GetMaxX() const
275  {
276  return maxTile.GetX();
277  }
278 
279  uint32_t GetMinY() const
280  {
281  return minTile.GetY();
282  }
283 
284  uint32_t GetMaxY() const
285  {
286  return maxTile.GetY();
287  }
288 
289  uint32_t GetWidth() const
290  {
291  return maxTile.GetX()-minTile.GetX()+1;
292  }
293 
294  uint32_t GetHeight() const
295  {
296  return maxTile.GetY()-minTile.GetY()+1;
297  }
298 
299  uint32_t GetCount() const
300  {
301  return GetWidth()*GetHeight();
302  }
303 
304  TileIdBoxConstIterator begin() const
305  {
306  return TileIdBoxConstIterator(minTile,
307  minTile,
308  maxTile);
309  }
310 
311  TileIdBoxConstIterator end() const
312  {
313  return TileIdBoxConstIterator(TileId(minTile.GetX(),
314  maxTile.GetY()+1),
315  minTile,
316  maxTile);
317  }
318 
319  bool operator==(const TileIdBox& other) const
320  {
321  return minTile==other.minTile &&
322  maxTile==other.maxTile;
323  }
324 
325  GeoBox GetBoundingBox(const Magnification& magnification) const;
326 
327  TileIdBox Include(const TileId& tileId) const;
328  TileIdBox Include(const TileIdBox& other) const;
329  TileIdBox Intersection(const TileIdBox& other) const;
330 
331  bool Intersects(const TileIdBox& other) const;
332 
333  std::string GetDisplayText() const
334  {
335  return std::string("["+minTile.GetDisplayText()+" - "+maxTile.GetDisplayText()+"]");
336  }
337  };
338 
342 }
343 
344 #endif
TypeInfoRef pointer
Definition: TypeInfoSet.h:38
uint32_t GetCount() const
Definition: TileId.h:299
TileId GetMin() const
Definition: TileId.h:259
TileId GetId() const
Definition: TileId.h:144
TileIdBoxConstIterator operator++(int)
Definition: TileId.h:204
Pixel AsPixel() const
Definition: TileId.h:71
TileId operator->() const
Definition: TileId.h:228
uint32_t GetWidth() const
Definition: TileId.h:289
TileIdBox(const Magnification &magnification, const GeoBox &boundingBox)
Definition: TileId.h:250
bool operator!=(const MapViewStruct &r1, const MapViewStruct &r2)
Definition: DBThread.h:61
uint32_t GetX() const
Definition: TileId.h:58
void GetBoundingBox(const std::vector< N > &nodes, double &minLon, double &maxLon, double &minLat, double &maxLat)
Definition: Geometry.h:107
const TileId & operator*() const
Definition: TileId.h:223
TypeInfoRef value_type
Definition: TypeInfoSet.h:36
Definition: Area.h:38
uint32_t GetMaxX() const
Definition: TileId.h:274
TileId GetMax() const
Definition: TileId.h:264
bool operator==(const TileId &other) const
Definition: TileId.h:81
TileIdBoxConstIterator & operator++()
Definition: TileId.h:190
uint32_t GetMaxY() const
Definition: TileId.h:284
const TypeInfoRef & reference
Definition: TypeInfoSet.h:37
TileIdBoxConstIterator(const TileId &currentTile, const TileId &minTile, const TileId &maxTile)
Definition: TileId.h:178
uint32_t GetMinX() const
Definition: TileId.h:269
#define CLASS_FINAL
Definition: Compiler.h:26
bool operator!=(const TileId &other) const
Definition: TileId.h:90
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
bool operator<(const TileCacheKey &a, const TileCacheKey &b)
uint32_t GetY() const
Definition: TileId.h:66
std::string GetDisplayText() const
Definition: TileId.h:333
TileIdBoxConstIterator end() const
Definition: TileId.h:311
bool operator==(const MapView &a, const MapView &b)
Definition: InputHandler.h:222
Definition: DBThread.h:86
uint32_t GetMinY() const
Definition: TileId.h:279
TypeInfoSetConstIterator self_type
Definition: TypeInfoSet.h:35
bool operator!=(const TileIdBoxConstIterator &other) const
Definition: TileId.h:218
uint32_t GetHeight() const
Definition: TileId.h:294
std::size_t operator()(const TileId &id) const noexcept
Definition: TileId.h:121
bool operator<(const TileId &other) const
Definition: TileId.h:100
Definition: TileId.h:119
bool operator==(const TileIdBox &other) const
Definition: TileId.h:319
Definition: TileId.h:45
Definition: TileId.h:129
std::input_iterator_tag iterator_category
Definition: TypeInfoSet.h:39
TileIdBoxConstIterator begin() const
Definition: TileId.h:304
bool operator==(const TileIdBoxConstIterator &other) const
Definition: TileId.h:213
uint32_t GetLevel() const
Definition: TileId.h:139