@@ -40,9 +40,11 @@ class TestQgsMeasureTool : public QObject
40
40
void cleanupTestCase ();// will be called after the last testfunction was executed.
41
41
void init () {} // will be called before each testfunction is executed.
42
42
void cleanup () {} // will be called after every testfunction.
43
- void testLengthCalculation ();
43
+ void testLengthCalculationCartesian ();
44
+ void testLengthCalculationProjected ();
44
45
void testLengthCalculationNoCrs ();
45
- void testAreaCalculation ();
46
+ void testAreaCalculationCartesian ();
47
+ void testAreaCalculationProjected ();
46
48
void degreeDecimalPlaces ();
47
49
48
50
private:
@@ -80,7 +82,7 @@ void TestQgsMeasureTool::cleanupTestCase()
80
82
QgsApplication::exitQgis ();
81
83
}
82
84
83
- void TestQgsMeasureTool::testLengthCalculation ()
85
+ void TestQgsMeasureTool::testLengthCalculationCartesian ()
84
86
{
85
87
// test length measurement
86
88
QgsSettings s;
@@ -97,6 +99,75 @@ void TestQgsMeasureTool::testLengthCalculation()
97
99
std::unique_ptr< QgsMeasureTool > tool ( new QgsMeasureTool ( mCanvas , false ) );
98
100
std::unique_ptr< QgsMeasureDialog > dlg ( new QgsMeasureDialog ( tool.get () ) );
99
101
102
+ dlg->mCartesian ->setChecked ( true );
103
+
104
+ tool->restart ();
105
+ tool->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
106
+ tool->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
107
+ // force dialog recalculation
108
+ dlg->addPoint ();
109
+
110
+ // check result
111
+ QString measureString = dlg->editTotal ->text ();
112
+ double measured = measureString.remove ( ' ,' ).split ( ' ' ).at ( 0 ).toDouble ();
113
+ double expected = 26930.637 ;
114
+ QGSCOMPARENEAR ( measured, expected, 0.001 );
115
+
116
+ // change project length unit, check calculation respects unit
117
+ QgsProject::instance ()->setDistanceUnits ( QgsUnitTypes::DistanceFeet );
118
+ std::unique_ptr< QgsMeasureTool > tool2 ( new QgsMeasureTool ( mCanvas , false ) );
119
+ std::unique_ptr< QgsMeasureDialog > dlg2 ( new QgsMeasureDialog ( tool2.get () ) );
120
+ dlg2->mCartesian ->setChecked ( true );
121
+
122
+ tool2->restart ();
123
+ tool2->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
124
+ tool2->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
125
+ // force dialog recalculation
126
+ dlg2->addPoint ();
127
+
128
+ // check result
129
+ measureString = dlg2->editTotal ->text ();
130
+ measured = measureString.remove ( ' ,' ).split ( ' ' ).at ( 0 ).toDouble ();
131
+ expected = 88355.108 ;
132
+ QGSCOMPARENEAR ( measured, expected, 0.001 );
133
+
134
+ // check new CoordinateReferenceSystem, points must be reprojected to paint them successfully (issue #15182)
135
+ QgsCoordinateReferenceSystem srs2 ( 4326 , QgsCoordinateReferenceSystem::EpsgCrsId );
136
+
137
+ QgsCoordinateTransform ct ( srs, srs2, QgsProject::instance () );
138
+
139
+ QgsPointXY p0 = ct.transform ( tool2->points ()[0 ] );
140
+ QgsPointXY p1 = ct.transform ( tool2->points ()[1 ] );
141
+
142
+ mCanvas ->setDestinationCrs ( srs2 );
143
+
144
+ QgsPointXY n0 = tool2->points ()[0 ];
145
+ QgsPointXY n1 = tool2->points ()[1 ];
146
+
147
+ QGSCOMPARENEAR ( p0.x (), n0.x (), 0.001 );
148
+ QGSCOMPARENEAR ( p0.y (), n0.y (), 0.001 );
149
+ QGSCOMPARENEAR ( p1.x (), n1.x (), 0.001 );
150
+ QGSCOMPARENEAR ( p1.y (), n1.y (), 0.001 );
151
+
152
+ }
153
+ void TestQgsMeasureTool::testLengthCalculationProjected ()
154
+ {
155
+ // test length measurement
156
+ QgsSettings s;
157
+ s.setValue ( QStringLiteral ( " /qgis/measure/keepbaseunit" ), true );
158
+
159
+ // set project CRS and ellipsoid
160
+ QgsCoordinateReferenceSystem srs ( 3111 , QgsCoordinateReferenceSystem::EpsgCrsId );
161
+ mCanvas ->setDestinationCrs ( srs );
162
+ QgsProject::instance ()->setCrs ( srs );
163
+ QgsProject::instance ()->setEllipsoid ( QStringLiteral ( " WGS84" ) );
164
+ QgsProject::instance ()->setDistanceUnits ( QgsUnitTypes::DistanceMeters );
165
+
166
+ // run length calculation
167
+ std::unique_ptr< QgsMeasureTool > tool ( new QgsMeasureTool ( mCanvas , false ) );
168
+ std::unique_ptr< QgsMeasureDialog > dlg ( new QgsMeasureDialog ( tool.get () ) );
169
+ dlg->mEllipsoidal ->setChecked ( true );
170
+
100
171
tool->restart ();
101
172
tool->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
102
173
tool->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
@@ -113,6 +184,7 @@ void TestQgsMeasureTool::testLengthCalculation()
113
184
QgsProject::instance ()->setDistanceUnits ( QgsUnitTypes::DistanceFeet );
114
185
std::unique_ptr< QgsMeasureTool > tool2 ( new QgsMeasureTool ( mCanvas , false ) );
115
186
std::unique_ptr< QgsMeasureDialog > dlg2 ( new QgsMeasureDialog ( tool2.get () ) );
187
+ dlg2->mEllipsoidal ->setChecked ( true );
116
188
117
189
tool2->restart ();
118
190
tool2->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
@@ -145,6 +217,7 @@ void TestQgsMeasureTool::testLengthCalculation()
145
217
QGSCOMPARENEAR ( p1.y (), n1.y (), 0.001 );
146
218
}
147
219
220
+
148
221
void TestQgsMeasureTool::testLengthCalculationNoCrs ()
149
222
{
150
223
// test length measurement when no projection is set
@@ -172,7 +245,7 @@ void TestQgsMeasureTool::testLengthCalculationNoCrs()
172
245
QGSCOMPARENEAR ( measured, expected, 0.001 );
173
246
}
174
247
175
- void TestQgsMeasureTool::testAreaCalculation ()
248
+ void TestQgsMeasureTool::testAreaCalculationCartesian ()
176
249
{
177
250
// test area measurement
178
251
QgsSettings s;
@@ -189,6 +262,62 @@ void TestQgsMeasureTool::testAreaCalculation()
189
262
std::unique_ptr< QgsMeasureTool > tool ( new QgsMeasureTool ( mCanvas , true ) );
190
263
std::unique_ptr< QgsMeasureDialog > dlg ( new QgsMeasureDialog ( tool.get () ) );
191
264
265
+ dlg->mCartesian ->setChecked ( true );
266
+
267
+ tool->restart ();
268
+ tool->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
269
+ tool->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
270
+ tool->addPoint ( QgsPointXY ( 2520109 , 2397715 ) );
271
+ tool->addPoint ( QgsPointXY ( 2520792 , 2425494 ) );
272
+ // force dialog recalculation
273
+ dlg->addPoint ();
274
+
275
+ // check result
276
+ QString measureString = dlg->editTotal ->text ();
277
+ double measured = measureString.remove ( ' ,' ).split ( ' ' ).at ( 0 ).toDouble ();
278
+ double expected = 1005640568.0 ;
279
+ QGSCOMPARENEAR ( measured, expected, 1.0 );
280
+
281
+ // change project area unit, check calculation respects unit
282
+ QgsProject::instance ()->setAreaUnits ( QgsUnitTypes::AreaSquareMiles );
283
+ std::unique_ptr< QgsMeasureTool > tool2 ( new QgsMeasureTool ( mCanvas , true ) );
284
+ std::unique_ptr< QgsMeasureDialog > dlg2 ( new QgsMeasureDialog ( tool2.get () ) );
285
+ dlg2->mCartesian ->setChecked ( true );
286
+
287
+ tool2->restart ();
288
+ tool2->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
289
+ tool2->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
290
+ tool2->addPoint ( QgsPointXY ( 2520109 , 2397715 ) );
291
+ tool2->addPoint ( QgsPointXY ( 2520792 , 2425494 ) );
292
+ // force dialog recalculation
293
+ dlg2->addPoint ();
294
+
295
+ // check result
296
+ measureString = dlg2->editTotal ->text ();
297
+ measured = measureString.remove ( ' ,' ).split ( ' ' ).at ( 0 ).toDouble ();
298
+ expected = 388.280 ;
299
+ QGSCOMPARENEAR ( measured, expected, 0.001 );
300
+ }
301
+
302
+ void TestQgsMeasureTool::testAreaCalculationProjected ()
303
+ {
304
+ // test area measurement
305
+ QgsSettings s;
306
+ s.setValue ( QStringLiteral ( " /qgis/measure/keepbaseunit" ), true );
307
+
308
+ // set project CRS and ellipsoid
309
+ QgsCoordinateReferenceSystem srs ( 3111 , QgsCoordinateReferenceSystem::EpsgCrsId );
310
+ mCanvas ->setDestinationCrs ( srs );
311
+ QgsProject::instance ()->setCrs ( srs );
312
+ QgsProject::instance ()->setEllipsoid ( QStringLiteral ( " WGS84" ) );
313
+ QgsProject::instance ()->setAreaUnits ( QgsUnitTypes::AreaSquareMeters );
314
+
315
+ // run length calculation
316
+ std::unique_ptr< QgsMeasureTool > tool ( new QgsMeasureTool ( mCanvas , true ) );
317
+ std::unique_ptr< QgsMeasureDialog > dlg ( new QgsMeasureDialog ( tool.get () ) );
318
+
319
+ dlg->mEllipsoidal ->setChecked ( true );
320
+
192
321
tool->restart ();
193
322
tool->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
194
323
tool->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
@@ -208,6 +337,8 @@ void TestQgsMeasureTool::testAreaCalculation()
208
337
std::unique_ptr< QgsMeasureTool > tool2 ( new QgsMeasureTool ( mCanvas , true ) );
209
338
std::unique_ptr< QgsMeasureDialog > dlg2 ( new QgsMeasureDialog ( tool2.get () ) );
210
339
340
+ dlg2->mEllipsoidal ->setChecked ( true );
341
+
211
342
tool2->restart ();
212
343
tool2->addPoint ( QgsPointXY ( 2484588 , 2425722 ) );
213
344
tool2->addPoint ( QgsPointXY ( 2482767 , 2398853 ) );
0 commit comments