@@ -41,6 +41,8 @@ class TestQgsDistanceArea: public QObject
41
41
void regression13601 ();
42
42
void collections ();
43
43
void measureUnits ();
44
+ void measureAreaAndUnits ();
45
+
44
46
};
45
47
46
48
void TestQgsDistanceArea::initTestCase ()
@@ -250,6 +252,98 @@ void TestQgsDistanceArea::measureUnits()
250
252
QVERIFY ( qgsDoubleNear ( result, 2328.0988253106957 , 0.001 ) );
251
253
}
252
254
255
+ void TestQgsDistanceArea::measureAreaAndUnits ()
256
+ {
257
+ QgsDistanceArea da;
258
+ da.setSourceCrs ( 3452 );
259
+ da.setEllipsoidalMode ( false );
260
+ da.setEllipsoid ( " NONE" );
261
+ QgsCoordinateReferenceSystem daCRS;
262
+ daCRS.createFromSrsId ( da.sourceCrsId () );
263
+ QgsPolyline ring;
264
+ ring << QgsPoint ( 0 , 0 )
265
+ << QgsPoint ( 1 , 0 )
266
+ << QgsPoint ( 1 , 1 )
267
+ << QgsPoint ( 2 , 1 )
268
+ << QgsPoint ( 2 , 2 )
269
+ << QgsPoint ( 0 , 2 )
270
+ << QgsPoint ( 0 , 0 );
271
+ QgsPolygon poly;
272
+ poly << ring;
273
+
274
+ QScopedPointer< QgsGeometry > polygon ( QgsGeometry::fromPolygon ( poly ) );
275
+
276
+ // We check both the measured area AND the units, in case the logic regarding
277
+ // ellipsoids and units changes in future
278
+ double area = da.measureArea ( polygon.data () );
279
+ QgsUnitTypes::AreaUnit units = da.areaUnits ();
280
+
281
+ QgsDebugMsg ( QString ( " measured %1 in %2" ).arg ( area ).arg ( QgsUnitTypes::toString ( units ) ) );
282
+
283
+ QVERIFY (( qgsDoubleNear ( area, 3.0 , 0.00000001 ) && units == QgsUnitTypes::SquareDegrees )
284
+ || ( qgsDoubleNear ( area, 37176087091.5 , 0.1 ) && units == QgsUnitTypes::SquareMeters ) );
285
+
286
+ da.setEllipsoid ( " WGS84" );
287
+ area = da.measureArea ( polygon.data () );
288
+ units = da.areaUnits ();
289
+
290
+ QgsDebugMsg ( QString ( " measured %1 in %2" ).arg ( area ).arg ( QgsUnitTypes::toString ( units ) ) );
291
+ QVERIFY (( qgsDoubleNear ( area, 3.0 , 0.00000001 ) && units == QgsUnitTypes::SquareDegrees )
292
+ || ( qgsDoubleNear ( area, 37176087091.5 , 0.1 ) && units == QgsUnitTypes::SquareMeters ) );
293
+
294
+ da.setEllipsoidalMode ( true );
295
+ area = da.measureArea ( polygon.data () );
296
+ units = da.areaUnits ();
297
+
298
+ QgsDebugMsg ( QString ( " measured %1 in %2" ).arg ( area ).arg ( QgsUnitTypes::toString ( units ) ) );
299
+ // should always be in Meters Squared
300
+ QVERIFY ( qgsDoubleNear ( area, 37416879192.9 , 0.1 ) );
301
+ QCOMPARE ( units, QgsUnitTypes::SquareMeters );
302
+
303
+ // test converting the resultant area
304
+ area = da.convertAreaMeasurement ( area, QgsUnitTypes::SquareMiles );
305
+ QVERIFY ( qgsDoubleNear ( area, 14446.7378 , 0.001 ) );
306
+
307
+ // now try with a source CRS which is in feet
308
+ ring.clear ();
309
+ ring << QgsPoint ( 1850000 , 4423000 )
310
+ << QgsPoint ( 1851000 , 4423000 )
311
+ << QgsPoint ( 1851000 , 4424000 )
312
+ << QgsPoint ( 1852000 , 4424000 )
313
+ << QgsPoint ( 1852000 , 4425000 )
314
+ << QgsPoint ( 1851000 , 4425000 )
315
+ << QgsPoint ( 1850000 , 4423000 );
316
+ poly.clear ();
317
+ poly << ring;
318
+ polygon.reset ( QgsGeometry::fromPolygon ( poly ) );
319
+
320
+ da.setSourceCrs ( 27469 );
321
+ da.setEllipsoidalMode ( false );
322
+ // measurement should be in square feet
323
+ area = da.measureArea ( polygon.data () );
324
+ units = da.areaUnits ();
325
+ QgsDebugMsg ( QString ( " measured %1 in %2" ).arg ( area ).arg ( QgsUnitTypes::toString ( units ) ) );
326
+ QVERIFY ( qgsDoubleNear ( area, 2000000 , 0.001 ) );
327
+ QCOMPARE ( units, QgsUnitTypes::SquareFeet );
328
+
329
+ // test converting the resultant area
330
+ area = da.convertAreaMeasurement ( area, QgsUnitTypes::SquareYards );
331
+ QVERIFY ( qgsDoubleNear ( area, 222222.2222 , 0.001 ) );
332
+
333
+ da.setEllipsoidalMode ( true );
334
+ // now should be in Square Meters again
335
+ area = da.measureArea ( polygon.data () );
336
+ units = da.areaUnits ();
337
+ QgsDebugMsg ( QString ( " measured %1 in %2" ).arg ( area ).arg ( QgsUnitTypes::toString ( units ) ) );
338
+ QVERIFY ( qgsDoubleNear ( area, 184149.37309564 , 0.00001 ) );
339
+ QCOMPARE ( units, QgsUnitTypes::SquareMeters );
340
+
341
+ // test converting the resultant area
342
+ area = da.convertAreaMeasurement ( area, QgsUnitTypes::SquareYards );
343
+ QgsDebugMsg ( QString ( " measured %1 in sq yrds" ).arg ( area ) );
344
+ QVERIFY ( qgsDoubleNear ( area, 220240.8172549 , 0.00001 ) );
345
+ }
346
+
253
347
QTEST_MAIN ( TestQgsDistanceArea )
254
348
#include " testqgsdistancearea.moc"
255
349
0 commit comments