safe.utilities.gis module

Helpers for GIS related functionality.

safe.utilities.gis.convert_to_safe_layer(layer)[source]

Thin wrapper around the safe read_layer function.

Parameters:layer (QgsMapLayer, read_layer) – QgsMapLayer or Safe layer.
Returns:A safe read_safe_layer object is returned.
Return type:read_layer
safe.utilities.gis.create_memory_layer(layer, new_name='')[source]

Return a memory copy of a layer

Parameters:
  • layer (QgsVectorLayer) – QgsVectorLayer that shall be copied to memory.
  • new_name (str) – The name of the copied layer.
Returns:

An in-memory copy of a layer.

Return type:

QgsMapLayer

safe.utilities.gis.extent_string_to_array(extent_text)[source]

Convert an extent string to an array.

Parameters:extent_text (str) – String representing an extent e.g. 109.829170982, -8.13333290561, 111.005344795, -7.49226294379
Returns:A list of floats, or None
Return type:list, None
safe.utilities.gis.extent_to_array(extent, source_crs, dest_crs=None)[source]

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

Parameters:
  • extent (QgsRectangle) – Rectangle defining a spatial extent in any CRS.
  • source_crs (QgsCoordinateReferenceSystem) – Coordinate system used for input extent.
  • dest_crs (QgsCoordinateReferenceSystem) – Coordinate system used for output extent. Defaults to EPSG:4326 if not specified.
Returns:

a list in the form [xmin, ymin, xmax, ymax] where all coordinates provided are in Geographic / EPSG:4326.

Return type:

list

safe.utilities.gis.get_wgs84_resolution(layer)[source]

Return resolution of raster layer in EPSG:4326.

If input layer is already in EPSG:4326, simply return the resolution If not, work it out based on EPSG:4326 representations of its extent.

Parameters:layer (QgsRasterLayer or QgsMapLayer) – Raster layer
Returns:The resolution of the given layer in the form of (res_x, res_y)
Return type:tuple
safe.utilities.gis.is_point_layer(layer)[source]

Check if a QGIS layer is vector and its geometries are points.

Parameters:layer (QgsVectorLayer, QgsMapLayer) – A vector layer.
Returns:True if the layer contains points, otherwise False.
Return type:bool
safe.utilities.gis.is_polygon_layer(layer)[source]

Check if a QGIS layer is vector and its geometries are polygons.

Parameters:layer (QgsVectorLayer, QgsMapLayer) – A vector layer.
Returns:True if the layer contains polygons, otherwise False.
Return type:bool
safe.utilities.gis.is_raster_layer(layer)[source]

Check if a QGIS layer is raster.

Parameters:layer (QgsRaster, QgsMapLayer, QgsVectorLayer) – A layer.
Returns:True if the layer contains polygons, otherwise False.
Return type:bool
safe.utilities.gis.layer_attribute_names(layer, allowed_types, current_keyword=None)[source]

Iterates over the layer and returns int or string fields.

Parameters:
  • layer (QgsVectorLayer, QgsMapLayer) – A vector layer whose attributes shall be returned.
  • allowed_types (list(QVariant)) – List of QVariant that are acceptable for the attribute. e.g.: [QtCore.QVariant.Int, QtCore.QVariant.String].
  • current_keyword (str) – The currently stored keyword for the attribute.
Returns:

A two-tuple containing all the attribute names of attributes that have int or string as field type (first element) and the position of the current_keyword in the attribute names list, this is None if current_keyword is not in the list of attributes (second element).

Return type:

tuple(list(str), int)

safe.utilities.gis.qgis_version()[source]

Get the version of QGIS.

Returns:QGIS Version where 10700 represents QGIS 1.7 etc.
Return type:int
safe.utilities.gis.read_impact_layer(impact_layer)[source]

Helper function to read and validate a safe native spatial layer.

Parameters:impact_layer (read_layer) – Layer object as provided by InaSAFE engine.
Returns:Valid QGIS layer or None
Return type:None, QgsRasterLayer, QgsVectorLayer
safe.utilities.gis.rectangle_geo_array(rectangle, map_canvas)[source]

Obtain the rectangle in EPSG:4326.

Parameters:
  • rectangle (QgsRectangle) – A rectangle instance.
  • map_canvas (QgsMapCanvas) – A map canvas instance.
Returns:

A list in the form [xmin, ymin, xmax, ymax] where all coordinates provided are in Geographic / EPSG:4326.

Return type:

list

Note

Delegates to extent_to_array()

safe.utilities.gis.validate_geo_array(extent)[source]

Validate a geographic extent.

New in version 3.2.

Parameters:extent (list) – A list in the form [xmin, ymin, xmax, ymax] where all coordinates provided are in Geographic / EPSG:4326.
Returns:True if the extent is valid, otherwise False
Return type:bool
safe.utilities.gis.vector_geometry_string(layer)[source]

Get string representation of geometry types of a QgsVectorLayer.

Parameters:layer – A vector layer.
safe.utilities.gis.viewport_geo_array(map_canvas)[source]

Obtain the map canvas current extent in EPSG:4326.

Parameters:map_canvas (QgsMapCanvas) – A map canvas instance.
Returns:A list in the form [xmin, ymin, xmax, ymax] where all coordinates provided are in Geographic / EPSG:4326.
Return type:list

Note

Delegates to rectangle_geo_array()