Skip to content
Permalink
Browse files
Use QgsAbstractGeometry methods for QgsGeometry::area results
Use the QgsAbstractGeometry method, so that consistent
results are obtained across the API and an exact length is used
for curved geometries (instead of the length of the segmentized
curves)
  • Loading branch information
nyalldawson committed Nov 2, 2021
1 parent 4b7dff6 commit d78775445f13e65280d4604bbfd2b5db25679c2b
Showing with 12 additions and 14 deletions.
  1. +1 −14 src/core/geometry/qgsgeometry.cpp
  2. +11 −0 tests/src/python/test_qgsgeometry.py
@@ -1889,21 +1889,8 @@ double QgsGeometry::area() const
{
return -1.0;
}
QgsGeos g( d->geometry.get() );

#if 0
//debug: compare geos area with calculation in QGIS
double geosArea = g.area();
double qgisArea = 0;
QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( d->geometry );
if ( surface )
{
qgisArea = surface->area();
}
#endif

mLastError.clear();
return g.area( &mLastError );
return d->geometry->area();
}

double QgsGeometry::length() const
@@ -931,18 +931,29 @@ def testReferenceGeometry(self):
result = geom.constGet().area()
self.assertAlmostEqual(result, exp, 5,
"Area {}: mismatch Expected:\n{}\nGot:\n{}\n".format(i + 1, exp, result))
result = geom.area()
self.assertAlmostEqual(result, exp, 5,
"Length {}: mismatch Expected:\n{}\nGot:\n{}\n".format(i + 1, exp, result))

# test length calculation
exp = float(row['length'])
result = geom.constGet().length()
self.assertAlmostEqual(result, exp, 5,
"Length {}: mismatch Expected:\n{}\nGot:\n{}\n".format(i + 1, exp, result))
if geom.type() != QgsWkbTypes.PolygonGeometry:
result = geom.length()
self.assertAlmostEqual(result, exp, 5,
"Length {}: mismatch Expected:\n{}\nGot:\n{}\n".format(i + 1, exp, result))

# test perimeter calculation
exp = float(row['perimeter'])
result = geom.constGet().perimeter()
self.assertAlmostEqual(result, exp, 5,
"Perimeter {}: mismatch Expected:\n{}\nGot:\n{}\n".format(i + 1, exp, result))
if geom.type() == QgsWkbTypes.PolygonGeometry:
result = geom.length()
self.assertAlmostEqual(result, exp, 5,
"Length {}: mismatch Expected:\n{}\nGot:\n{}\n".format(i + 1, exp, result))

def testCollection(self):
g = QgsGeometry.fromWkt('MultiLineString EMPTY')

0 comments on commit d787754

Please sign in to comment.