Converting a raster to a vector

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.

Getting ready

You will need to download the following classified raster and place it in your /qgis_data/rasters directory:

https://geospatialpython.googlecode.com/svn/landuse_bay.zip

How to do it...

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.

  1. In the QGIS Python Console, import the processing module:
    import processing
    
  2. Next, run the algorithm specifying the process name, input image, the field name for the class number, and optionally the output shapefile:
    processing.runalg("gdalogr:polygonize","C:/qgis_data/rasters/landuse_bay.tif","DN",None)
    
  3. You should get a vector layer with three classes, defined as polygons, denoting developed areas. In the sample image below, we have assigned unique colors to each class: developed area (darkest), water (midtones), and land (lightest color):
    How to do it...

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset