Class Raster

class, projection=None, geotransform=None, name=None, keywords=None, style_info=None)[source]


InaSAFE representation of raster data

  • data: Can be either
    • a filename of a raster file format known to GDAL
    • an MxN array of raster data
    • None (FIXME (Ole): Don’t think we need this option)
  • projection: Geospatial reference in WKT format.

    Only used if data is provide as a numeric array, if None, WGS84 geographic is assumed

  • geotransform: GDAL geotransform (6-tuple).

    (top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution). See e.g. Only used if data is provide as a numeric array,

  • name: Optional name for layer. If None, basename is used.

  • keywords: Optional dictionary with keywords that describe the

    layer. When the layer is stored, these keywords will be written into an associated file with extension .keywords. Keywords can for example be used to display text about the layer in a web application.

  • style_info: Dictionary with information about how this layer

    should be styled. See impact_functions/ for examples.

  • InaSAFE raster layer instance
  • TypeError, ReadLayerError, WriteLayerError, InaSAFEError, GetDataError
If data is a filename, all other arguments are ignored as they will be inferred from the file.

Return raster layer data as qgis QgsRasterayer.

A stub is used now:
save all data in a file, then create QgsRaterLayer from the file.
  • TypeError if qgis is not avialable

Return copy of raster layer

This copy will be equal to self in the sense defined by __eq__

get_bins(N=10, quantiles=False)[source]

Get N values between the min and the max occurred in this dataset.

Return sorted list of length N+1 where the first element is min and the last is max. Intermediate values depend on the keyword quantiles: If quantiles is True, they represent boundaries between quantiles. If quantiles is False, they represent equidistant interval boundaries.


Get bounding box coordinates for raster layer

Format is [West, South, East, North]
get_data(nan=True, scaling=None, copy=False)[source]

Get raster data as numeric array

  • nan: Optional flag controlling handling of missing values.

    If keyword nan has a numeric value, nodata values will be replaced by that value. E.g. to set missing values to 0, do get_data(nan=0.0)

    NOTE: The following behaviour is depricated, since we handle this on file load: [If nan is True (default), nodata values will be replaced with numpy.nan]

  • scaling: Optional flag controlling if data is to be scaled

    if it has been resampled. Admissible values are

    False: data is retrieved without modification.

    True: Data is rescaled based on the squared ratio

    between its current and native resolution. This is typically required if raster data represents a density such as population per km^2

    None: The behaviour will depend on the keyword

    “population” associated with the layer. If it is “density”, scaling will be applied otherwise not. This is the default.

    scalar value: If scaling takes a numerical scalar value,

    that will be use to scale the data

  • copy (optional): If present and True return copy

Scaling does not currently work with projected layers. See issue #123

Get min and max from raster

If raster has a nominated no_data value, this is ignored.
min, max

Return longitudes and latitudes (the axes) for grid.


Return two vectors (longitudes and latitudes) corresponding to grid. The values are offset by half a pixel size to correspond to pixel registration.

I.e. If the grid origin (top left corner) is (105, 10) and the resolution is 1 degrees in each direction, then the vectors will take the form

longitudes = [100.5, 101.5, ..., 109.5] latitudes = [0.5, 1.5, ..., 9.5]


Return geotransform for this raster layer


  • geotransform: 6 digit vector

    (top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution).

    See e.g.

  • copy (optional): If present and True return copy


Get the internal representation of NODATA

This is always numpy.NaN [issue #297]
get_resolution(isotropic=False, native=False)[source]

Get raster resolution as a 2-tuple (resx, resy)

  • isotropic: If True, verify that dx == dy and return dx

    If False return 2-tuple (dx, dy)

  • native: Optional flag. If True, return native resolution if

    available. Otherwise return actual.


Read and unpack raster data


Read raster data from qgis layer QgsRasterLayer.

A stub is used now:
save all data in a file, then call safe.read_from_file
  • TypeError if qgis is not avialable

  • IOError if can’t store temporary file

  • GetDataError if can’t create copy of qgis_layer’s



Convert raster grid to vector point data

a vector layer object with data points corresponding to grid points. The order is row-major which means that the x (longitude) direction is varying the fastest.

Convert raster grid to vector point data

  • coordinates: Nx2 array of x, y (lon, lat) coordinates
  • values: N array of corresponding grid values

Save raster data to file

  • filename: filename with extension .tif

Gdal documentation at: