safe.impact_statistics.zonal_stats module

InaSAFE Disaster risk assessment tool developed by AusAid -
Zonal Stats.

Contact : ole.moller.nielsen@gmail.com

Note

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

safe.impact_statistics.zonal_stats.calculate_zonal_stats(raster_layer, polygon_layer)[source]

Calculate zonal statics given two layers.

Parameters:
  • raster_layer (QgsRasterLayer, QgsMapLayer) – A QGIS raster layer.
  • polygon_layer (QgsVectorLayer, QgsMapLayer) – A QGIS vector layer containing polygons.
Returns:

A data structure containing sum, mean, min, max, count of raster values for each polygonal area.

Return type:

dict

Raises:

InvalidParameterError, InvalidGeometryError

Note:
  • InvalidParameterError if incorrect inputs are received.
  • InvalidGeometryError if none geometry is found during calculations.
  • Any other exceptions are propagated.

Example of output data structure:

{ 1: {‘sum’: 10, ‘count’: 20, ‘min’: 1, ‘max’: 4, ‘mean’: 2},
2: {‘sum’: 10, ‘count’: 20, ‘min’: 1, ‘max’: 4, ‘mean’: 2}, 3 {‘sum’: 10, ‘count’: 20, ‘min’: 1, ‘max’: 4, ‘mean’: 2}}

The key in the outer dict is the feature id

Note

This is a python port of the zonal stats implementation in QGIS . See https://github.com/qgis/Quantum-GIS/blob/master/src/analysis/ vector/qgszonalstatistics.cpp

Note

Currently not projection checks are made to ensure that both layers are in the same CRS - we assume they are.

safe.impact_statistics.zonal_stats.centroid_intersection_stats(band, geometry, pixel_offset_x, pixel_offset_y, cells_x, cells_y, cell_size_x, cell_size_y, raster_box, no_data)[source]

Stats where centroid of each cell must intersect the polygon.

Parameters:
  • band (GDALRasterBand) – A valid band from a raster layer.
  • geometry (QgsGeometry) – A valid polygon geometry.
  • pixel_offset_x (int) – Left offset for raster window.
  • pixel_offset_y (int) – Offset from bottom for raster window.
  • cells_x (int) – Width of the raster window.
  • cells_y (int) – Height of the raster window.
  • cell_size_x (float) – Size in the x direction of a single cell.
  • cell_size_y (float) – Size in the y direction of a single cell.
  • raster_box (QgsRectangle) – Box defining the extents of the raster.
  • no_data (int, float) – Value for no data in the raster.
Returns:

Sum, Count - sum of the values of all pixels and the count of pixels that intersect with the geometry.

Return type:

(float, int)

safe.impact_statistics.zonal_stats.intersection_box(raster_box, feature_box, cell_size_x, cell_size_y)[source]

Calculate cell offset and distances for the intersecting bbox.

Parameters:
  • raster_box (QgsRectangle) – Box defining the extents of the raster.
  • feature_box (QgsRectangle) – Bounding box for the feature.
  • cell_size_x (float) – Size in the x direction of a single cell.
  • cell_size_y (float) – Size in the y direction of a single cell.
Returns:

Offsets in the x and y directions, and number of cells in the x and y directions.

Return type:

(int, int, int, int)

safe.impact_statistics.zonal_stats.inverse_transform(geo_transform)[source]

Invert standard 3x2 set of geo-transform coefficients.

Parameters:
  • geo_transform (list (six floats or empty)) – Geo-referencing transform from raster metadata ( which is unaltered).
  • geo_transform – Invert geo-referencing transform (updated) on success, empty list on failure.
safe.impact_statistics.zonal_stats.map_to_pixel(x_coordinate, y_coordinate, geo_transform)[source]

Convert map coordinates to pixel coordinates.

Parameters:
  • x_coordinate (float) – Input map X coordinate.
  • y_coordinate (float) – Input map Y coordinate.
  • geo_transform (list (six floats)) – Geo-referencing transform from raster metadata.

:returns pixel_x, pixel_y - Output pixel coordinates :rtype: (int, int)

safe.impact_statistics.zonal_stats.numpy_stats(band, geometry, geo_transform, no_data, crs)[source]
Parameters:
  • band (GDALRasterBand) – A valid band from a raster layer.
  • geometry (QgsGeometry) – A polygon geometry used to calculate statistics.
  • geo_transform (list (six floats)) – Geo-referencing transform from raster metadata.
  • no_data (int, float) – Value for no data in the raster.
  • crs (OGRSpatialReference) – Coordinate reference system of the vector layer.
Returns:

Sum, Count - sum of the values of all pixels and the count of pixels that intersect with the geometry.

Return type:

(float, int)

safe.impact_statistics.zonal_stats.pixel_to_map(pixel_x, pixel_y, geo_transform)[source]

Convert pixel coordinates to map coordinates.

Parameters:
  • pixel_x (float) – Input pixel X coordinate
  • pixel_y (float) – Input pixel Y coordinate
  • geo_transform (list (six floats)) – Geo-referencing transform from raster metadata.

:returns map_x, map_y - Output map coordinates :rtype: (float, float)

safe.impact_statistics.zonal_stats.precise_stats(band, geometry, pixel_offset_x, pixel_offset_y, cells_x, cells_y, cell_size_x, cell_size_y, raster_box, no_data)[source]

Weighted pixel sum for polygon based on only intersecting parts.

Parameters:
  • band (GDALRasterBand) – A valid band from a raster layer.
  • geometry (QgsGeometry) – A valid polygon geometry.
  • pixel_offset_x (int) – Left offset for raster window.
  • pixel_offset_y (int) – Offset from bottom for raster window.
  • cells_x (int) – Width of the raster window.
  • cells_y (int) – Height of the raster window.
  • cell_size_x (float) – Size in the x direction of a single cell.
  • cell_size_y (float) – Size in the y direction of a single cell.
  • raster_box (QgsRectangle) – Box defining the extents of the raster.
  • no_data (int, float) – Value for no data in the raster.
Returns:

Sum, Count - sum of the values of all pixels and the count of pixels that intersect with the geometry.

Return type:

(float, int)

safe.impact_statistics.zonal_stats.tr(text)[source]

We define a tr() alias here since the utilities implementation.

The code below is not a class and does not inherit from QObject.
Parameters:text (str) – String to be translated
Returns:Translated version of the given string if available, otherwise the original string.
safe.impact_statistics.zonal_stats.transform(x, y, geo_transform)[source]

Apply a geo transform to coordinates.

Parameters:
  • x (float) – Input X coordinate.
  • y (float) – Input Y coordinate
  • geo_transform (list (six floats)) – Geo-referencing transform from raster metadata.

:returns output_x, output_y - Transformed X and Y coordinates :rtype: (float, float)