geolocation.h Source File

CPP API: geolocation.h Source File
geolocation.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Kilian Volmer, Sascha Korf, Carlotta Gerstein, Daniel Abele, Elisabeth Kluth, Khoa Nguyen, David Kerkmann
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_GEOGRAPHY_LOCATIONS_H
21 #define MIO_GEOGRAPHY_LOCATIONS_H
22 
23 #include "distance.h"
26 #include "memilio/config.h"
27 
28 #include <cassert>
29 #include <cmath>
30 #include <numbers>
31 
32 namespace mio
33 {
34 namespace geo
35 {
42 {
43 
44 public:
52  : m_latitude(lat)
53  , m_longitude(lon)
54  {
55  check_input();
56  }
62  GeographicalLocation(std::pair<ScalarType, ScalarType> coordinates)
63  : m_latitude(coordinates.first)
64  , m_longitude(coordinates.second)
65  {
66  check_input();
67  }
68 
73  GeographicalLocation() = default;
74 
78  bool operator==(const GeographicalLocation& other) const
79  {
80  return (m_latitude == other.m_latitude && m_longitude == other.m_longitude);
81  }
82 
86  bool operator!=(const GeographicalLocation& other) const
87  {
88  return !(*this == other);
89  }
90 
96  bool is_close(const GeographicalLocation& other,
98  {
99  return distance(other) < tol;
100  }
101 
108  {
109  return Members("GeographicalLocation").add("latitude", m_latitude).add("longitude", m_longitude);
110  }
111 
112  /*
113  * @brief Calculate the distance between two GeographicalLocation%s.
114  * @param other The other GeographicalLocation.
115  * @return The distance between the two locations as @ref mio::geo::Distance .
116  *
117  * Uses the haversine formula (https://en.wikipedia.org/wiki/Haversine_formula) to calculate the distance between
118  * two geographical locations. Uses an earth radius of 6371 km (https://en.wikipedia.org/wiki/Earth_radius).
119  * The math functions use radians, whereas coordinates are given in degrees.
120  */
122  {
123  const ScalarType delta_latitude = (m_latitude - other.m_latitude) * radians;
124  const ScalarType delta_longitude = (m_longitude - other.m_longitude) * radians;
125  const ScalarType sin_delta_latitude_half = sin(delta_latitude * 0.5);
126  const ScalarType sin_delta_longitude_half = sin(delta_longitude * 0.5);
127  const ScalarType first_part = sin_delta_latitude_half * sin_delta_latitude_half;
128  const ScalarType second_part = cos(m_latitude * radians) * cos(other.m_latitude * radians) *
129  sin_delta_longitude_half * sin_delta_longitude_half;
130  const ScalarType distance = 2.0 * earth_radius.kilometers() * asin(sqrt(first_part + second_part));
131  return kilometers(distance);
132  }
133 
140  {
141  return m_latitude;
142  }
143 
150  {
151  return m_longitude;
152  }
153 
160  {
161  m_latitude = lat;
162  check_input();
163  }
164 
171  {
172  m_longitude = lon;
173  check_input();
174  }
175 
176 private:
180  void check_input() const
181  {
182  assert(m_latitude <= 90. && m_latitude >= -90. && "Latitude must be in [-90, 90]");
183  assert(m_longitude <= 180. && m_longitude >= -180. && "Longitude must be in [-180, 180]");
184  }
185 
189  constexpr static ScalarType radians = std::numbers::pi / 180.0;
190 };
191 
192 } // namespace geo
193 
194 } // namespace mio
195 
196 #endif // MIO_GEOGRAPHY_LOCATIONS_H
Represents a distance.
Definition: distance.h:37
ScalarType kilometers() const
return distance in kilometers.
Definition: distance.h:55
Class representing a geographical location on the Earth's surface.
Definition: geolocation.h:42
void set_latitude(ScalarType lat)
Set the latitude object.
Definition: geolocation.h:159
constexpr static mio::geo::Distance earth_radius
Definition: geolocation.h:188
bool operator!=(const GeographicalLocation &other) const
Compare two GeographicalLocations for inequality.
Definition: geolocation.h:86
GeographicalLocation(ScalarType lat, ScalarType lon)
Construct a new Geographical Location object.
Definition: geolocation.h:51
void set_longitude(ScalarType lon)
Set the longitude object.
Definition: geolocation.h:170
ScalarType get_longitude() const
Get the longitude object.
Definition: geolocation.h:149
bool operator==(const GeographicalLocation &other) const
Compare two GeographicalLocations for equality.
Definition: geolocation.h:78
auto default_serialize()
Default serialize the GeographicalLocation object.
Definition: geolocation.h:107
bool is_close(const GeographicalLocation &other, Distance tol=Distance(10 *Limits< ScalarType >::zero_tolerance())) const
Check that this location is within a (small) distance of another.
Definition: geolocation.h:96
ScalarType get_latitude() const
Get the latitude object.
Definition: geolocation.h:139
constexpr static ScalarType radians
Definition: geolocation.h:189
ScalarType m_longitude
Definition: geolocation.h:187
ScalarType m_latitude
Definition: geolocation.h:186
Distance distance(const GeographicalLocation &other) const
Definition: geolocation.h:121
GeographicalLocation(std::pair< ScalarType, ScalarType > coordinates)
Construct a new Geographical Location object.
Definition: geolocation.h:62
void check_input() const
Assert that the latitude and longitude are within valid ranges.
Definition: geolocation.h:180
GeographicalLocation()=default
Construct a new Geographical Location object using the default constructor to keep compatibility with...
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_sin< AD_TAPE_REAL > > sin(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:913
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_asin< AD_TAPE_REAL > > asin(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:968
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_sqrt< AD_TAPE_REAL > > sqrt(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:1023
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_cos< AD_TAPE_REAL > > cos(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:924
constexpr Distance kilometers(ScalarType kilometers)
Create a Distance of a specified number of kilometers.
Definition: distance.h:145
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Type specific limits for floating-points.
Definition: memilio/config.h:59
List of a class's members.
Definition: default_serialize.h:113
Members< ValueTypes..., T > add(const char *member_name, T &member)
Add a class member.
Definition: default_serialize.h:139