Skip to content

Commit 61bb2ac

Browse files
authored
Merge pull request #4828 from nyalldawson/fix_16820
Fix incorrect area calculation in corner cases (fix #16820)
2 parents dae0bf8 + d850393 commit 61bb2ac

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/core/qgsdistancearea.cpp

+29-4
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,14 @@ double QgsDistanceArea::computePolygonArea( const QList<QgsPointXY> &points ) co
678678
double Qbar1, Qbar2;
679679
double area;
680680

681+
/* GRASS comment: threshold for dy, should be between 1e-4 and 1e-7
682+
* QGIS note: while the grass comment states that thres should be between 1e-4->1e-7,
683+
* a value of 1e-7 caused TestQgsDistanceArea::regression14675() to regress
684+
* The maximum threshold possible which permits regression14675() to pass
685+
* was found to be ~0.7e-7.
686+
*/
687+
const double thresh = 0.7e-7;
688+
681689
QgsDebugMsgLevel( "Ellipsoid: " + mEllipsoid, 3 );
682690
if ( !willUseEllipsoid() )
683691
{
@@ -708,11 +716,28 @@ double QgsDistanceArea::computePolygonArea( const QList<QgsPointXY> &points ) co
708716
x1 += m_TwoPI;
709717

710718
dx = x2 - x1;
711-
area += dx * ( m_Qp - getQ( y2 ) );
712-
713719
dy = y2 - y1;
714-
if ( !qgsDoubleNear( dy, 0.0 ) )
715-
area += dx * getQ( y2 ) - ( dx / dy ) * ( Qbar2 - Qbar1 );
720+
if ( qAbs( dy ) > thresh )
721+
{
722+
/* account for different latitudes y1, y2 */
723+
area += dx * ( m_Qp - ( Qbar2 - Qbar1 ) / dy );
724+
}
725+
else
726+
{
727+
/* latitudes y1, y2 are (nearly) identical */
728+
729+
/* if y2 becomes similar to y1, i.e. y2 -> y1
730+
* Qbar2 - Qbar1 -> 0 and dy -> 0
731+
* (Qbar2 - Qbar1) / dy -> ?
732+
* (Qbar2 - Qbar1) / dy should approach Q((y1 + y2) / 2)
733+
* Metz 2017
734+
*/
735+
area += dx * ( m_Qp - getQ( y2 ) );
736+
737+
/* original:
738+
* area += dx * getQ( y2 ) - ( dx / dy ) * ( Qbar2 - Qbar1 );
739+
*/
740+
}
716741
}
717742
if ( ( area *= m_AE ) < 0.0 )
718743
area = -area;

tests/src/core/testqgsdistancearea.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class TestQgsDistanceArea: public QObject
4646
void measureAreaAndUnits();
4747
void emptyPolygon();
4848
void regression14675();
49+
void regression16820();
4950

5051
};
5152

@@ -379,6 +380,16 @@ void TestQgsDistanceArea::regression14675()
379380
QGSCOMPARENEAR( calc.measureArea( geom ), 0.83301, 0.02 );
380381
}
381382

383+
void TestQgsDistanceArea::regression16820()
384+
{
385+
QgsDistanceArea calc;
386+
calc.setEllipsoid( QStringLiteral( "WGS84" ) );
387+
calc.setSourceCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:32634" ) ) );
388+
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((110250.54038314701756462 5084495.57398066483438015, 110243.46975068224128336 5084507.17200060561299324, 110251.23908144699817058 5084506.68309532757848501, 110251.2394439501222223 5084506.68307251576334238, 110250.54048078990308568 5084495.57553235255181789, 110250.54038314701756462 5084495.57398066483438015))" ) ) );
389+
//lots of tolerance here - the formulas get quite unstable with small areas due to division by very small floats
390+
QGSCOMPARENEAR( calc.measureArea( geom ), 43.183369, 0.02 );
391+
}
392+
382393
QGSTEST_MAIN( TestQgsDistanceArea )
383394
#include "testqgsdistancearea.moc"
384395

0 commit comments

Comments
 (0)