Utilities for storage module
Convert coordinates to linear_ring
Parameters: 


Note: Based on http://www.packtpub.com/article/workinggeospatialdatapython
Convert coordinates to wkt format
Parameters: 


Returns:  wkt: geometry in the format known to ogr: Examples 
Compute intersection between two or more bounding boxes.
Parameters:  args – two or more bounding boxes. Each is assumed to be a list or a tuple with four coordinates (W, S, E, N) 

Returns:  The minimal common bounding box 
Grow bounding box with one unit of resolution in each direction
This will ensure there are enough pixels to robustly provide interpolated values without having to painstakingly deal with all corner cases such as 1 x 1, 1 x 2 and 2 x 1 arrays.
The border will also make sure that points that would otherwise fall outside the domain (as defined by a tight bounding box) get assigned values.
Parameters: 


Returns:  Adjusted bounding box 

 
 * *  * *
 O
 
 * *  * *

Calculate the signed area of nonselfintersecting polygon
Parameters: 


Returns:  area: Area of polygon (subject to the value of argument signed) 
Return type:  numpy.ndarray 
Calculate the centroid of nonselfintersecting polygon
Parameters:  polygon (numpy.ndarray) – Numeric array of points (longitude, latitude). It is assumed to be closed, i.e. first and last points are identical 

Returns:  calculated centroid 
Return type:  numpy.ndarray 
Check that geotransform is valid
Parameters:  geotransform (tuple) – GDAL geotransform (6tuple). (top left x, we pixel resolution, rotation, top left y, rotation, ns pixel resolution). See e.g. http://www.gdal.org/gdal_tutorial.html 

Note
This assumes that the spatial reference uses geographic coordinates, so will not work for projected coordinate systems.
Combine polygon and point layers
Parameters:  layers (list) – List of vector layers of type polygon or point 

Returns:  One point layer with all input point layers and centroids from all input polygon layers. 
Return type:  numpy.ndarray 
Raises:  InaSAFEError (in case attribute names are not the same.) 
Provides string representation of numeric geometry types
Parameters:  g_type (ogr.wkb*, None) – geometry type: 

FIXME (Ole): I can’t find anything like this in ORG. Why?
Convert geotransform to bounding box
Parameters: 


Returns:  bbox: Bounding box as a list of geographic coordinates [west, south, east, north] 
Note
Rows and columns are needed to determine eastern and northern bounds. FIXME: Not sure if the pixel vs gridline registration issue is observed correctly here. Need to check against gdal > v1.7
Convert geotransform to resolution
Parameters: 


Returns:  resolution: grid spacing (res_x, res_y) in (positive) decimal degrees ordered as longitude first, then latitude. or (res_x + res_y) / 2 (if isotropic is True) 
Determine geometry type based on data
Parameters: 


Returns:  geometry_type: Either ogr.wkbPoint, ogr.wkbLineString or ogr.wkbPolygon 
If geometry type cannot be determined an Exception is raised.
There is no consistency check across all entries of the geometry list, only the first element is used in this determination.
Extract polygon data from OGR geometry
Parameters:  G – OGR polygon geometry 

Returns:  List of InaSAFE polygon instances 
Extract coordinates from OGR ring object
Parameters:  ring – OGR ring object 

Returns:  Nx2 numpy array of vertex coordinates (lon, lat) 
Return type:  numpy.array 
Determine if x behaves like a true sequence but not a string
Parameters:  x (object) – Sequence like object 

Returns:  Test result 
Return type:  bool 
Grow bounding box to exceed specified resolution if needed
Parameters: 


Returns:  Adjusted bounding box guaranteed to exceed specified resolution 
Calculate a list of points along a line with a given delta
Parameters: 


Returns:  Numeric array of points (longitude, latitude). 
Return type:  numpy.ndarray 
Creates an array of points between two points given a delta
Parameters: 


Returns:  Array of points. 
Return type:  numpy.ndarray 
Convert vectors of longitudes and latitudes to geotransform
Parameters: 


Returns:  geotransform: 6tuple (top left x, we pixel resolution, rotation, top left y, rotation, ns pixel resolution) 
Read keywords dictionary from file
Parameters: 


Returns:  keywords: Dictionary of keyword, value pairs 
A keyword layer with sublayers may look like this:
[osm_buildings] datatype: osm category: exposure subcategory: building purpose: dki title: buildings_osm_4326
[osm_flood] datatype: flood category: hazard subcategory: building title: flood_osm_4326
Whereas a simple keywords file would look like this
datatype: flood category: hazard subcategory: building title: flood_osm_4326
If filename does not exist, an empty dictionary is returned Blank lines are ignored Surrounding whitespace is removed from values, but keys are unmodified If there are no ‘:’, then the keyword is treated as a key with no value
Compares to linear rings as numpy arrays
Parameters: 


where x’ is x reversed in the first dimension. This corresponds to linear rings being seen as equal irrespective of whether they are organised in clock wise or counter clock wise order
Helper function to make a QgsMapLayer from a safe read_layer layer.
Parameters:  layer (read_layer) – Layer object as provided by InaSAFE engine. 

Returns:  A validated QGIS layer or None. Returns None when QGIS is not available. 
Return type:  QgsMapLayer, QgsVectorLayer, QgsRasterLayer, None 
Raises:  Exception if layer is not valid. 
Write keywords dictonary to file
Parameters: 


A keyword file with sublayers may look like this:
[osm_buildings] datatype: osm category: exposure subcategory: building purpose: dki title: buildings_osm_4326
[osm_flood] datatype: flood category: hazard subcategory: building title: flood_osm_4326
Keys must be strings not containing the ”:” character Values can be anything that can be converted to a string (using Python’s str function)
Surrounding whitespace is removed from values, but keys are unmodified The reason being that keys must always be valid for the dictionary they came from. For values we have decided to be flexible and treat entries like ‘unit:m’ the same as ‘unit: m’, or indeed ‘unit: m ‘. Otherwise, unintentional whitespace in values would lead to surprising errors in the application.