Libosmium  2.10.3
Fast and flexible C++ library for working with OpenStreetMap data
tile.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_GEOM_TILE_HPP
2 #define OSMIUM_GEOM_TILE_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <cassert>
37 #include <cstdint>
38 
41 #include <osmium/osm/location.hpp>
42 
43 namespace osmium {
44 
45  namespace geom {
46 
47  namespace detail {
48 
49  template <typename T>
50  inline T restrict_to_range(T value, T min, T max) {
51  if (value < min) return min;
52  if (value > max) return max;
53  return value;
54  }
55 
56  } // namespace detail
57 
61  struct Tile {
62 
64  uint32_t x;
65 
67  uint32_t y;
68 
70  uint32_t z;
71 
80  explicit Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept : x(tx), y(ty), z(zoom) {
81  assert(zoom <= 30u);
82  assert(x < (1u << zoom));
83  assert(y < (1u << zoom));
84  }
85 
94  explicit Tile(uint32_t zoom, const osmium::Location& location) :
95  z(zoom) {
96  assert(zoom <= 30u);
97  assert(location.valid());
99  const int32_t n = 1 << zoom;
100  const double scale = detail::max_coordinate_epsg3857 * 2 / n;
101  x = uint32_t(detail::restrict_to_range<int32_t>(int32_t((c.x + detail::max_coordinate_epsg3857) / scale), 0, n-1));
102  y = uint32_t(detail::restrict_to_range<int32_t>(int32_t((detail::max_coordinate_epsg3857 - c.y) / scale), 0, n-1));
103  }
104 
110  bool valid() const noexcept {
111  if (z > 30) {
112  return false;
113  }
114  const uint32_t max = 1 << z;
115  return x < max && y < max;
116  }
117 
118  }; // struct Tile
119 
121  inline bool operator==(const Tile& lhs, const Tile& rhs) {
122  return lhs.z == rhs.z && lhs.x == rhs.x && lhs.y == rhs.y;
123  }
124 
125  inline bool operator!=(const Tile& lhs, const Tile& rhs) {
126  return ! (lhs == rhs);
127  }
128 
132  inline bool operator<(const Tile& lhs, const Tile& rhs) {
133  if (lhs.z < rhs.z) return true;
134  if (lhs.z > rhs.z) return false;
135  if (lhs.x < rhs.x) return true;
136  if (lhs.x > rhs.x) return false;
137  return lhs.y < rhs.y;
138  }
139 
140  } // namespace geom
141 
142 } // namespace osmium
143 
144 #endif // OSMIUM_GEOM_TILE_HPP
bool valid() const noexcept
Definition: tile.hpp:110
constexpr bool valid() const noexcept
Definition: location.hpp:341
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: attr.hpp:333
Definition: coordinates.hpp:46
uint32_t x
x coordinate
Definition: tile.hpp:64
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition: mercator_projection.hpp:76
bool operator<(const Tile &lhs, const Tile &rhs)
Definition: tile.hpp:132
uint32_t y
y coordinate
Definition: tile.hpp:67
Definition: location.hpp:266
Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept
Definition: tile.hpp:80
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:83
uint32_t z
Zoom level.
Definition: tile.hpp:70
Definition: tile.hpp:61
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:76
Tile(uint32_t zoom, const osmium::Location &location)
Definition: tile.hpp:94