Skip to content

Commit

Permalink
Fix incorrect area calculation for polygon (fix #14675)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 15, 2016
1 parent a6cb81b commit bf4cf51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/qgsdistancearea.cpp
Expand Up @@ -930,7 +930,8 @@ double QgsDistanceArea::computePolygonArea( const QList<QgsPoint>& points ) cons
dx = x2 - x1;
area += dx * ( m_Qp - getQ( y2 ) );

if (( dy = y2 - y1 ) != 0.0 )
dy = y2 - y1;
if ( !qgsDoubleNear( dy, 0.0 ) )
area += dx * getQ( y2 ) - ( dx / dy ) * ( Qbar2 - Qbar1 );
}
if (( area *= m_AE ) < 0.0 )
Expand Down
12 changes: 12 additions & 0 deletions tests/src/core/testqgsdistancearea.cpp
Expand Up @@ -43,6 +43,7 @@ class TestQgsDistanceArea: public QObject
void measureUnits();
void measureAreaAndUnits();
void emptyPolygon();
void regression14675();

};

Expand Down Expand Up @@ -356,6 +357,17 @@ void TestQgsDistanceArea::emptyPolygon()
da.measurePolygon( QList< QgsPoint >() );
}

void TestQgsDistanceArea::regression14675()
{
//test regression #14675
QgsDistanceArea calc;
calc.setEllipsoidalMode( true );
calc.setEllipsoid( "GRS80" );
calc.setSourceCrs( 145L );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( "Polygon ((917593.5791854317067191 6833700.00807378999888897, 917596.43389983859378844 6833700.67099479306489229, 917599.53056440979707986 6833700.78673478215932846, 917593.5791854317067191 6833700.00807378999888897))" ) );
QVERIFY( qgsDoubleNear( calc.measureArea( &geom ), 0.83301, 0.0001 ) );

This comment has been minimized.

Copy link
@m-kuhn

m-kuhn Jun 23, 2016

Member

@nyalldawson on osx we get some difference:

TestQgsDistanceArea::regression14675() Expecting 0.850590 got 0.833010 (diff 0.017580 > 0.000100)

https://travis-ci.org/qgis/QGIS/jobs/138311086#L1166

I think the regression which is tested is several orders of magnitude worse. What do you think, is there something to fix or should the tolerance be adjusted?

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Jun 23, 2016

Author Collaborator

I've noticed the calculations seem a bit unstable for very small areas (such as those tested here). I think adjusting the tolerance is correct here, although we should possibly look at automatically switching to something like
https://www.mapbox.com/blog/cheap-ruler/
for small area calculation.

}

QTEST_MAIN( TestQgsDistanceArea )
#include "testqgsdistancearea.moc"

Expand Down

0 comments on commit bf4cf51

Please sign in to comment.