-
Notifications
You must be signed in to change notification settings - Fork 105
/
georeferencing.h
717 lines (575 loc) · 18.2 KB
/
georeferencing.h
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Copyright 2012-2019 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering 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.
*
* OpenOrienteering 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 OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPENORIENTEERING_GEOREFERENCING_H
#define OPENORIENTEERING_GEOREFERENCING_H
#include <cmath>
#include <vector>
#include <QObject>
#include <QPointF>
#include <QString>
#include <QTransform>
#include "core/latlon.h"
#include "core/map_coord.h"
class QDebug;
class QXmlStreamReader;
class QXmlStreamWriter;
// IWYU pragma: no_forward_declare QPointF
#ifdef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
using ProjTransformData = void;
#else
using ProjTransformData = struct PJconsts;
#endif
namespace OpenOrienteering {
/**
* A utility which encapsulates PROJ API variants and resource management.
*/
struct ProjTransform
{
ProjTransform() noexcept = default;
ProjTransform(const ProjTransform&) = delete;
ProjTransform(ProjTransform&& other) noexcept;
ProjTransform(const QString& crs_spec);
~ProjTransform();
ProjTransform& operator=(const ProjTransform& other) = delete;
ProjTransform& operator=(ProjTransform&& other) noexcept;
bool isValid() const noexcept;
bool isGeographic() const;
QPointF forward(const LatLon& lat_lon, bool* ok) const;
LatLon inverse(const QPointF& projected, bool* ok) const;
QString errorText() const;
private:
ProjTransform(ProjTransformData* pj) noexcept;
ProjTransformData* pj = nullptr;
};
/**
* A Georeferencing defines a mapping between "map coordinates" (as measured on
* paper) and coordinates in the real world. It provides functions for
* converting coordinates from one coordinate system to another.
*
* Conversions between map coordinates and "projected coordinates" (flat metric
* coordinates in a projected coordinate reference system) are made as affine
* transformation based on the map scale (principal scale), grid scale factor,
* the grivation and a defined reference point.
*
* Conversions between projected coordinates and geographic coordinates (here:
* latitude/longitude for the WGS84 datum) are made based on a specification
* of the coordinate reference system of the projected coordinates. The actual
* geographic transformation is done by the PROJ library for geographic
* projections.
*
* If no (valid) specification is given, the projected coordinates are regarded
* as local coordinates. Local coordinates cannot be converted to other
* geographic coordinate systems. The georeferencing is "local".
*
* Conversions between "map coordinates" and "geographic coordinates" use the
* projected coordinates as intermediate step.
*/
class Georeferencing : public QObject // clazy:exclude=copyable-polymorphic
{
Q_OBJECT
friend QDebug operator<<(QDebug dbg, const Georeferencing& georef);
public:
/// Georeferencing state
enum State
{
/// Only conversions between map and local projected coordinates are possible.
Local = 1,
/// All coordinate conversions are possible (if there is no error in the
/// crs specification).
Normal = 2
};
/**
* A shared PROJ specification of a WGS84 geographic CRS.
*/
static const QString geographic_crs_spec;
/**
* @brief Returns the precision of the grid scale factor.
*
* The precision is given in number of decimal places,
* i.e. digits after the decimal point.
*/
static constexpr unsigned int scaleFactorPrecision();
/**
* @brief Rounds according to the defined precision of the grid scale factor.
*
* @see scaleFactorPrecision();
*/
static double roundScaleFactor(double value);
/**
* @brief Returns the precision of declination/grivation/convergence.
*
* The precision is given in number of decimal places,
* i.e. digits after the decimal point.
*
* All values set as declination or grivation will be rounded to this precisison.
*/
static constexpr unsigned int declinationPrecision();
/**
* @brief Rounds according to the defined precision of declination/grivation/convergence.
*
* @see declinationPrecision();
*/
static double roundDeclination(double);
/**
* Constructs a scale-only georeferencing.
*/
Georeferencing();
/**
* Constructs a georeferencing which is a copy of an existing georeferencing.
*
* Note: Since QObjects may not be copied, this is better understood as
* creating a new object with the same settings.
*/
Georeferencing(const Georeferencing& other);
/**
* Cleans up memory allocated by the georeferencing
*/
~Georeferencing() override;
/**
* Saves the georeferencing to an XML stream.
*/
void save(QXmlStreamWriter& xml) const;
/**
* Creates a georeferencing from an XML stream.
*/
void load(QXmlStreamReader& xml, bool load_scale_only);
/**
* Assigns the properties of another Georeferencing to this one.
*
* Having this method in a QObject is in contradiction to Qt conventions.
* But we really need to assign properties from one object to another,
* where each object maintains its own identity.
*/
Georeferencing& operator=(const Georeferencing& other);
/**
* Returns if the georeferencing settings are valid.
*
* This means that coordinates can be converted between map and projected
* coordinates. isLocal() can be checked to determine if a conversion
* to geographic coordinates is also possible.
*/
bool isValid() const;
/**
* Returns true if this georeferencing is local.
*
* A georeferencing is local if no (valid) coordinate system specification
* is given for the projected coordinates. A local georeferencing cannot
* convert coordinates from and to geographic coordinate systems.
*/
bool isLocal() const;
/**
* Returns true if the "projected CRS" is actually geographic.
*
* \see proj_angular_output(PJ *, enum PJ_DIRECTION) in PROJ
*/
bool isGeographic() const;
/**
* Returns the georeferencing state.
*/
State getState() const;
/**
* Sets the georeferencing state.
*
* This is only necessary to decrease the state to Local, as otherwise it
* will be automatically changed when setting the respective values.
*/
void setState(State value);
/**
* Returns the principal scale denominator.
*
* The principal scale - or representative fraction - is the ratio between
* units on the printed map and units on ground.
*/
unsigned int getScaleDenominator() const;
/**
* Sets the principal scale denominator.
*/
void setScaleDenominator(int value);
/**
* Returns the grid scale factor.
*
* The grid scale factor is the ratio between a length in the grid and the
* length on the earth model. It is applied as a factor to ground distances
* to get grid plane distances.
*
* Mapper doesn't explicitly deal with any other factors (elevation factor,
* unit of measurement). Technically, this property can be used as a
* combined factor.
*/
double getGridScaleFactor() const;
/**
* Sets the grid scale factor.
*
* \see getGridScaleFactor()
*/
void setGridScaleFactor(double value);
/**
* Returns the magnetic declination (in degrees).
*
* @see setDeclination()
*/
double getDeclination() const;
/**
* Sets the magnetic declination (in degrees).
*
* Magnetic declination is the angle between magnetic north and true north.
* In the context of OpenOrienteering Mapper, it is the angle between the
* y axis of map coordinates and the latitude axis of geographic
* coordinates.
*
* This will also affect the grivation value and the transformations.
*/
void setDeclination(double value);
/**
* Returns the grivation.
*
* @see setGrivation()
*/
double getGrivation() const;
/**
* Returns the deviation of the grivation from the one given in pre-0.6 files.
*
* Only valid immediately after loading a georeferencing from a file.
* Returns 0.0 in any other context.
*
* Files from Mapper versions before 0.6 may have used any number of decimal
* places for grivation. Since version 0.6, grivation is rounded to the
* number of decimal places defined by declinationPrecision(). When this
* rounding takes place (i.e. only when opening a file which has not been
* saved by 0.6 or later), the difference between the original value and the
* rounded value is temporarily provided by this function. This value can be
* used to for correcting dependent data. Any changes to declination or
* grivation will invalidate this value.
*
* @see getGrivation()
* @see declinationPrecision()
*/
double getGrivationError() const;
/**
* Sets the grivation (in degrees).
*
* Grivation is the angle between magnetic north and grid north.
* In the context of OpenOrienteering Mapper, it is the angle between the y
* axes of map coordinates and projected coordinates.
*
* This will also affect the declination value and the transformations.
*/
void setGrivation(double value);
/**
* Returns the map coordinates of the reference point.
*/
MapCoord getMapRefPoint() const;
/**
* Defines the map coordinates of the reference point.
*
* This will <b>not</b> update the map and geographic coordinates of the reference point.
*/
void setMapRefPoint(const MapCoord& point);
/**
* Returns the projected coordinates of the reference point.
*/
QPointF getProjectedRefPoint() const;
/**
* Defines the projected coordinates of the reference point.
*
* This may trigger changes of the geographic coordinates of the reference
* point, the convergence, the grivation and the transformations.
*/
void setProjectedRefPoint(const QPointF& point, bool update_grivation = true);
/**
* Returns the unique id of the projected CRS.
*/
QString getProjectedCRSId() const;
/**
* Returns the name of the coordinate reference system (CRS) of the
* projected coordinates.
*/
QString getProjectedCRSName() const;
/**
* Returns the name of the projected coordinates.
*/
QString getProjectedCoordinatesName() const;
/**
* Returns the array of projected crs parameter values.
*/
const std::vector<QString>& getProjectedCRSParameters() const;
/**
* Returns the specification of the coordinate reference system (CRS) of the
* projected coordinates
* @return a PROJ specification of the CRS
*/
QString getProjectedCRSSpec() const { return projected_crs_spec; }
/**
* Sets the coordinate reference system (CRS) of the projected coordinates.
*
* This will not touch any of the reference points, the declination, the
* grivation. It is up to the user to decide how to reestablish a valid
* configuration of geographic reference point, projected reference point,
* declination and grivation.
*
* @param id an identifier
* @param spec the PROJ specification of the CRS
* @param params parameter values (ignore for empty spec)
* @return true if the specification is valid or empty, false otherwise
*/
bool setProjectedCRS(const QString& id, QString spec, std::vector< QString > params = std::vector<QString>());
/**
* Calculates the meridian convergence at the reference point.
*
* The meridian convergence is the angle between grid north and true north.
*
* @return zero for a local georeferencing, or a calculated approximation
*/
double getConvergence() const;
/**
* Returns the geographic coordinates of the reference point.
*/
LatLon getGeographicRefPoint() const;
/**
* Defines the geographic coordinates of the reference point.
*
* This may trigger changes of the projected coordinates of the reference
* point, the convergence, the grivation and the transformations.
*/
void setGeographicRefPoint(LatLon lat_lon, bool update_grivation = true);
/**
* Transforms map (paper) coordinates to projected coordinates.
*/
QPointF toProjectedCoords(const MapCoord& map_coords) const;
/**
* Transforms map (paper) coordinates to projected coordinates.
*/
QPointF toProjectedCoords(const MapCoordF& map_coords) const;
/**
* Transforms projected coordinates to map (paper) coordinates.
*/
MapCoord toMapCoords(const QPointF& projected_coords) const;
/**
* Transforms projected coordinates to map (paper) coordinates.
*/
MapCoordF toMapCoordF(const QPointF& projected_coords) const;
/**
* Transforms map (paper) coordinates to geographic coordinates (lat/lon).
*/
LatLon toGeographicCoords(const MapCoordF& map_coords, bool* ok = 0) const;
/**
* Transforms CRS coordinates to geographic coordinates (lat/lon).
*/
LatLon toGeographicCoords(const QPointF& projected_coords, bool* ok = 0) const;
/**
* Transforms geographic coordinates (lat/lon) to CRS coordinates.
*/
QPointF toProjectedCoords(const LatLon& lat_lon, bool* ok = 0) const;
/**
* Transforms geographic coordinates (lat/lon) to map coordinates.
*/
MapCoord toMapCoords(const LatLon& lat_lon, bool* ok = nullptr) const;
/**
* Transforms geographic coordinates (lat/lon) to map coordinates.
*/
MapCoordF toMapCoordF(const LatLon& lat_lon, bool* ok = nullptr) const;
/**
* Transforms map coordinates from the other georeferencing to
* map coordinates of this georeferencing, if possible.
*/
MapCoordF toMapCoordF(const Georeferencing* other, const MapCoordF& map_coords, bool* ok = nullptr) const;
/**
* Returns the current error text.
*/
QString getErrorText() const;
/**
* Converts a value from radians to degrees.
*/
static double radToDeg(double val);
/**
* Converts a value from degrees to radians.
*/
static double degToRad(double val);
/**
* Converts a value from degrees to a D°M'S" string.
*/
static QString degToDMS(double val);
/**
* Updates the transformation parameters between map coordinates and
* projected coordinates from the current projected reference point
* coordinates, the grivation and the scale.
*/
void updateTransformation();
/**
* Updates the grivation.
*
* The new value is calculated from the declination and the convergence.
* For a local georeferencing, the convergence is zero, and grivation
* is set to the same value as declination.
*/
void updateGrivation();
/**
* Initializes the declination.
*
* The new value is calculated from the grivation and the convergence.
* For a local georeferencing, the convergence is zero, and declination
* is set to the same value as grivation.
*/
void initDeclination();
/**
* Sets the transformation matrix from map coordinates to projected
* coordinates directly.
*/
void setTransformationDirectly(const QTransform& transform);
const QTransform& mapToProjected() const;
const QTransform& projectedToMap() const;
signals:
/**
* Indicates a change of the state property.
*/
void stateChanged();
/**
* Indicates a change to the transformation rules between map coordinates
* and projected coordinates.
*/
void transformationChanged();
/**
* Indicates a change to the projection rules between geographic coordinates
* and projected coordinates.
*/
void projectionChanged();
/**
* Indicates a change of the declination.
*
* The declination has no direct influence on projection or transformation.
* That's why there is an independent signal.
*/
void declinationChanged();
private:
void setDeclinationAndGrivation(double declination, double grivation);
State state;
unsigned int scale_denominator;
double grid_scale_factor;
double declination;
double grivation;
double grivation_error;
MapCoord map_ref_point;
QPointF projected_ref_point;
QTransform from_projected;
QTransform to_projected;
/// Projected CRS id, used for lookup in the CRS registry.
/// If the spec is directly entered as string, the id is empty.
QString projected_crs_id;
QString projected_crs_spec;
std::vector< QString > projected_crs_parameters;
ProjTransform proj_transform;
LatLon geographic_ref_point;
};
/**
* Dumps a Georeferencing to the debug output.
*
* Note that this requires a *reference*, not a pointer.
*/
QDebug operator<<(QDebug dbg, const Georeferencing &georef);
//### Georeferencing inline code ###
inline
constexpr unsigned int Georeferencing::scaleFactorPrecision()
{
return 6u;
}
inline
double Georeferencing::roundScaleFactor(double value)
{
// This must match the implementation in scaleFactorPrecision().
return floor(value*1000000.0+0.5)/1000000.0;
}
inline
constexpr unsigned int Georeferencing::declinationPrecision()
{
// This must match the implementation in declinationRound().
return 2u;
}
inline
double Georeferencing::roundDeclination(double value)
{
// This must match the implementation in declinationPrecision().
return floor(value*100.0+0.5)/100.0;
}
inline
bool Georeferencing::isValid() const
{
return state == Local || proj_transform.isValid();
}
inline
bool Georeferencing::isLocal() const
{
return state == Local;
}
inline
Georeferencing::State Georeferencing::getState() const
{
return state;
}
inline
unsigned int Georeferencing::getScaleDenominator() const
{
return scale_denominator;
}
inline
double Georeferencing::getGridScaleFactor() const
{
return grid_scale_factor;
}
inline
double Georeferencing::getDeclination() const
{
return declination;
}
inline
double Georeferencing::getGrivation() const
{
return grivation;
}
inline
double Georeferencing::getGrivationError() const
{
return grivation_error;
}
inline
MapCoord Georeferencing::getMapRefPoint() const
{
return map_ref_point;
}
inline
QPointF Georeferencing::getProjectedRefPoint() const
{
return projected_ref_point;
}
inline
QString Georeferencing::getProjectedCRSId() const
{
return projected_crs_id;
}
inline
const std::vector<QString>& Georeferencing::getProjectedCRSParameters() const
{
return projected_crs_parameters;
}
inline
LatLon Georeferencing::getGeographicRefPoint() const
{
return geographic_ref_point;
}
} // namespace OpenOrienteering
#endif