-
Notifications
You must be signed in to change notification settings - Fork 14
/
CoordinateConverter.cpp
163 lines (140 loc) · 5.28 KB
/
CoordinateConverter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//------------------------------------------------------
// Copyright (c) Riskaware 2015
//------------------------------------------------------
//
// system: EAGGR
//
// subsystem: Coordinate Conversion
//
//------------------------------------------------------
/// @file CoordinateConverter.cpp
///
/// Implements the EAGGR::CoordinateConversion::CoordinateConverter class.
///
/// This file is part of OpenEAGGR.
///
/// OpenEAGGR is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Lesser General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// OpenEAGGR is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Lesser General Public License for more details.
///
/// A copy of the GNU Lesser General Public License is available in COPYING.LESSER
/// or can be found at <http://www.gnu.org/licenses/>.
//------------------------------------------------------
#include <sstream>
#include "CoordinateConverter.hpp"
#include "Src/Utilities/RadianMacros.hpp"
#include "Src/EAGGRException.hpp"
namespace EAGGR
{
namespace CoordinateConversion
{
CoordinateConverter::CoordinateConverter()
{
m_wgs84CoordinateSystem = pj_init_plus("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
if (m_wgs84CoordinateSystem == NULL)
{
throw EAGGRException("Failed to create WGS84 coordinate system.");
}
m_sphereCoordinateSystem = pj_init_plus("+proj=longlat +ellps=sphere +datum=WGS84 +no_defs");
if (m_sphereCoordinateSystem == NULL)
{
throw EAGGRException("Failed to create spherical coordinate system.");
}
}
CoordinateConverter::~CoordinateConverter()
{
if (m_wgs84CoordinateSystem != NULL)
{
pj_free (m_wgs84CoordinateSystem);
}
if (m_sphereCoordinateSystem != NULL)
{
pj_free (m_sphereCoordinateSystem);
}
}
EAGGR::LatLong::SphericalAccuracyPoint CoordinateConverter::ConvertWGS84ToSphere(
const EAGGR::LatLong::Wgs84AccuracyPoint a_wgs84Point) const
{
EAGGR::Utilities::Maths::Radians latitudeRadians = a_wgs84Point.GetLatitudeInRadians();
EAGGR::Utilities::Maths::Radians longitudeRadians = a_wgs84Point.GetLongitudeInRadians();
EAGGR::Utilities::Maths::Radians convertedLatitude = latitudeRadians;
EAGGR::Utilities::Maths::Radians convertedLongitude = longitudeRadians;
// Initially assume a height of zero and perform the conversion
double z = 0.0;
int returnCode = pj_transform(
m_wgs84CoordinateSystem,
m_sphereCoordinateSystem,
m_NUM_POINTS_TO_CONVERT,
m_POINT_OFFSET,
&convertedLongitude,
&convertedLatitude,
&z);
if (returnCode != 0)
{
char* error = pj_strerrno(returnCode);
std::stringstream stream;
stream << "Coordinate transformation error: " << error;
throw EAGGRException(stream.str());
}
// First conversion provides a height offset with an offset in the latitude/longitude values
// Need to perform the conversion again on the original lat/long using the negated height offset
// This provides the correct lat/long to a good approximation.
convertedLatitude = latitudeRadians;
convertedLongitude = longitudeRadians;
z = -z;
returnCode = pj_transform(
m_wgs84CoordinateSystem,
m_sphereCoordinateSystem,
m_NUM_POINTS_TO_CONVERT,
m_POINT_OFFSET,
&convertedLongitude,
&convertedLatitude,
&z);
if (returnCode != 0)
{
char* error = pj_strerrno(returnCode);
std::stringstream stream;
stream << "Coordinate transformation error: " << error;
throw EAGGRException(stream.str());
}
return EAGGR::LatLong::SphericalAccuracyPoint(
RADIANS_IN_DEG(convertedLatitude),
RADIANS_IN_DEG(convertedLongitude),
EAGGR::LatLong::SphericalAccuracyPoint::SquareMetresToAngleAccuracy(
a_wgs84Point.GetAccuracy()));
}
EAGGR::LatLong::Wgs84AccuracyPoint CoordinateConverter::ConvertSphereToWGS84(
const EAGGR::LatLong::SphericalAccuracyPoint a_spherePoint) const
{
double latitudeRadians = a_spherePoint.GetLatitudeInRadians();
double longitudeRadians = a_spherePoint.GetLongitudeInRadians();
double z = 0.0;
int returnCode = pj_transform(
m_sphereCoordinateSystem,
m_wgs84CoordinateSystem,
m_NUM_POINTS_TO_CONVERT,
m_POINT_OFFSET,
&longitudeRadians,
&latitudeRadians,
&z);
if (returnCode != 0)
{
char* error = pj_strerrno(returnCode);
std::stringstream stream;
stream << "Coordinate transformation error: " << error;
throw EAGGRException(stream.str());
}
return EAGGR::LatLong::Wgs84AccuracyPoint(
RADIANS_IN_DEG(latitudeRadians),
RADIANS_IN_DEG(longitudeRadians),
EAGGR::LatLong::SphericalAccuracyPoint::AngleAccuracyToSquareMetres(
a_spherePoint.GetAccuracy()));
}
}
}