safe.storage.vector module

Vector Module

Tip

Provides functionality for manipulation of vector data. The data can be in-memory or file based.

Resources for understanding vector data formats and the OGR library: Treatise on vector data model: http://www.esri.com/news/arcuser/0401/topo.html OGR C++ reference: http://www.gdal.org/ogr

class safe.storage.vector.Vector(data=None, projection=None, geometry=None, geometry_type=None, name=None, keywords=None, style_info=None, sublayer=None)[source]

Bases: safe.storage.layer.Layer

InaSAFE representation of vector data.

Args:
  • data: Can be either
    • A filename of a vector file format known to GDAL.
    • List of dictionaries of field names and attribute values associated with each point coordinate.
    • A QgsVectorLayer associated with geometry and data.
    • None
  • projection: Geospatial reference in WKT format.

    Only used if geometry is provided as a numeric array, if None, WGS84 geographic is assumed.

  • geometry: A list of either point coordinates or polygons/lines

    (see note below).

  • geometry_type: Desired interpretation of geometry.

    Valid options are ‘point’, ‘line’, ‘polygon’ or the ogr types: 1, 2, 3. If None, a geometry_type will be inferred from the data.

  • name: Optional name for layer. If None, basename is used.

  • keywords: Optional dictionary with keywords that describe the

    layer. When the layer is stored, these keywords will be written into an associated file with extension ‘.keywords’.

    Keywords can for example be used to display text about the layer in an application.

  • style_info: Dictionary with information about how this layer

    should be styled. See impact_functions/styles.py for examples.

  • sublayer: str Optional sublayer (band name in the case of raster,

    table name in case of sqlite etc.) to load. Only applicable to those dataformats supporting more than one layer in the data file.

Returns:
  • InaSAFE vector layer instance
Raises:
  • TypeError, ReadLayerError, WriteLayerError, InaSAFEError, GetDataError

Notes:

If data is a filename, all other arguments are ignored as they will be inferred from the file.

The geometry type will be inferred from the dimensions of geometry. If each entry is one set of coordinates the type will be ogr.wkbPoint, if it is an array of coordinates the type will be ogr.wkbPolygon.

To cast array entries as lines set geometry_type explicitly to ‘line’ in the call to Vector. Otherwise, they will default to polygons.

Each polygon or line feature take the form of an Nx2 array representing vertices where line segments are joined.

If polygons have holes, their geometry must be passed in as a list of polygon geometry objects (as defined in module geometry.py)

as_qgis_native()[source]

Return vector layer data as qgis QgsVectorLayer.

A stub is used now:
save all data in a file, then create QgsVectorLayer from the file.
Raises:
  • TypeError if qgis is not avialable
copy()[source]

Return copy of vector layer

This copy will be equal to self in the sense defined by __eq__

get_attribute_names()[source]

Get available attribute names.

These are the ones that can be used with get_data

get_bounding_box()[source]

Get bounding box coordinates for vector layer.

Format is [West, South, East, North]

get_data(attribute=None, index=None, copy=False)[source]

Get vector attributes.

Parameters:
  • attribute (str) – Specify an attribute name of which to return data.
  • index (int) – Indicates a specific value on which to call the attribute. Ignored if no attribute is set.
  • copy (bool) – Indicate whether to return a pointer to the data, or a copy of.
Raises:

GetDataError

Returns:

A list where each entry is a dictionary of attributes for one feature.

Return type:

list,

Note:

Data is returned as a list where each entry is a dictionary of attributes for one feature. Entries in get_geometry() and get_data() are related as 1-to-1

If optional argument attribute is specified and a valid name, then the list of values for that attribute is returned.

If optional argument index is specified on the that value will be returned. Any value of index is ignored if attribute is None.

If optional argument copy is True and all attributes are requested, a copy will be returned. Otherwise a pointer to the data is returned.

get_extrema(attribute=None)[source]

Get min and max values from specified attribute

