Stride Reference Manual  1.0
GeoCoordCalculator.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <random>
5 #include <cmath>
6 
7 #include "util/GeoCoordinate.h"
8 
9 namespace stride {
10 namespace util {
11 
12 using namespace std;
13 
16 public:
17  static const GeoCoordCalculator& getInstance();
18 
19  double getDistance(const GeoCoordinate& coord1, const GeoCoordinate& coord2) const;
23 
24  template<class T>
26  const GeoCoordinate& coord,
27  double radius,
28  T rng) const {
31  double temp2 = radius / 6371;
32  double temp1 = sin(temp2 / 2.0) * sin(temp2 / 2.0);
33 
34  double max_delta_latitude = asin(sqrt(temp1)) * 360.0 / PI;
35 
36  double my_cos = cos(coord.m_latitude * PI / 180.0);
37  double my_pow = pow(my_cos, 2);
38  double max_delta_longitude = asin(sqrt(temp1 / my_pow)) * 360.0 / PI;
39 
40  std::uniform_real_distribution<double> dist_longitude(-max_delta_longitude, max_delta_longitude);
41  std::uniform_real_distribution<double> dist_latitude(-max_delta_latitude, max_delta_latitude);
42  GeoCoordinate random_coordinate;
43 
44  do {
45  double new_longitude = coord.m_longitude + dist_longitude(rng);
46  double new_latitude = coord.m_latitude + dist_latitude(rng);
47 
48 
49  random_coordinate.m_longitude = new_longitude;
50  random_coordinate.m_latitude = new_latitude;
51  this->convertToRegularCoordinates(new_latitude, new_longitude);
52 
53  } while (getDistance(coord, random_coordinate) > radius);
54  return random_coordinate;
55  }
58 
59  GeoCoordinate getMiddle(const GeoCoordinate& coord1, const GeoCoordinate& coord2) const {
62  double lon1_rad = coord1.m_longitude * PI / 180;
63  double lon2_rad = coord2.m_longitude * PI / 180;
64  double lat1_rad = coord1.m_latitude * PI / 180;
65  double lat2_rad = coord2.m_latitude * PI / 180;
66 
67  double lon_diff = lon2_rad - lon1_rad;
68  double x = cos(lat2_rad) * cos(lon_diff);
69  double y = cos(lat2_rad) * sin(lon_diff);
70 
71  double center_lat = atan2(sin(lat1_rad) + sin(lat2_rad),
72  sqrt((cos(lat1_rad) + x) * (cos(lat1_rad) + x) + y * y));
73  double center_lon = lon1_rad + atan2(y, cos(lat1_rad) + x);
74 
75  return GeoCoordinate(center_lat * 180 / PI, center_lon * 180 / PI);
76  }
77 
78  void convertToRegularCoordinates(double& latitude, double& longitude) const;
79 
80 private:
82 
84 
85  GeoCoordCalculator(GeoCoordCalculator const&) = delete;
86 
87  void operator=(GeoCoordCalculator const&) = delete;
88 };
89 
90 }
91 }
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
GeoCoordinate generateRandomCoord(const GeoCoordinate &coord, double radius, T rng) const
Result is in kilometers Uses the haversine formula See: http://www.movable-type.co.uk/scripts/latlong.html.
#define PI
Definition: popgen/utils.h:13
STL namespace.
GeoCoordinate getMiddle(const GeoCoordinate &coord1, const GeoCoordinate &coord2) const
radius is in kilometres TODO make the distribution fair