Raster datasets represent real-world features efficiently but can have limited usage for geospatial analysis. Once you have classified an image into a manageable data set, you can convert those raster classes into a vector data set for more sophisticated GIS analysis. GDAL has a function for this operation called polygonize.
You will need to download the following classified raster and place it in your /qgis_data/rasters
directory:
Normally, you would save the output of this recipe as a shapefile. We won't specify an output file name. The Processing Toolbox will assign it a temporary filename and return that filename. We'll simply load the temporary file into QGIS. The algorithm allows you to write to a shapefile by specifying it as the last parameter.
processing
module:import processing
processing.runalg("gdalogr:polygonize","C:/qgis_data/rasters/landuse_bay.tif","DN",None)
GDAL looks for clusters of pixels and creates polygons around them. It is important to have as few classes as possible. If there is too much variation in the pixels, then GDAL will create a polygon around each pixel in the image. You turn this recipe into a true analysis product by using the recipe in Chapter 1, Calculating the Area of a Polygon to quantify each class of land use.