safe.storage.raster module

Class Raster

class safe.storage.raster.Raster(data=None, projection=None, geotransform=None, name=None, keywords=None, style_info=None)[source]

Bases: safe.storage.layer.Layer

InaSAFE representation of raster data

Args:
  • 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. http://www.gdal.org/gdal_tutorial.html 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/styles.py for examples.

Returns:
  • InaSAFE raster layer instance
Raises:
  • TypeError, ReadLayerError, WriteLayerError, InaSAFEError, GetDataError
Note:
If data is a filename, all other arguments are ignored as they will be inferred from the file.
as_qgis_native()[source]

Return raster layer data as qgis QgsRasterayer.

A stub is used now:
save all data in a file, then create QgsRaterLayer from the file.
Raises:
  • TypeError if qgis is not avialable
copy()[source]

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()[source]

Get bounding box coordinates for raster layer

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

Get raster data as numeric array

Args:
  • 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

Note:
Scaling does not currently work with projected layers. See issue #123
get_extrema()[source]

Get min and max from raster

Note:
If raster has a nominated no_data value, this is ignored.
Returns:
min, max
get_geometry()[source]

Return longitudes and latitudes (the axes) for grid.

Note:

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]

get_geotransform(copy=False)[source]

Return geotransform for this raster layer

Returns:

  • geotransform: 6 digit vector

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

    See e.g. http://www.gdal.org/gdal_tutorial.html

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

get_nodata_value()[source]

Get the internal representation of NODATA

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

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

Args:
  • 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_from_file(filename)[source]

Read and unpack raster data

read_from_qgis_native(qgis_layer)[source]

Read raster data from qgis layer QgsRasterLayer.

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

  • IOError if can’t store temporary file

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

    dataProvider

to_vector_layer()[source]

Convert raster grid to vector point data

Returns:
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.
to_vector_points()[source]

Convert raster grid to vector point data

Returns:
  • coordinates: Nx2 array of x, y (lon, lat) coordinates
  • values: N array of corresponding grid values
write_to_file(filename)[source]

Save raster data to file

Args:
  • filename: filename with extension .tif

Gdal documentation at: http://www.gdal.org/classGDALRasterBand.html