-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathmapposition.cpp
More file actions
166 lines (136 loc) · 4.58 KB
/
mapposition.cpp
File metadata and controls
166 lines (136 loc) · 4.58 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
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "mapposition.h"
#include "inpututils.h"
MapPosition::MapPosition( QObject *parent ) : QObject( parent )
{
}
PositionKit *MapPosition::positionKit() const
{
return mPositionKit;
}
void MapPosition::setPositionKit( PositionKit *newPositionKit )
{
if ( mPositionKit == newPositionKit )
return;
if ( mPositionKit )
mPositionKit->disconnect();
mPositionKit = newPositionKit;
if ( mPositionKit )
connect( mPositionKit, &PositionKit::positionChanged, this, &MapPosition::update );
emit positionKitChanged( mPositionKit );
update();
}
InputMapSettings *MapPosition::mapSettings() const
{
return mMapSettings;
}
void MapPosition::setMapSettings( InputMapSettings *newMapSettings )
{
if ( mMapSettings == newMapSettings )
return;
if ( mMapSettings )
{
mMapSettings->disconnect();
}
mMapSettings = newMapSettings;
if ( mMapSettings )
{
connect( mMapSettings, &InputMapSettings::extentChanged, this, &MapPosition::update );
connect( mMapSettings, &InputMapSettings::destinationCrsChanged, this, &MapPosition::update );
connect( mMapSettings, &InputMapSettings::mapUnitsPerPixelChanged, this, &MapPosition::update );
connect( mMapSettings, &InputMapSettings::visibleExtentChanged, this, &MapPosition::update );
connect( mMapSettings, &InputMapSettings::outputSizeChanged, this, &MapPosition::update );
connect( mMapSettings, &InputMapSettings::outputDpiChanged, this, &MapPosition::update );
}
emit mapSettingsChanged( mMapSettings );
update();
}
QgsPoint MapPosition::mapPosition() const
{
return mMapPosition;
}
QgsPointXY MapPosition::screenPosition() const
{
return mScreenPosition;
}
double MapPosition::screenAccuracy() const
{
return mScreenAccuracy;
}
void MapPosition::update()
{
// map position needs to go first
recalculateMapPosition();
recalculateScreenAccuracy();
recalculateScreenPosition();
}
void MapPosition::recalculateMapPosition()
{
QgsPoint newMapPosition;
if ( mMapSettings && mPositionKit )
{
QgsPoint geoposition = mPositionKit->positionCoordinate();
// During startup, GPS position might not be available so we do not transform empty points.
if ( !geoposition.isEmpty() )
{
QgsPointXY srcPoint = QgsPointXY( geoposition.x(), geoposition.y() );
QgsPointXY mapPositionXY = InputUtils::transformPointXY(
mPositionKit->positionCrs2D(),
mMapSettings->destinationCrs(),
mMapSettings->transformContext(),
srcPoint
);
if ( !mapPositionXY.isEmpty() )
{
newMapPosition = QgsPoint( mapPositionXY );
newMapPosition.addZValue( geoposition.z() );
}
}
}
if ( mMapPosition != newMapPosition )
{
mMapPosition = newMapPosition;
emit mapPositionChanged( mMapPosition );
}
}
void MapPosition::recalculateScreenPosition()
{
QgsPointXY newScreenPosition;
if ( mMapSettings && mPositionKit )
{
newScreenPosition = mapSettings()->coordinateToScreen( mMapPosition );
}
// We use comparison with higher epsilon to limit issues with floating point precision (in QPointF) and
// make less updates as 0.001 precision is well enough for us. QgsPointXY internally uses 1E-8 precision
if ( !InputUtils::equals( mScreenPosition, newScreenPosition, 0.001 ) )
{
mScreenPosition = newScreenPosition;
emit screenPositionChanged( mScreenPosition );
}
}
void MapPosition::recalculateScreenAccuracy()
{
double newScreenAccuracy = 2.0;
if ( mMapSettings && mPositionKit )
{
double hacc = mPositionKit->horizontalAccuracy();
if ( hacc > 0 )
{
double scpm = InputUtils::screenUnitsToMeters( mMapSettings, 1 );
if ( scpm > 0 )
newScreenAccuracy = 2 * ( hacc / scpm );
}
}
if ( !qgsDoubleNear( mScreenAccuracy, newScreenAccuracy ) )
{
mScreenAccuracy = newScreenAccuracy;
emit screenAccuracyChanged( mScreenAccuracy );
}
}