libosmscout  1.1.1
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 <string>
26 #include <tuple>
27 
28 #include <osmscout/OSMScoutTypes.h>
29 
31 #include <osmscout/util/Distance.h>
32 #include <osmscout/util/Bearing.h>
33 
34 #include <osmscout/system/Math.h>
36 
37 namespace osmscout {
45  extern OSMSCOUT_API const double lonConversionFactor;
46 
54  extern OSMSCOUT_API const double latConversionFactor;
55 
56  constexpr uint32_t maxRawCoordValue = 0x7FFFFFF; // 134217727
57 
62  const size_t coordByteSize=7;
63 
69  class OSMSCOUT_API GeoCoord CLASS_FINAL
70  {
71  private:
72  double lat = 0.0;
73  double lon = 0.0;
74 
75  public:
79  GeoCoord() = default;
80 
81  GeoCoord(const GeoCoord& other) = default;
82 
86  GeoCoord(double lat,
87  double lon)
88  :lat(lat),lon(lon)
89  {
90  // no code
91  }
92 
96  void Set(double lat,
97  double lon)
98  {
99  this->lat=lat;
100  this->lon=lon;
101  }
102 
106  double GetLat() const
107  {
108  return lat;
109  }
110 
114  double GetLon() const
115  {
116  return lon;
117  }
118 
122  std::string GetDisplayText() const;
123 
124  std::ostream& operator<<(std::ostream& stream) const
125  {
126  stream << GetDisplayText();
127  return stream;
128  }
129 
138  Id GetId() const;
139 
143  void EncodeToBuffer(unsigned char buffer[]) const // NOLINT
144  {
145  auto latValue=(uint32_t)round((lat+90.0)*latConversionFactor);
146  auto lonValue=(uint32_t)round((lon+180.0)*lonConversionFactor);
147 
148  buffer[0]=((latValue >> 0u) & 0xffu);
149  buffer[1]=((latValue >> 8u) & 0xffu);
150  buffer[2]=((latValue >> 16u) & 0xffu);
151 
152  buffer[3]=((lonValue >> 0u) & 0xffu);
153  buffer[4]=((lonValue >> 8u) & 0xffu);
154  buffer[5]=((lonValue >> 16u) & 0xffu);
155 
156  buffer[6]=((latValue >> 24u) & 0x07u) | ((lonValue >> 20u) & 0x70u);
157  }
158 
162  Id GetHash() const
163  {
164  auto latValue=(uint64_t)round((lat+90.0)*latConversionFactor);
165  auto lonValue=(uint64_t)round((lon+180.0)*lonConversionFactor);
166  uint64_t number =0;
167 
168  for (size_t i=0; i<27; i++) {
169  size_t bit=26-i;
170 
171  number=number << 1u;
172  number=number+((latValue >> bit) & 0x01u);
173 
174  number=number << 1u;
175  number=number+((lonValue >> bit) & 0x01u);
176  }
177 
178  return number;
179  }
180 
184  void DecodeFromBuffer(const unsigned char buffer[]) // NOLINT
185  {
186  uint32_t latDat= (buffer[0] << 0u)
187  | (buffer[1] << 8u)
188  | (buffer[2] << 16u)
189  | ((buffer[6] & 0x0fu) << 24u);
190 
191  uint32_t lonDat= (buffer[3] << 0u)
192  | (buffer[4] << 8u)
193  | (buffer[5] << 16u)
194  | ((buffer[6] & 0xf0u) << 20u);
195 
196  lat=latDat/latConversionFactor-90.0;
197  lon=lonDat/lonConversionFactor-180.0;
198  }
199 
203  bool IsEqual(const GeoCoord& other) const
204  {
205  return lat==other.lat && lon==other.lon;
206  }
207 
237  static bool Parse(const std::string& text,
238  GeoCoord& coord);
239 
249  Distance GetDistance(const GeoCoord &target) const;
250 
262  GeoCoord Add(const Bearing &bearing, const Distance &distance) const;
263 
267  bool operator==(const GeoCoord& other) const
268  {
269  return lat==other.lat && lon==other.lon;
270  }
271 
275  bool operator!=(const GeoCoord& other) const
276  {
277  return lat!=other.lat || lon!=other.lon;
278  }
279 
280  bool operator<(const GeoCoord& other) const
281  {
282  return std::tie(lat, lon) < std::tie(other.lat, other.lon);
283  }
284 
288  GeoCoord& operator=(const GeoCoord& other) = default;
289 
290  Distance operator-(const GeoCoord& other) const
291  {
292  return GetDistance(other);
293  }
294  };
295 }
296 
297 #endif
uint64_t Id
Definition: OSMScoutTypes.h:41
constexpr uint32_t maxRawCoordValue
Definition: GeoCoord.h:56
Definition: Area.h:86
std::ostream & operator<<(std::ostream &stream) const
Definition: GeoCoord.h:124
bool operator==(const GeoCoord &other) const
Definition: GeoCoord.h:267
Distance operator-(const GeoCoord &other) const
Definition: GeoCoord.h:290
bool operator!=(const GeoCoord &other) const
Definition: GeoCoord.h:275
bool IsEqual(const GeoCoord &other) const
Definition: GeoCoord.h:203
Definition: Area.h:38
GeoCoord(double lat, double lon)
Definition: GeoCoord.h:86
bool operator<(const GeoCoord &other) const
Definition: GeoCoord.h:280
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
void Set(double lat, double lon)
Definition: GeoCoord.h:96
Id GetHash() const
Definition: GeoCoord.h:162
void DecodeFromBuffer(const unsigned char buffer[])
Definition: GeoCoord.h:184
double GetLon() const
Definition: GeoCoord.h:114
OSMSCOUT_API const double latConversionFactor
OSMSCOUT_API const double lonConversionFactor
const size_t coordByteSize
Definition: GeoCoord.h:62
void EncodeToBuffer(unsigned char buffer[]) const
Definition: GeoCoord.h:143
double GetLat() const
Definition: GeoCoord.h:106