safe.utilities.clipper module

InaSAFE Disaster risk assessment tool developed by AusAid -
Clipper implementation.*

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.utilities.clipper.adjust_clip_extent(clip_extent, cell_size, layer_extent)[source]

Helper function to adjust the clip extent to the edge of the pixel.

This function will shift all edges of the extent to the outmost edge of the raster’s pixel row or column on which the edge coincides.

Parameters:
  • clip_extent (list) – An array representing the clip extents in the form [xmin, ymin, xmax, ymax]. This is the optimal extent between the exposure, hazard and view port.
  • cell_size (tuple) – The size of a pixel in geo reference unit in the form (res_x, res_y).
  • layer_extent (list) – An array representing the full extents of the layer in the form [xmin, ymin, xmax, ymax].
Returns:

An array containing an the adjusted clip extent in the form [xmin, ymin, xmax, ymax]

Return type:

list

safe.utilities.clipper.clip_geometry(clip_polygon, geometry)[source]

Clip a geometry (linestring or polygon) using a clip polygon.

To do this we combine the clip polygon with the input geometry which will add nodes to the input geometry where it intersects the clip polygon. Next we get the symmetrical difference between the input geometry and the combined geometry.

Parameters:
  • clip_polygon (QgsGeometry) – A Polygon or Polygon25D geometry to clip with. Multipart polygons are not supported so the client needs to take care of that.
  • geometry (QgsGeometry) – Linestring or polygon that should be clipped.
Returns:

A new geometry clipped to the region of the clip polygon.

Return type:

QgsGeometry

safe.utilities.clipper.clip_layer(layer, extent, cell_size=None, extra_keywords=None, explode_flag=True, hard_clip_flag=False, explode_attribute=None)[source]

Clip a Hazard or Exposure layer to the extents provided.

Note

Will delegate to clipVectorLayer or clipRasterLayer as needed.

Parameters:
  • layer – A valid QGIS vector or raster layer
  • extent (list(float, float, float, float)) – Either an array representing the exposure layer extents in the form [xmin, ymin, xmax, ymax]. It is assumed that the coordinates are in EPSG:4326 although currently no checks are made to enforce this. or: A QgsGeometry of type polygon. Polygon clipping is currently only supported for vector datasets.
  • cell_size (float) – cell size which the layer should be resampled to. This argument will be ignored for vector layers and if not provided for a raster layer, the native raster cell size will be used.
  • extra_keywords (dict) – Optional keywords dictionary to be added to output layer.
  • explode_flag (bool) – A bool specifying whether multipart features should be ‘exploded’ into singleparts. This parameter is ignored for raster layer clipping.
  • hard_clip_flag (bool) – A bool specifying whether line and polygon features that extend beyond the extents should be clipped such that they are reduced in size to the part of the geometry that intersects the extent only. Default is False. This parameter is ignored for raster layer clipping.
  • explode_attribute (str) – A str specifying to which attribute #1, #2 and so on will be added in case of explode_flag being true. The attribute is modified only if there are at least 2 parts. This parameter is ignored for raster layer clipping.
Returns:

Clipped layer (placed in the system temp dir). The output layer will be reprojected to EPSG:4326 if needed.

Return type:

QgsMapLayer

safe.utilities.clipper.explode_multipart_geometry(geometry)[source]

Convert a multipart geometry to a list of single parts.

This method was adapted from Carson Farmer’s fTools doGeometry implementation in QGIS.

Parameters:geometry (QgsGeometry) – A geometry to be exploded it it is multipart.
Returns:A list of single part geometries.
Return type:list
safe.utilities.clipper.extent_to_geoarray(extent, source_crs)[source]

Convert the supplied extent to geographic and return as as array.

Parameters:
  • extent – QgsRectangle to be transformed to geocrs.
  • source_crs – QgsCoordinateReferenceSystem representing the original extent’s CRS.
Returns:

Transformed extents in EPSG:4326 in the form [xmin, ymin, xmax, ymax]

safe.utilities.clipper.extent_to_kml(extent)[source]

A helper to get a little kml doc for an extent.

We can use the resulting kml with gdal warp for clipping.

Parameters:extent (list(float)) – Extent in the form [xmin, ymin, xmax, ymax].