Skip to content

Commit

Permalink
Use QgsAbstractGeometry methods for QgsGeometry::area results
Browse files Browse the repository at this point in the history
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 d787754
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
15 changes: 1 addition & 14 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -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')
Expand Down

0 comments on commit d787754

Please sign in to comment.