Utilities for storage module
Convert coordinates to linear_ring
A can be either a numpy array or a list of coordinates.
Other options include ogr.wkbLineString
Note: Based on http://www.packtpub.com/article/working-geospatial-data-python
Convert coordinates to wkt format
A can be either a numpy array or a list of coordinates.
geom_type: Determines output keyword ‘POLYGON’ or ‘LINESTRING’
Compute intersection between two or more bounding boxes
Each is assumed to be a list or a tuple with four coordinates (W, S, E, N)
Grow bounding box with one unit of resolution in each direction
This will ensure there is 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.
bbox: Bounding box with format [W, S, E, N]
res - Raster resolution in either direction If resolution is None bbox is returned unchanged.
Calculate the signed area of non-self-intersecting polygon
to be closed, i.e. first and last points are identical
sign:
If points are ordered counter clockwise, the signed area will be positive. If points are ordered clockwise, it will be negative Default is False which means that the area is always positive.
Calculate the centroid of non-self-intersecting polygon
to be closed, i.e. first and last points are identical
Check that geotransform is valid
(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
Combine polygon and point layers
Provides string representation of numeric geometry types
FIXME (Ole): I can’t find anything like this in ORG. Why?
Convert geotransform to bounding box
(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
columns: Number of columns in grid
rows: Number of rows in grid
[west, south, east, north]
Convert geotransform to resolution
(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
isotropic: If True, return the average (dx + dy) / 2
degrees ordered as longitude first, then latitude. or (resx + resy) / 2 (if isotropic is True)
Determine geometry type based on data
which are assumed to be numpy arrays of coordinates
geometry_type: Optional type - ‘point’, ‘line’, ‘polygon’ or None
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
Determine if x behaves like a true sequence but not a string
Grow bounding box to exceed specified resolution if needed
Calculate a list of points along a line with a given delta
Creates an array of points between two points given a delta
Convert vectors of longitudes and latitudes to geotransform
top left y, rotation, n-s pixel resolution)
Read keywords dictionary from file
The format of one line is expected to be either string: string or string
such as sqlite or netcdf which can potentially hold more than one layer. The string should map to the layer group as per the example below. If the keywords file contains sublayer definitions but no sublayer was defined, the first layer group will be returned.
a dict of dicts, where the top level dict entries each represent a sublayer, and the values of that dict will be dicts of keyword entries.
Raises: None
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
Wheras 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
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
Write keywords dictonary to file
keywords: Dictionary of keyword, value pairs
filename: Name of keywords file. Extension expected to be .keywords
such as sqlite or netcdf which can potentially hold more than one layer. The string should map to the layer group as per the example below. If the keywords file contains sublayer definitions but no sublayer was defined, keywords file content will be removed and replaced with only the keywords provided here.
Returns: None
Raises: None
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.
This module forms part of the InaSAFE tool.