[Qgis-developer] Help needed in accessing classified raster category

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Mar 30 09:37:28 EDT 2010


On Tue, Mar 30, 2010 at 1:24 PM, Bishwarup Banerjee
<bishwarup.banerjee at gmail.com> wrote:
> Dear All,
>
> Is there any way to access the Category values of the raster layer through
> python in QGIS?
>
> I have a classified raster and I want to access the classes through python.
> For example : I have classified soil map of a partricular area, with
> different types of soil, now I want to access the soil types of that raster
> through python.
>
> It will be very kind of you, if can help me out.

 Easiest thing is to use the gdal API and get it into a numpy array.
Something like (taken from rastercalc):

import numpy
import osgeo.gdal as gdal

def layerAsArray( layer ):
  gdalData = gdal.Open( str( layer.source() ) )
  array = gdalData.ReadAsArray().astype( numpy.float32 )
  gdalData = None
  return array

numpy.float32 might not be the right type if you have a classification
raster, but hopefully you'll get the idea.

There's more useful-looking functions in rastercalc and other plugins.

Barry


More information about the Qgis-developer mailing list