-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectionMapper.cpp
More file actions
140 lines (104 loc) · 5.34 KB
/
ProjectionMapper.cpp
File metadata and controls
140 lines (104 loc) · 5.34 KB
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
/*
* Copyright (C) 2016 Gianluca Iselli <gianluca.iselli@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "ProjectionMapper.h"
#include "Utilities.h"
ProjectionMapper::ProjectionMapper(double SW_lat, double SW_lon, double area_width, double area_height, double cell_side_size)
: m_SWLat(SW_lat), m_SWLon(SW_lon), m_AreaWidth(area_width), m_AreaHeight(area_height), m_CellSideSize(cell_side_size) {
LogD(0, "ProjectionMapper(%lf, %lf, %.2lf, %.2lf, %.2lf)\n", SW_lat, SW_lon, area_width, area_height, cell_side_size);
int err;
m_CellWidthCount = (uint)(m_AreaWidth/m_CellSideSize);
m_CellHeightCount = (uint)(m_AreaHeight/m_CellSideSize);
if (!(m_PJMerc = pj_init_plus("+proj=merc +lat_ts=0 +lon_0=0")))
DieWithError(1, "Failed to initiate merc proj\n");
if (!(m_PJLatlng = pj_init_plus("+proj=latlong +ellps=clrk66")))
DieWithError(1, "Failed to initiate latlong proj\n");
// Translate the Origin coordinates into Northings and Eastings
m_OriginNorthing = m_SWLat * DEG_TO_RAD;
m_OriginEastings = m_SWLon * DEG_TO_RAD;
if ((err = pj_transform(m_PJLatlng, m_PJMerc, 1, 1, &m_OriginEastings, &m_OriginNorthing, NULL)) != 0)
DieWithError(1, "Failed to transform (SW_lat, SW_lon) - Error code: %d Error message: %s\n", err, pj_strerrno(err));
// Add the area size to the Northing/Easting coordinate and transform
double x = area_width + m_OriginEastings;
double y = area_height + m_OriginNorthing;
if ((err = pj_transform(m_PJMerc, m_PJLatlng, 1, 1, &x, &y, NULL)) != 0)
DieWithError(1, "Failed to transform (x,y) - Error code: %d Error message: %s\n", err, pj_strerrno(err));
m_NWLat = y * RAD_TO_DEG;
m_NWLon = x * RAD_TO_DEG;
LogD(1, "NWLat: %lf, NWLon: %lf\n", m_NWLat, m_NWLon);
}
ProjectionMapper::~ProjectionMapper() {
LogD(0, "~ProjectionMapper()\n");
pj_free(m_PJMerc);
pj_free(m_PJLatlng);
}
void ProjectionMapper::LocalPosXY2IndexXY(uint pos_x, uint pos_y, uint& idx_x, uint& idx_y) {
LogD(0, "LocalPosXY2IndexXY(%d, %d, *idx_x*, *idx_y*)\n", pos_x, pos_y);
if (pos_x >= m_AreaWidth)
throw MakeException(std::out_of_range, "pos_x("+std::to_string(pos_x)+") >= m_AreaWidth");
if (pos_y >= m_AreaHeight)
throw MakeException(std::out_of_range, "pos_y("+std::to_string(pos_y)+") >= m_AreaHeight");
idx_x = (uint)round((double)pos_x / m_CellSideSize);
idx_y = (uint)round((double)pos_y / m_CellSideSize);
if (idx_x >= m_CellWidthCount)
idx_x = m_CellWidthCount - 1;
if (idx_y >= m_CellHeightCount)
idx_y = m_CellHeightCount - 1;
LogD(1, "LocalPosXY2IndexXY (%d, %d) -> idx_x: %d idx_y: %d\n", pos_x, pos_y, idx_x, idx_y);
}
void ProjectionMapper::LocalPosXY2LatLng(uint pos_x, uint pos_y, double& lat, double& lng) {
LogD(0, "LocalPosXPosY2LatLng(%d, %d, *lat*, *lng*)\n", pos_x, pos_y);
int err;
if (pos_x >= m_AreaWidth)
throw MakeException(std::out_of_range, "pos_x("+std::to_string(pos_x)+") >= m_AreaWidth");
if (pos_y >= m_AreaHeight)
throw MakeException(std::out_of_range, "pos_y("+std::to_string(pos_y)+") >= m_AreaHeight");
double x = pos_x + m_OriginEastings;
double y = pos_y + m_OriginNorthing;
if ((err = pj_transform(m_PJMerc, m_PJLatlng, 1, 1, &x, &y, NULL)) != 0)
DieWithError(1, "Failed to transform (x,y) - Error code: %d Error message: %s\n", err, pj_strerrno(err));
lat = y * RAD_TO_DEG;
lng = x * RAD_TO_DEG;
LogD(1, "LocalPosXY2LatLng (%d, %d) -> lat: %lf lng: %lf\n", pos_x, pos_y, lat, lng);
}
void ProjectionMapper::LatLng2IndexXY(double lat, double lng, uint& idx_x, uint& idx_y) {
LogD(0, "LatLng2IndexXY(%lf, %lf, *idx_x*, *idx_y*)\n", lat, lng);
int err;
double pos_x, pos_y;
double east = lng * DEG_TO_RAD;
double north = lat * DEG_TO_RAD;
if ((err = pj_transform(m_PJLatlng, m_PJMerc, 1, 1, &east, &north, NULL)) != 0)
DieWithError(1, "Failed to transform (SW_lat, SW_lon) - Error code: %d Error message: %s\n", err, pj_strerrno(err));
// TODO: this round is a little hack.. Is it right?
pos_x = east - m_OriginEastings;
pos_y = north - m_OriginNorthing;
if (pos_x >= m_AreaWidth)
throw MakeException(std::out_of_range, "pos_x("+std::to_string(pos_x)+") >= m_AreaWidth");
if (pos_x < 0)
throw MakeException(std::out_of_range, "pos_x("+std::to_string(pos_x)+") < 0");
if (pos_y >= m_AreaHeight)
throw MakeException(std::out_of_range, "pos_y("+std::to_string(pos_y)+") > m_AreaHeight");
if (pos_y < 0)
throw MakeException(std::out_of_range, "pos_y("+std::to_string(pos_y)+") < 0");
idx_x = (uint)round(pos_x / m_CellSideSize);
idx_y = (uint)round(pos_y / m_CellSideSize);
if (idx_x >= m_CellWidthCount)
idx_x = m_CellWidthCount - 1;
if (idx_y >= m_CellHeightCount)
idx_y = m_CellHeightCount - 1;
LogD(1, "LatLng2IndexXY (%lf, %lf) -> pos_x/y(%.3lf, %.3lf) -> idx_x: %d idx_y: %d\n", lat, lng, pos_x, pos_y, idx_x, idx_y);
}