6

It seems that there are at least two different ways to calculate the area of a polygon in QGIS using Python:

1) area = feature.geometry().area()
2) d = QgsDistanceArea()
   area_v2 = d.measurePolygon(feature.geometry().asPolygon()[0])

These two approaches produce very similar outcomes, but not the same. It also seems that QGIS prefers the second approach -once the geometry column is added through Vector -> Export/Add Geometry Columns. Presumingly, this is related with the projections. However, everything I deal on QGIS is on EPSG:3857 since I am using OSM data. Furthermore, the perimeter values obtained from this approach match with the first approach when the following statement is executed:

 perimeter = feature.geometry().length()  

I am inclined to choose the first approach due to its simplicity, so why does QGIS rely on the second approach?

5

I think this is described in the QGIS Documents: Geometry Predicates and Operations:

Areas and perimeters don’t take CRS into account when computed using these methods from the QgsGeometry class. For a more powerful area and distance calculation, the QgsDistanceArea class can be used. If projections are turned off, calculations will be planar, otherwise they’ll be done on the ellipsoid. When an ellipsoid is not set explicitly, WGS84 parameters are used for calculations.

So indeed your first method is the more simple one. Also, I think it depends on the tool you use as some are coded to use QgsDistanceArea() and others are not. In the end, it just depends on how large of an area you're examining and the level of precision you are wanting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.