Stride Reference Manual  1.0
GeoCoordCalculator.cpp
Go to the documentation of this file.
1 #include "util/GeoCoordinate.h"
3 
4 using namespace stride;
5 using namespace util;
6 using namespace std;
7 
8 double earth_radius = 6371;
9 
12  static GeoCoordCalculator calc;
13 
14  return calc;
15 }
16 
17 double GeoCoordCalculator::getDistance(const GeoCoordinate& coord1, const GeoCoordinate& coord2) const {
18  double delta_latitude = coord2.m_latitude - coord1.m_latitude;
19  double delta_longitude = coord2.m_longitude - coord1.m_longitude;
20 
21  double temp1 = sin(delta_latitude * PI / 360.0) * sin(delta_latitude * PI / 360.0) +
22  cos(coord1.m_latitude * PI / 180.0) * cos(coord2.m_latitude * PI / 180.0) *
23  sin(delta_longitude * PI / 360.0) * sin(delta_longitude * PI / 360.0);
24 
25  double temp2 = 2.0 * asin(min(1.0, sqrt(temp1)));
26 
27  return earth_radius * temp2;
28 }
29 
30 void GeoCoordCalculator::convertToRegularCoordinates(double& latitude, double& longitude) const {
31  bool changed = false;
32 
33  while (latitude > 270) {
34  latitude -= 360;
35  }
36 
37  while (latitude < -270) {
38  latitude += 360;
39  }
40 
41  while (longitude > 180) {
42  longitude -= 360;
43  }
44 
45  while (longitude < -180) {
46  longitude += 360;
47  }
48 
49  if (latitude < -90.0) {
50  latitude += 180;
51  latitude = -latitude;
52  longitude += 180;
53  changed = true;
54  }
55 
56  if (latitude > 90) {
57  latitude -= 180;
58  latitude = -latitude;
59  longitude += 180;
60  changed = true;
61  }
62 
63  if (longitude < -180.0) {
64  changed = true;
65  }
66 
67  if (longitude > 180) {
68  longitude -= 180.0;
69  changed = true;
70  }
71 
72  if (changed) {
73  convertToRegularCoordinates(latitude, longitude);
74  }
75 }
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
#define PI
Definition: popgen/utils.h:13
STL namespace.
double earth_radius
void convertToRegularCoordinates(double &latitude, double &longitude) const
double getDistance(const GeoCoordinate &coord1, const GeoCoordinate &coord2) const
static const GeoCoordCalculator & getInstance()
Singleton pattern.