libosmscout 1.1.1
Loading...
Searching...
No Matches
GeoCoord.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_GEOCOORD_H
2#define OSMSCOUT_GEOCOORD_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2013 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
24
25#include <array>
26#include <cstddef>
27#include <string>
28#include <tuple>
29
31
35
38
39namespace osmscout {
47 extern OSMSCOUT_API const double lonConversionFactor;
48
56 extern OSMSCOUT_API const double latConversionFactor;
57
58 constexpr uint32_t maxRawCoordValue = 0x7FFFFFF; // 134217727
59
64 const size_t coordByteSize=7;
65
71 class OSMSCOUT_API GeoCoord CLASS_FINAL
72 {
73 private:
74 double lat = 0.0;
75 double lon = 0.0;
76
77 public:
78 using GeoCoordBuffer = std::array<std::byte,7>;
79
80 static constexpr int MinLatitude = -90;
81 static constexpr int MaxLatitude = 90;
82 static constexpr int MinLongitude = -180;
83 static constexpr int MaxLongitude = 180;
84
85 public:
89 GeoCoord() = default;
90
91 GeoCoord(const GeoCoord& other) = default;
92
96 GeoCoord(double lat,
97 double lon)
98 :lat(lat),lon(lon)
99 {
100 // no code
101 }
102
106 void Set(double lat,
107 double lon)
108 {
109 this->lat=lat;
110 this->lon=lon;
111 }
112
116 double GetLat() const
117 {
118 return lat;
119 }
120
124 double GetLon() const
125 {
126 return lon;
127 }
128
132 std::string GetDisplayText() const;
133
134 std::ostream& operator<<(std::ostream& stream) const
135 {
136 stream << GetDisplayText();
137 return stream;
138 }
139
148 Id GetId() const;
149
156 void EncodeToBuffer(GeoCoordBuffer& buffer) const // NOLINT
157 {
158 auto latValue=(uint32_t)round((lat+90.0)*latConversionFactor);
159 auto lonValue=(uint32_t)round((lon+180.0)*lonConversionFactor);
160
161 buffer[0]=std:: byte(latValue >> 0u);
162 buffer[1]=std::byte(latValue >> 8u);
163 buffer[2]=std::byte(latValue >> 16u);
164
165 buffer[3]=std::byte(lonValue >> 0u);
166 buffer[4]=std::byte(lonValue >> 8u);
167 buffer[5]=std::byte(lonValue >> 16u);
168
169 buffer[6]=std::byte((latValue >> 24u) & 0x07u) | std::byte((lonValue >> 20u) & 0x70u);
170 }
171
175 Id GetHash() const
176 {
177 auto latValue=(uint64_t)round((lat+90.0)*latConversionFactor);
178 auto lonValue=(uint64_t)round((lon+180.0)*lonConversionFactor);
179 uint64_t number =0;
180
181 for (size_t i=0; i<27; i++) {
182 size_t bit=26-i;
183
184 number=number << 1u;
185 number=number+((latValue >> bit) & 0x01u);
186
187 number=number << 1u;
188 number=number+((lonValue >> bit) & 0x01u);
189 }
190
191 return number;
192 }
193
197 bool IsEqual(const GeoCoord& other) const
198 {
199 return lat==other.lat && lon==other.lon;
200 }
201
205 bool IsValid() const
206 {
207 return lat >= MinLatitude && lat <= MaxLatitude &&
208 lon >= MinLongitude && lon <= MaxLongitude;
209 }
210
241 static bool Parse(const std::string& text,
242 GeoCoord& coord);
243
253 Distance GetDistance(const GeoCoord &target) const;
254
266 GeoCoord Add(const Bearing &bearing, const Distance &distance) const;
267
271 bool operator==(const GeoCoord& other) const
272 {
273 return lat==other.lat && lon==other.lon;
274 }
275
279 bool operator!=(const GeoCoord& other) const
280 {
281 return lat!=other.lat || lon!=other.lon;
282 }
283
284 bool operator<(const GeoCoord& other) const
285 {
286 return std::tie(lat, lon) < std::tie(other.lat, other.lon);
287 }
288
292 GeoCoord& operator=(const GeoCoord& other) = default;
293
294 Distance operator-(const GeoCoord& other) const
295 {
296 return GetDistance(other);
297 }
298 };
299}
300
301#endif
#define OSMSCOUT_API
Definition CoreImportExport.h:45
Definition Area.h:88
Distance operator-(const GeoCoord &other) const
Definition GeoCoord.h:294
static constexpr int MaxLatitude
Definition GeoCoord.h:81
const GeoCoord coord
Definition RouteStateAgent.h:49
bool operator<(const GeoCoord &other) const
Definition GeoCoord.h:284
static constexpr int MinLongitude
Definition GeoCoord.h:82
GeoCoord & operator=(const GeoCoord &other)=default
bool operator!=(const GeoCoord &other) const
Definition GeoCoord.h:279
void Set(double lat, double lon)
Definition GeoCoord.h:106
bool IsValid() const
Definition GeoCoord.h:205
std::ostream & operator<<(std::ostream &stream) const
Definition GeoCoord.h:134
Distance GetDistance(const GeoCoord &target) const
double GetLat() const
Definition GeoCoord.h:116
GeoCoord Add(const Bearing &bearing, const Distance &distance) const
double GetLon() const
Definition GeoCoord.h:124
static constexpr int MinLatitude
Definition GeoCoord.h:80
bool operator==(const GeoCoord &other) const
Definition GeoCoord.h:271
GeoCoord(const GeoCoord &other)=default
std::array< std::byte, 7 > GeoCoordBuffer
Definition GeoCoord.h:78
static constexpr int MaxLongitude
Definition GeoCoord.h:83
const GeoCoord target
Definition RouteStateAgent.h:50
Id GetHash() const
Definition GeoCoord.h:175
void EncodeToBuffer(GeoCoordBuffer &buffer) const
Definition GeoCoord.h:156
Vertex2D * buffer
Definition Transformation.h:343
bool IsEqual(const GeoCoord &other) const
Definition GeoCoord.h:197
static bool Parse(const std::string &text, GeoCoord &coord)
GeoCoord(double lat, double lon)
Definition GeoCoord.h:96
std::string GetDisplayText() const
const size_t coordByteSize
Definition GeoCoord.h:64
OSMSCOUT_API const double lonConversionFactor
uint64_t Id
Definition OSMScoutTypes.h:40
OSMSCOUT_API const double latConversionFactor
Definition Area.h:39
constexpr uint32_t maxRawCoordValue
Definition GeoCoord.h:58