Libosmium  2.11.0
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-2017 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 constexpr const T& clamp(const T& value, const T& min, const T& max) {
51  return value < min ? min : (max < value ? max : value);
52  }
53 
54  } // namespace detail
55 
60  inline constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept {
61  return 1u << zoom;
62  }
63 
68  inline constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept {
69  return detail::max_coordinate_epsg3857 * 2 / num_tiles_in_zoom(zoom);
70  }
71 
77  inline constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept {
78  return static_cast<uint32_t>(detail::clamp<int32_t>(
79  static_cast<int32_t>((x + detail::max_coordinate_epsg3857) / tile_extent_in_zoom(zoom)),
80  0, num_tiles_in_zoom(zoom) -1
81  ));
82  }
83 
89  inline constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept {
90  return static_cast<uint32_t>(detail::clamp<int32_t>(
91  static_cast<int32_t>((detail::max_coordinate_epsg3857 - y) / tile_extent_in_zoom(zoom)),
92  0, num_tiles_in_zoom(zoom) -1
93  ));
94  }
95 
99  struct Tile {
100 
102  uint32_t x;
103 
105  uint32_t y;
106 
108  uint32_t z;
109 
118  explicit Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept :
119  x(tx),
120  y(ty),
121  z(zoom) {
122  assert(zoom <= 30u);
123  assert(x < num_tiles_in_zoom(zoom));
124  assert(y < num_tiles_in_zoom(zoom));
125  }
126 
135  explicit Tile(uint32_t zoom, const osmium::Location& location) :
136  z(zoom) {
137  assert(zoom <= 30u);
138  assert(location.valid());
139  const auto coordinates = lonlat_to_mercator(location);
140  x = mercx_to_tilex(zoom, coordinates.x);
141  y = mercy_to_tiley(zoom, coordinates.y);
142  }
143 
152  explicit Tile(uint32_t zoom, const osmium::geom::Coordinates& coordinates) :
153  z(zoom) {
154  assert(zoom <= 30u);
155  x = mercx_to_tilex(zoom, coordinates.x);
156  y = mercy_to_tiley(zoom, coordinates.y);
157  }
158 
164  bool valid() const noexcept {
165  if (z > 30) {
166  return false;
167  }
168  const auto max = num_tiles_in_zoom(z);
169  return x < max && y < max;
170  }
171 
172  }; // struct Tile
173 
175  inline bool operator==(const Tile& lhs, const Tile& rhs) {
176  return lhs.z == rhs.z && lhs.x == rhs.x && lhs.y == rhs.y;
177  }
178 
179  inline bool operator!=(const Tile& lhs, const Tile& rhs) {
180  return ! (lhs == rhs);
181  }
182 
186  inline bool operator<(const Tile& lhs, const Tile& rhs) {
187  if (lhs.z < rhs.z) return true;
188  if (lhs.z > rhs.z) return false;
189  if (lhs.x < rhs.x) return true;
190  if (lhs.x > rhs.x) return false;
191  return lhs.y < rhs.y;
192  }
193 
194  } // namespace geom
195 
196 } // namespace osmium
197 
198 #endif // OSMIUM_GEOM_TILE_HPP
double y
Definition: coordinates.hpp:51
bool valid() const noexcept
Definition: tile.hpp:164
constexpr uint32_t num_tiles_in_zoom(uint32_t zoom) noexcept
Definition: tile.hpp:60
Tile(uint32_t zoom, const osmium::geom::Coordinates &coordinates)
Definition: tile.hpp:152
constexpr bool valid() const noexcept
Definition: location.hpp:341
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
constexpr uint32_t mercx_to_tilex(uint32_t zoom, double x) noexcept
Definition: tile.hpp:77
Definition: attr.hpp:333
Definition: coordinates.hpp:48
uint32_t x
x coordinate
Definition: tile.hpp:102
Coordinates lonlat_to_mercator(const Coordinates &c)
Definition: mercator_projection.hpp:120
bool operator<(const Tile &lhs, const Tile &rhs)
Definition: tile.hpp:186
uint32_t y
y coordinate
Definition: tile.hpp:105
Definition: location.hpp:266
Tile(uint32_t zoom, uint32_t tx, uint32_t ty) noexcept
Definition: tile.hpp:118
bool operator!=(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:145
constexpr uint32_t mercy_to_tiley(uint32_t zoom, double y) noexcept
Definition: tile.hpp:89
double x
Definition: coordinates.hpp:50
uint32_t z
Zoom level.
Definition: tile.hpp:108
Definition: tile.hpp:99
bool operator==(const Coordinates &lhs, const Coordinates &rhs) noexcept
Definition: coordinates.hpp:135
Tile(uint32_t zoom, const osmium::Location &location)
Definition: tile.hpp:135
constexpr double tile_extent_in_zoom(uint32_t zoom) noexcept
Definition: tile.hpp:68