This recipe simply measures the area of a polygon.
For this recipe, we'll use a single-feature polygon shapefile, which you can download from https://geospatialpython.googlecode.com/files/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 instance:
d = QgsDistanceArea()
Pass the polygon list to the
measureArea()
method:m = d.measurePolygon(boundary.geometry().asPolygon()[0])
Convert the measurement from decimal degrees to miles:
d.convertMeasurement(m, QGis.Degrees, QGis.NauticalMiles, True)
Verify that your...