-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoToScreen.cpp
More file actions
266 lines (222 loc) · 9.15 KB
/
GeoToScreen.cpp
File metadata and controls
266 lines (222 loc) · 9.15 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//GeoToScreen.cpp
#include "GeoToScreen.h"
#include <iostream>
#include <QPointF>
#include <vector>
#include <QDebug>
CGeoToScreen::CGeoToScreen()
{
//Create context
CreateContext();
}
CGeoToScreen::~CGeoToScreen()
{
DestroyObjects();
}
void CGeoToScreen::CreateContext()
{
m_projContext = proj_context_create();
if (!m_projContext)
{
qDebug() << "CGeoToScreen: Failed to create context.";
}
}
void CGeoToScreen::DestroyObjects()
{
// Clean up the proj objects to prevent memory leaks
if (m_projTransform)
{
proj_destroy(m_projTransform);
}
if (m_projContext)
{
proj_context_destroy(m_projContext);
}
}
int CGeoToScreen::CreateProjection(const QGeoCoordinate& centre)
{
//Check a context is in place
if (!m_projContext)
{
qDebug() << "CGeoToScreen: No context available when creating projections.";
return -1;
}
// Construct the PROJ string with the given center coordinates
m_pjGeoCentre = proj_coord(centre.longitude(), centre.latitude(), 0, 0);
std::string projString = "+proj=laea +lat_0=" + std::to_string(m_pjGeoCentre.xyz.y ) + " +lon_0=" + std::to_string(m_pjGeoCentre.xyz.x ) + " +ellps=WGS84 +units=m";
//Create the projection transformation
PJ* p = proj_create_crs_to_crs(m_projContext, "EPSG:4326", projString.c_str(), nullptr);
if (p == nullptr)
{
qDebug() << "CGeoToScreen: Failed to create PROJ transformation pipeline.";
int err = proj_context_errno(m_projContext);
qDebug() << "PROJ error code: " << err << " (" << proj_errno_string(err) << ")";
return -1;
}
// Normalize for visualization to get lon/lat order and use degrees not radians
m_projTransform = proj_normalize_for_visualization(m_projContext, p );
if (m_projTransform == nullptr)
{
qDebug() << "CGeoToScreen: Failed to normalize PROJ transformation.";
proj_destroy(p );
return -1;
}
//Tidy up
proj_destroy(p);
return 0;
}
void CGeoToScreen::SetScreenSize(const QSizeF& screenSize)
{
m_screenSize = screenSize;
}
int CGeoToScreen::GetGeoBoundingBox(int stepVertical, int stepHorizontal, QGeoCoordinate& bbBottomLeft, QGeoCoordinate& bbTopRight)
{
//Check there is a transformation object
if (!m_projTransform)
{
qDebug() << "CGeoToScreen: No projection object created when getting bounding box. Call CreateProjection first.";
return -1;
}
//Set the extent of the bounding rectangle in projected CRS
m_pjProjBottomLeft.xyz.x = 0 - stepHorizontal;
m_pjProjBottomLeft.xyz.y = 0 - stepVertical;
m_pjProjTopRight.xyz.x = 0 + stepHorizontal;
m_pjProjTopRight.xyz.y = 0 + stepVertical;
//Do a reverse transformation
PJ_COORD pjGeoBottomLeft, pjGeoTopRight;
int retval = proj_trans_bounds(m_projContext, m_projTransform, PJ_INV,
m_pjProjBottomLeft.xyz.x, m_pjProjBottomLeft.xyz.y,
m_pjProjTopRight.xyz.x, m_pjProjTopRight.xyz.y,
&pjGeoBottomLeft.xyz.x, &pjGeoBottomLeft.xyz.y,
&pjGeoTopRight.xyz.x, &pjGeoTopRight.xyz.y,
21 //Recommended value
);
//Put into QGeoCoordinate format
bbBottomLeft = QGeoCoordinate(pjGeoBottomLeft.xyz.y, pjGeoBottomLeft.xyz.x);
bbTopRight= QGeoCoordinate(pjGeoTopRight.xyz.y, pjGeoTopRight.xyz.x);
return 0;
}
/*int CGeoToScreen::GetGeoBoundingBox(int stepVertical, int stepHorizontal, QGeoCoordinate& bbBottomLeft, QGeoCoordinate& bbTopRight)
{
//Check there is a transformation object
if (!m_projTransform)
{
qDebug() << "CGeoToScreen: No projection object created when getting bounding box. Call CreateProjection first.";
return -1;
}
//Set the extent of the bounding rectangle in projected CRS
m_pjProjBottomLeft.xyz.x = 0 - stepHorizontal;
m_pjProjBottomLeft.xyz.y = 0 - stepVertical;
m_pjProjTopRight.xyz.x = 0 + stepHorizontal;
m_pjProjTopRight.xyz.y = 0 + stepVertical;
//Do a reverse transformation
PJ_COORD pjGeoBottomLeft = proj_trans(m_projTransform, PJ_INV, m_pjProjBottomLeft);
PJ_COORD pjGeoTopRight = proj_trans(m_projTransform, PJ_INV, m_pjProjTopRight);
//Put into QGeoCoordinate format
//bbBottomLeft = QGeoCoordinate(pjGeoBottomLeft.xyz.y, pjGeoBottomLeft.xyz.x);
//bbTopRight= QGeoCoordinate(pjGeoTopRight.xyz.y, pjGeoTopRight.xyz.x);
// in case the bounding box isn't rectangular in the geographic CRS
PJ_COORD bl, br, tl, tr;
bl.xyz.x = m_pjProjBottomLeft.xyz.x; bl.xyz.y = m_pjProjBottomLeft.xyz.y;
br.xyz.x = m_pjProjTopRight.xyz.x; br.xyz.y = m_pjProjBottomLeft.xyz.y;
tl.xyz.x = m_pjProjBottomLeft.xyz.x; tl.xyz.y = m_pjProjTopRight.xyz.y;
tr.xyz.x = m_pjProjTopRight.xyz.x; tr.xyz.y = m_pjProjTopRight.xyz.y;
PJ_COORD bl_geo = proj_trans(m_projTransform, PJ_INV, bl);
PJ_COORD br_geo = proj_trans(m_projTransform, PJ_INV, br);
PJ_COORD tl_geo = proj_trans(m_projTransform, PJ_INV, tl);
PJ_COORD tr_geo = proj_trans(m_projTransform, PJ_INV, tr);
// Find the min/max lon/lat from the four corners
double min_lon = std::min({ bl_geo.lp.lam, br_geo.lp.lam, tl_geo.lp.lam, tr_geo.lp.lam });
double max_lon = std::max({ bl_geo.lp.lam, br_geo.lp.lam, tl_geo.lp.lam, tr_geo.lp.lam });
double min_lat = std::min({ bl_geo.lp.phi, br_geo.lp.phi, tl_geo.lp.phi, tr_geo.lp.phi });
double max_lat = std::max({ bl_geo.lp.phi, br_geo.lp.phi, tl_geo.lp.phi, tr_geo.lp.phi });
// Put into QGeoCoordinate format (requires degrees)
bbBottomLeft = QGeoCoordinate(min_lat, min_lon );
bbTopRight = QGeoCoordinate(max_lat, max_lon);
return 0;
}*/
int CGeoToScreen::transform(const QGeoPolygon& geoPolygon, QPolygonF& screenPolygon)
{
// Ensure the transformation object has been created
if (!m_projTransform)
{
qWarning() << "CGeoToScreen: No projection created. Call CreateProjection first.";
screenPolygon = QPolygonF();
return -1;
}
// The number of points in the input polygon
int pointCount = geoPolygon.size();
if (pointCount == 0)
{
screenPolygon.clear();
return 0; // Nothing to do
}
// Create vectors to hold coordinates which deals with memory management
std::vector<double> lons(pointCount);
std::vector<double> lats(pointCount);
// Copy the coordinates into the arrays
for (int i = 0; i < pointCount; ++i)
{
lons[i] = geoPolygon.coordinateAt(i).longitude();
lats[i] = geoPolygon.coordinateAt(i).latitude();
}
// Perform the core transformation using the PROJ library
int result = proj_trans_generic(
m_projTransform, PJ_FWD,
lons.data(), sizeof(double), pointCount,
lats.data(), sizeof(double), pointCount,
nullptr, 0, 0, nullptr, 0, 0
);
if (result < 0)
{
qCritical() << "CGeoToScreen: Failed to transform coordinates:" << proj_errno_string(proj_context_errno(PJ_DEFAULT_CTX));
screenPolygon.clear();
return -1;
}
// Pre-calculate constants to avoid redundant calculations in the loop
const double projWidth = m_pjProjTopRight.xyz.x - m_pjProjBottomLeft.xyz.x;
const double projHeight = m_pjProjTopRight.xyz.y - m_pjProjBottomLeft.xyz.y;
if (projWidth == 0.0 || projHeight == 0.0)
{
qWarning() << "CGeoToScreen: Projected bounding box has zero width or height.";
screenPolygon.clear();
return -1;
}
// Now convert the projected coordinates (in meters) to screen coordinates (in pixels)
for (int i = 0; i < pointCount; ++i)
{
double screenX = ((lons[i] - m_pjProjBottomLeft.xyz.x) / projWidth) * m_screenSize.width();
// Map the y-coordinate from projected space to screen pixels, and flip the Y-axis
double screenY = ((m_pjProjTopRight.xyz.y - lats[i]) / projHeight) * m_screenSize.height();
screenPolygon.append(QPointF(screenX, screenY));
}
return 0;
}
int CGeoToScreen::transformPoint(const QGeoCoordinate& geoPoint, QPointF& screenPoint)
{
// Ensure the transformation object has been created
if (!m_projTransform)
{
qWarning() << "CGeoToScreen: No projection created in transformPoint. Call CreateProjection first.";
return -1;
}
//Transform the geoPoint to projection
PJ_COORD pjGeoPoint;
pjGeoPoint.xyz.x = geoPoint.longitude();
pjGeoPoint.xyz.y = geoPoint.latitude();
PJ_COORD pjProjPoint = proj_trans(m_projTransform, PJ_FWD, pjGeoPoint);
// Calculate width and height as easier to debug
const double projWidth = m_pjProjTopRight.xyz.x - m_pjProjBottomLeft.xyz.x;
const double projHeight = m_pjProjTopRight.xyz.y - m_pjProjBottomLeft.xyz.y;
if (projWidth == 0.0 || projHeight == 0.0)
{
qWarning() << "CGeoToScreen: Projected bounding box has zero width or height.";
return -1;
}
// Now convert the projected coordinate (in meters) to screen coordinates (in pixels)
double screenX = ((pjProjPoint.xyz.x - m_pjProjBottomLeft.xyz.x) / projWidth) * m_screenSize.width();
double screenY = ((m_pjProjTopRight.xyz.y - pjProjPoint.xyz.y ) / projHeight) * m_screenSize.height();
screenPoint.setX(screenX);
screenPoint.setY(screenY);
return 0;
}