coordinates.h
Go to the documentation of this file.
1 /* Copyright 2018-2019 TomTom N.V., 2014 Gareth Cross
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7 http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License. */
14 
15 #pragma once
16 
17 #include <cmath>
18 #include <stdexcept>
19 #include <string>
20 #include <tuple>
21 
22 #include "mercator.h"
23 
28 {
29  double lat, lon;
30 };
31 
40 template <typename NumericType = int>
42 {
43  NumericType x, y;
44 };
45 
51 template <typename NumericType = int>
53 {
54  if (zoom > MAX_ZOOM)
55  {
56  throw std::invalid_argument("Zoom level " + std::to_string(zoom) + " too high");
57  }
58  else if (coord.lat < -85.0511 || coord.lat > 85.0511)
59  {
60  throw std::invalid_argument("Latitude " + std::to_string(coord.lat) + " invalid");
61  }
62  else if (coord.lon < -180 || coord.lon > 180)
63  {
64  throw std::invalid_argument("Longitude " + std::to_string(coord.lon) + " invalid");
65  }
66 
67  double constexpr rho = M_PI / 180;
68  double const lat_rad = coord.lat * rho;
69 
71  int const n = 1 << zoom;
72  ret.x = n * ((coord.lon + 180) / 360.0);
73  ret.y = n * (1 - (std::log(std::tan(lat_rad) + 1 / std::cos(lat_rad)) / M_PI)) / 2;
74  return ret;
75 }
76 
82 template <typename NumericType = int>
84 {
85  WGSCoordinate ret{ 0, 0 };
86  ret.lon = coord.x / std::pow(2.0, zoom) * 360.0 - 180;
87  double n = M_PI - 2.0 * M_PI * (1 + coord.y) / std::pow(2.0, zoom);
88  ret.lat = 180.0 / M_PI * std::atan(0.5 * (std::exp(n) - std::exp(-n)));
89  return ret;
90 }
91 
92 template <typename NumericType>
94 {
95  return std::tie(self.x, self.y) == std::tie(other.x, other.y);
96 }
97 template <typename NumericType>
99 {
100  return std::tie(self.x, self.y) >= std::tie(other.x, other.y);
101 }
102 template <typename NumericType>
103 bool operator<=(TileCoordinateGeneric<NumericType> self, TileCoordinateGeneric<NumericType> other)
104 {
105  return std::tie(self.x, self.y) <= std::tie(other.x, other.y);
106 }
107 
TileCoordinateGeneric< NumericType > fromWGSCoordinate(WGSCoordinate coord, int zoom)
Definition: coordinates.h:52
bool operator>=(TileCoordinateGeneric< NumericType > self, TileCoordinateGeneric< NumericType > other)
Definition: coordinates.h:98
static constexpr int MAX_ZOOM
Max zoom level to support.
Definition: mercator.h:23
WGSCoordinate toWGSCoordinate(TileCoordinateGeneric< NumericType > coord, int zoom)
Definition: coordinates.h:83
bool operator==(TileCoordinateGeneric< NumericType > self, TileCoordinateGeneric< NumericType > other)
Definition: coordinates.h:93


rviz_satellite
Author(s): Gareth Cross , Andre Schröder
autogenerated on Thu May 4 2023 02:31:43