This recipe measures the area of a polygon. Area calculation can be an end in itself to measure the size of a plot of land or a building. It can also be the input to other calculations such as land use maps.
For this recipe, we'll use a single-feature polygon shapefile, which you can download from the following link:
https://github.com/GeospatialPython/Learn/raw/master/Mississippi.zip
Unzip the shapefile and put it in a directory named qgis_data/ms
within your root or home directory.
Perform the following steps to measure the area of a large polygon:
First, import the QGIS constants library, as follows:
from qgis.core import QGis
Load the layer:
lyr = QgsVectorLayer("/qgis_data/ms/mississippi.shp", "Mississippi", "ogr")
Access the layer's features:
fts = lyr.getFeatures()
Get the boundary feature:
boundary = fts.next()
Create the measurement object...