Parameters:attribute (str) – Specify an attribute name of which to return data.
Raises:InaSAFEError
Returns:minimum and maximum attribute values
Return type:
get_geometry(copy=False, as_geometry_objects=False)[source]

Return geometry for vector layer.

Depending on the feature type, geometry is:

geometry type   output type

point           list of 2x1 array of longitudes and latitudes)
line            list of arrays of coordinates
polygon         list of arrays of coordinates

Optional boolean argument as_geometry_objects will change the return value to a list of geometry objects rather than a list of arrays. This currently only applies to polygon geometries

Parameters:
  • copy (bool) – Set to return a copy of the data rather than a pointer.
  • as_geometry_objects (bool) – Set to return geometry objects rather than a list of arrays.
Raises:

InaSAFEError

Returns:

A list of geometry objects or arrays.

Return type:

list

get_geometry_name()[source]

Return geometry name for vector layer

get_geometry_type()[source]

Return geometry type for vector layer

get_topN(attribute, N=10)[source]

Get top N features

Parameters:
  • attribute (str) – The name of attribute where values are sought
  • N (int) – How many
Returns:

New vector layer with selected features

is_line_data[source]

Check whether this is a line

Returns:Test result
Return type:bool
is_multi_polygon_data[source]

Check whether this is multipolygon

Returns:Test result
Return type:bool
is_point_data[source]

Check whether this is a point

Returns:Test result
Return type:bool
is_polygon_data[source]

Check whether this is a polygon

Returns:Test result
Return type:bool
read_from_file(filename)[source]

Read and unpack vector data.

It is assumed that the file contains only one layer with the pertinent features. Further it is assumed for the moment that all geometries are points.

  • A feature is a geometry and a set of attributes.
  • A geometry refers to location and can be point, line, polygon or combinations thereof.
  • The attributes or obtained through GetField()

The full OGR architecture is documented at * http://www.gdal.org/ogr/ogr_arch.html * http://www.gdal.org/ogr/ogr_apitut.html

Examples are at * danieljlewis.org/files/2010/09/basicpythonmap.pdf * http://invisibleroads.com/tutorials/gdal-shapefile-points-save.html * http://www.packtpub.com/article/geospatial-data-python-geometry

Limitation of the Shapefile are documented in http://resources.esri.com/help/9.3/ArcGISDesktop/com/Gp_ToolRef/ geoprocessing_tool_reference/ geoprocessing_considerations_for_shapefile_output.htm

Parameters:filename (str) – a fully qualified location to the file
Raises:ReadLayerError
read_from_qgis_native(qgis_layer)[source]

Read and unpack vector data from qgis layer QgsVectorLayer.

A stub is used now:
save all data in a file, then call safe.read_from_file
Raises:
  • TypeError if qgis is not avialable
  • IOError if can’t store temporary file
write_to_file(filename, sublayer=None)[source]

Save vector data to file

Parameters:
  • filename (str) – filename with extension .shp or .gml
  • sublayer (str) – Optional parameter for writing a sublayer. Ignored unless we are writing to an sqlite file.
Raises:

WriteLayerError

Note:

Shp limitation, if attribute names are longer than 10 characters they will be truncated. This is due to limitations in the shp file driver and has to be done here since gdal v1.7 onwards has changed its handling of this issue: http://www.gdal.org/ogr/drv_shapefile.html

For this reason we recommend writing to spatialite.

safe.storage.vector.convert_line_to_points(V, delta)[source]

Convert line vector data to point vector data

Parameters:
  • V (Vector) – Vector layer with line data
  • delta (float) – Incremental step to find the points
Returns:

Vector layer with point data and the same attributes as V

Return type:

Vector

safe.storage.vector.convert_polygons_to_centroids(V)[source]

Convert polygon vector data to point vector data

Parameters:V (Vector) – Vector layer with polygon data
Returns:Vector layer with point data and the same attributes as V
Return type:Vector