Skip to content

Commit

Permalink
Fix ellipsoidal area calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 3, 2018
1 parent 36d33ee commit 297dbe0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/core/qgsdistancearea.cpp
Expand Up @@ -645,7 +645,7 @@ void QgsDistanceArea::computeAreaInit()
} }


double a2 = ( mSemiMajor * mSemiMajor ); double a2 = ( mSemiMajor * mSemiMajor );
double e2 = 1 - ( a2 / ( mSemiMinor * mSemiMinor ) ); double e2 = 1 - ( ( mSemiMinor * mSemiMinor ) / a2 );
double e4, e6; double e4, e6;


m_TwoPI = M_PI + M_PI; m_TwoPI = M_PI + M_PI;
Expand Down
12 changes: 6 additions & 6 deletions tests/src/core/testqgsdistancearea.cpp
Expand Up @@ -239,15 +239,15 @@ void TestQgsDistanceArea::collections()
//collection of polygons //collection of polygons
QgsGeometry polys( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() ); QgsGeometry polys( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() );
result = myDa.measureArea( polys ); result = myDa.measureArea( polys );
QGSCOMPARENEAR( result, 670434859475LL, 1 ); QGSCOMPARENEAR( result, 663136985074LL, 1 );
result = myDa.measureLength( polys ); result = myDa.measureLength( polys );
QGSCOMPARENEAR( result, 0, 4 * std::numeric_limits<double>::epsilon() ); QGSCOMPARENEAR( result, 0, 4 * std::numeric_limits<double>::epsilon() );


//mixed collection //mixed collection
QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() ); QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() );
//measure area specifically //measure area specifically
result = myDa.measureArea( mixed ); result = myDa.measureArea( mixed );
QGSCOMPARENEAR( result, 670434859475LL, 1 ); QGSCOMPARENEAR( result, 663136985075LL, 1 );
//measure length //measure length
result = myDa.measureLength( mixed ); result = myDa.measureLength( mixed );
QGSCOMPARENEAR( result, 12006159, 1 ); QGSCOMPARENEAR( result, 12006159, 1 );
Expand Down Expand Up @@ -312,12 +312,12 @@ void TestQgsDistanceArea::measureAreaAndUnits()
units = da.areaUnits(); units = da.areaUnits();
QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) ); QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) );
// should always be in Meters Squared // should always be in Meters Squared
QGSCOMPARENEAR( area, 37416879192.9, 0.1 ); QGSCOMPARENEAR( area, 36918093794.1, 0.1 );
QCOMPARE( units, QgsUnitTypes::AreaSquareMeters ); QCOMPARE( units, QgsUnitTypes::AreaSquareMeters );


// test converting the resultant area // test converting the resultant area
area = da.convertAreaMeasurement( area, QgsUnitTypes::AreaSquareMiles ); area = da.convertAreaMeasurement( area, QgsUnitTypes::AreaSquareMiles );
QGSCOMPARENEAR( area, 14446.7378, 0.001 ); QGSCOMPARENEAR( area, 14254.155703, 0.001 );


// now try with a source CRS which is in feet // now try with a source CRS which is in feet
ring.clear(); ring.clear();
Expand Down Expand Up @@ -350,13 +350,13 @@ void TestQgsDistanceArea::measureAreaAndUnits()
area = da.measureArea( polygon ); area = da.measureArea( polygon );
units = da.areaUnits(); units = da.areaUnits();
QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) ); QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) );
QGSCOMPARENEAR( area, 184149.37, 1.0 ); QGSCOMPARENEAR( area, 185818.590966, 1.0 );
QCOMPARE( units, QgsUnitTypes::AreaSquareMeters ); QCOMPARE( units, QgsUnitTypes::AreaSquareMeters );


// test converting the resultant area // test converting the resultant area
area = da.convertAreaMeasurement( area, QgsUnitTypes::AreaSquareYards ); area = da.convertAreaMeasurement( area, QgsUnitTypes::AreaSquareYards );
QgsDebugMsg( QString( "measured %1 in sq yrds" ).arg( area ) ); QgsDebugMsg( QString( "measured %1 in sq yrds" ).arg( area ) );
QGSCOMPARENEAR( area, 220240.8172549, 0.3 ); QGSCOMPARENEAR( area, 222237.185213, 0.3 );
} }


void TestQgsDistanceArea::emptyPolygon() void TestQgsDistanceArea::emptyPolygon()
Expand Down

0 comments on commit 297dbe0

Please sign in to comment.