urclib.common_utils
Collection of functions that are used in both grid creation and score analyses.
Module Contents
Classes
Manages filepaths associated with a collection of data. |
Functions
Context which prints the time it took to get from beginning to end of the with block, in seconds. |
|
|
Print raw seconds in nicely hour, minute, seconds format. |
|
Parse out script arguments into a workspace. |
|
Lists the fields in a feature class, shapefile, or table in a specified dataset. |
|
Remove a file if present. |
|
Create intersect raster for specific field values in vector layer |
|
Convert specified Vector layers to raster. |
|
Build a fishnet grid that is culled to existing geometry. |
|
Write a raster data to a new gdal.Dataset object |
Attributes
- urclib.common_utils.gdt_np_map
- urclib.common_utils._ogrTypeLabels
- urclib.common_utils._ogrPointTypes
- urclib.common_utils._ogrLineTypes
- urclib.common_utils._ogrPolyTypes
- urclib.common_utils._ogrMultiTypes
- urclib.common_utils._ogrErrLabels
- urclib.common_utils.GEOTIFF_OPTIONS = ['GEOTIFF_KEYS_FLAVOR=STANDARD', 'TFW=YES']
- class urclib.common_utils.UrcWorkspace(workspace_dir=None, **kwargs)
Bases:
objectManages filepaths associated with a collection of data.
- workspace
Path to the root directory of the workspace collection; defaults to current working directory.
- Type:
str,optional
- Parameters:
workspace_dir (str) – The root directory for the workspace.
**kwargs – Additional key-path pairs to assign.
- __getitem__(item)
Retrieve a path for a given key.
- Parameters:
item (str) – The path to retrieve.
- Returns:
The requested path.
- Return type:
str
- Raises:
KeyError – If item does not exist in self.
- __setitem__(key, value)
Assign a path to a key.
- Parameters:
key (str) – The key to identify the path with.
value (str) – The path to assign.
- Raises:
ValueError – value is not of type str.
- __contains__(item)
- __iter__()
- __len__()
- __delitem__(key)
- update(invals)
Update contents of workspaces with that of a dict.
- Parameters:
invals (dict) – The content to update the workspace with.
- __repr__()
Return repr(self).
- keys()
Retrieve the keys for filepaths.
- Returns:
The names used to identify individual file paths.
- Return type:
dict_keys
- items()
Provide Iterator for walking through labels, and associated paths.
- Yields:
Tuple – label, and full path, respectively
- get(key, default=None)
Retrieve value of key if it exists; otherwise return the default value.
- Parameters:
key (str) – The tag of the path to retrieve.
default (object,optional) – The default value to pass if a value for key does not exist.
- Returns:
The value for key, or the value of default if no value for key exists.
- Return type:
object
- Raises:
KeyError – if key is not present in the in_workspace and default is None.
- delete_files(*args)
Delete the specified files.
- Parameters:
*args – List of keys of files to delete.
- test_files_exist()
Test each path entry to determine if path exists.
- Returns:
- (dlg_label,exists) for each entry in REE_workspace, where “exists” is True or False depending on
whether a file is found at location pointed to by associated path.
- Return type:
list
- exists(key)
Test to see if a given file exists in the filesystem.
- Parameters:
key (str) – The dlg_label for the path to query.
- Returns:
True if the key and associated file both exist; False otherwise.
- Return type:
bool
- urclib.common_utils.do_time_capture()
Context which prints the time it took to get from beginning to end of the with block, in seconds.
- urclib.common_utils.print_timestamp(raw_seconds)
Print raw seconds in nicely hour, minute, seconds format.
- Parameters:
raw_seconds (float) – The raw seconds to print.
- urclib.common_utils.parse_workspace_args(vals, workspace, outputs)
Parse out script arguments into a workspace.
- Parameters:
vals (dict) – The arguments to parse.
workspace (UrcWorkspace) – The destination of any values prefixed with IN_.
outputs (UrcWorkspace) – The destination of any values prefixed with OUT_.
- urclib.common_utils.list_fieldnames(featureclass)
Lists the fields in a feature class, shapefile, or table in a specified dataset.
- Parameters:
featureclass (osgeo.ogr.Layer) – Layer to query for field names.
- Returns:
The names of each field (as strs).
- Return type:
list
- urclib.common_utils.delete_file(path)
Remove a file if present.
- Parameters:
path (str) – The file to delete, if present.
- Returns:
True if file existed and deleted; False if file was not present
- Return type:
bool
- urclib.common_utils.raster_domain_intersect(in_coords, in_mask, src_sref, join_lyr, fld_name, nodata=-9999)
Create intersect raster for specific field values in vector layer
- Parameters:
in_coords (np.ndarray) – Map from pixel to space coordinates.
in_mask (np.ndarray) – Raw data from mask layer, with 1 is include, 0 is exclude.
src_sref (osr.SpatialReference) – The spatial reference to project into.
join_lyr (ogr.Layer) – The vector layer to parse.
fld_name (str) – The name of the field to use for indexing.
nodata (int,optional) – The value to use to represent “no data” pixels. defaults to -9999.
- Returns:
index values corresponding to pixel coordinates as defined with inCoords.
- Return type:
np.ndarray
- urclib.common_utils.rasterize(id, fc_list, in_ds, xsize, ysize, geotrans, srs, drvr_name='mem', prefix='', suffix='', nodata=-9999, gdtype=gdal.GDT_Int32, opts=None)
Convert specified Vector layers to raster.
- Parameters:
id (str) – The id for the new Raster dataset.
fc_list (list) – A list of list of layers to rasterize.
in_ds (gdal.Dataset) – The input dataset.
xsize (int) – The width of the new raster, in pixels.
ysize (int) – The height of the new raster, in pixels.
geotrans (tuple) – Matrix of float values describing geographic transformation.
srs (osr.SpatialReference) – The Spatial Reference System to provide for the new Raster.
drvr_name (str,optional) – Name of driver to use to create new raster; defaults to “MEM”.
prefix (str,optional) – Prefix to apply to comp_name for gdal.Dataset dlg_label; this could be a path to a directory if raster is being saved to disk.
suffix (str,optional) – Suffix to apply to comp_name for gdal.Dataset dlg_label; this could be the file extension if raster is being saved to disk.
nodata (numeric,optional) – The value to represent no-data in the new Raster; default is -9999
gdtype (int,optional) – Flag indicating the data type for the raster; default is “gdal.GDT_Int32”.
opts (list,optional) – String flags to forward to GDAL drivers, if any.
- Returns:
The rasterized vector layer.
- Return type:
gdal.Dataset
- urclib.common_utils.index_features(in_lyr, cell_width, cell_height, drivername='MEM', create_options=None)
Build a fishnet grid that is culled to existing geometry.
- Parameters:
in_lyr (osgeo.ogr.Layer) – The Layer containing the geometry to act as a rough mask.
cell_width (float) – The width of each cell.
cell_height (float) – The height of each cell.
drivername (str,optional) – The driver to use for generating the mask raster. Defaults to MEM.
create_options (list,optional) – String flags to forward to GDAL drivers, if any.
- Returns:
numpy array for coordinate mapping, and gdal.Dataset with masking info.
- Return type:
tuple
- urclib.common_utils.write_raster(mask_lyr, data, name, drivername='GTiff', gdtype=gdal.GDT_Byte, nodata=-9999)
Write a raster data to a new gdal.Dataset object
- Parameters:
mask_lyr (gdal.Dataset) – Raster object containing mask, dimension, and geotransform information.
data (np.ndarray) – The data to write to the Dataset
name (str) – The unique identifier and (depending on the driver) the path to the file to write.
drivername (str,optional) – The driver to use to create the dataset. Defaults to GTiff.
gdtype (int,optinal) – The internal data type to use in the generated raster. Defaults to gdal.GDT_Byte.
nodata (int,optional) – The value to use to represent “no data” pixels. defaults to -9999.
- Returns:
Reference to newly created dataset; can be safely ignored if just writing to disk.
- Return type:
gdal.Dataset