plans.qutils#
Utility routines for QGIS 3.x Python console
Overview#
Required dependencies in QGIS:
pcraster
saga
gdal
processing
geopandas
Example#
# todo [major docstring improvement] – examples Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla mollis tincidunt erat eget iaculis. Mauris gravida ex quam, in porttitor lacus lobortis vitae. In a lacinia nisl.
import numpy as np
print("Hello World!")
Mauris gravida ex quam, in porttitor lacus lobortis vitae. In a lacinia nisl. Mauris gravida ex quam, in porttitor lacus lobortis vitae. In a lacinia nisl.
Functions
|
|
|
Convert lulc values from source map. |
|
Get the number of features from a vector layer |
|
|
|
Calculates the area of specified basins from a raster file. |
|
Get all |
|
Get a blank raster copy from other raster |
|
Generates a boolean raster based on a condition applied to an input raster. |
|
Apply a carving method to DEM raster |
|
Get a reprojected DEM raster from a larger DEM dataset |
|
Get the basin Ids from downstream cells. |
|
|
|
Get the extent from a raster layer |
|
Get the extent from a vector layer |
|
Get the Fuzzy map of a linear membership. |
|
Get the HAND raster map from a DEM. |
|
Get the HAND-enhanced TWI map from HAND, TWI and HAND weight |
|
Get a list of HTWI based on many weights |
|
Get lulc maps from source and convert to plans-format. |
|
Get LULC maps from a larger main LULC dataset. |
|
|
|
Get |
|
|
|
Get all |
|
Get the drainage boolean mask using TPS method |
|
Get the TWI map from Slope and Upslope Area |
|
Check if extent B is bounded by extent A |
|
Reproject a vector layer |
|
Retrieve lulc series maps from a source folder. |
|
Retrieve and merge raster tiles into single raster |
|
Retrieves and saves upstream basins for a given set of gauges. |
|
Setup protocols for getting |
|
- plans.qutils.clip_layer(db_input, layer_input, db_overlay, layer_overlay, db_output, layer_output)[source]#
- plans.qutils.reproject_layer(db_input, layer_name, crs_target, db_output=None, layer_output=None)[source]#
Reproject a vector layer
- Parameters:
db_input (str) – path to geopackage database
crs_target (str) – EPSG code (number only) for the target CRS
layer_name (str) – name of the layer in database
- Returns:
new layer name (echo)
- Return type:
str
- plans.qutils.is_bounded_by(extent_a, extent_b)[source]#
Check if extent B is bounded by extent A
- Parameters:
extent_a (dict) – extent A
extent_b (dict) – extent B
- Returns:
Case if bounded
- Return type:
bool
- plans.qutils.get_extent_from_raster(file_input)[source]#
Get the extent from a raster layer
- Parameters:
file_input (str) – file path to raster layer
- Returns:
dictionary of bouding box: xmin, xmax, ymin, ymax
- Return type:
dict
- plans.qutils.get_extent_from_vector(db_input, layer_name)[source]#
Get the extent from a vector layer
- Parameters:
db_input (str) – path to geopackage database
layer_name (str) – name of the layer in geopackage database
- Returns:
dictionary of bouding box: xmin, xmax, ymin, ymax
- Return type:
dict
- plans.qutils.count_vector_features(input_db, layer_name)[source]#
Get the number of features from a vector layer
- Parameters:
input_db (str or Path) – path to geopackage database
layer_name (str) – name of the layer in geopackage database
- Returns:
number of features
- Return type:
int
- plans.qutils.retrieve_raster_tiles(db_aoi, layer_aoi, db_tiles, layer_tiles, folder_raster, folder_output, name_output, tile_field='tile_code')[source]#
Retrieve and merge raster tiles into single raster
- Parameters:
db_aoi (str) – path to Area Of Interest database (geopackage)
layer_aoi (str) – name of aoi layer
db_tiles (str) – path to tiles database (geopackage)
layer_tiles (str) – name of tile layer
folder_raster (str) – path to input raster directory (tiles)
folder_output (str) – path to output raster directory (merged)
name_output (str) – name of output raster file (merged) – no extension
tile_field (str) – name of tile field
- Returns:
path to output raster file (merged)
- Return type:
str
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function dem_src = qutils.retrieve_raster_tiles( db_aoi="/path/to/aoi_database.gpkg", layer_aoi="layer", db_tiles="/path/to/tile_database.gpkg", layer_tiles="tiles", folder_raster="/path/to/folder", folder_output="/path/to/folder", name_output="src_dem" )
- plans.qutils.retrieve_upstream_basins(db_basins, layer_basin, db_gauges, layer_gauges, db_output, layer_output, field_gauge, field_basin, field_basin_down, field_geometry='geometry')[source]#
Retrieves and saves upstream basins for a given set of gauges.
This function loads basin and gauge data from GeoPackage files, identifies the upstream basins for each gauge, and then saves the resulting GeoDataFrame to a new GeoPackage file. The upstream basins for each gauge are dissolved into a single feature before saving.
- Parameters:
db_basins (str) – The path to the GeoPackage database containing the basin features.
layer_basin (str) – The name of the layer in db_basins containing the basin features.
db_gauges (str) – The path to the GeoPackage database containing the gauge features.
layer_gauges (str) – The name of the layer in db_gauges containing the gauge features.
db_output (str) – The path to the output GeoPackage database where the results will be saved.
layer_output (str) – The name of the layer to be created in db_output.
field_gauge (str) – The name of the column in gauges that contains the unique identifier for each gauge.
field_basin (str) – The name of the column in basins that contains the unique identifier for each basin.
field_basin_down (str) – The name of the column in basins that contains the unique identifier of the downstream basin.
field_geometry (str) – The name of the geometry column. Default value = “geometry”
- Returns:
None
- Return type:
None
- plans.qutils.get_basins_areas(file_basins, ids_basins)[source]#
Calculates the area of specified basins from a raster file.
This function reads a raster file representing different basins, identifies the cells corresponding to each basin ID provided, calculates the total area for each basin in square kilometers, and returns the results in a dictionary.
- Parameters:
file_basins (str) – The file path to the raster containing the basin IDs.
ids_basins (list) – A list of basin IDs for which to calculate the area.
- Returns:
A dictionary containing the basin IDs and their corresponding upstream areas in square kilometers.
- Return type:
dict
- plans.qutils.get_blank(file_input, file_output, blank_value=0, dtype='byte')[source]#
Get a blank raster copy from other raster
- Parameters:
file_input (str) – path to inputs raster file
file_output (str) – path to output raster file
blank_value (int) – value for constant blank raster
dtype (str) – output raster data type (“byte”, “int” – else defaults to float)
- Returns:
file path of output (echo)
- Return type:
str
# todo [script example]
- plans.qutils.get_boolean(file_input, file_output, bool_value, condition='ET')[source]#
Generates a boolean raster based on a condition applied to an input raster. This function reads a raster, applies a user-defined condition (greater than, less than, or equal to) with a specified value, and creates a new raster where cells meeting the condition are set to 1 and all others are set to 0. The output is saved as a byte integer raster file.
- Parameters:
file_input (str) – The file path to the input raster.
file_output (str) – The file path where the output boolean raster will be saved.
bool_value (int or float) – The value against which to apply the condition.
condition (str) – The condition to apply (“GT” for greater than, “LT” for less than, “ET” for equal to). Default value = “ET”
- Returns:
The file path of the created output raster.
- Return type:
str
- plans.qutils.get_fuzzy(file_input, file_output, low_bound, high_bound)[source]#
Get the Fuzzy map of a linear membership. Reverse bounds to reverse membership.
- Parameters:
file_input (str) – path to raster file input
file_output (str) – path to raster file output
low_bound (float) – value of low bound
high_bound (float) – value of high bound
- Returns:
path of output (echo)
- Return type:
str
# todo [script example]
- plans.qutils.get_dem(file_src_dem, file_output, target_crs, target_extent, target_cellsize=30, source_crs='4326')[source]#
Get a reprojected DEM raster from a larger DEM dataset
- Parameters:
file_src_dem (str) – file path to sourced (larger) DEM dataset raster
file_output (str) – file path to output raster
target_crs (str) – EPSG code (number only) for the target CRS
target_extent (dict) – dictionary of bouding box: xmin, xmax, ymin, ymax
target_cellsize (float) – target resolution of cellsize in map units (degrees or meters)
source_crs (str) – EPSG code (number only) for the source CRS. default is 4326 (WGS-84)
- Returns:
file path of output (echo)
- Return type:
str
# todo [script example]
- plans.qutils.get_downstream_ids(file_ldd, file_basins, file_outlets)[source]#
Get the basin Ids from downstream cells.
ldd - Direction convention:
7 8 9 4 5 6 1 2 3
- Parameters:
file_ldd (str) – file path to LDD raster (map of LDD direction convention)
file_basins (str) – file path to basins raster
file_outlets (str) – file path to outlets raster
- Returns:
dictionaty with lists of “Id” and “Downstream_Id”
- Return type:
dict
# todo [script example]
- plans.qutils.get_carved_dem(file_dem, file_rivers, file_output, wedge_width=3, wedge_depth=10)[source]#
Apply a carving method to DEM raster
- Parameters:
file_dem (str) – file path to DEM raster
file_rivers (str) – file path to main rivers pseudo-boolean raster
file_output (str) – file path to output raster
wedge_width (int) – width parameter in unit cells (pixels)
wedge_depth (float) – depth parameter in dem units (e.g., meters)
- Returns:
file path of output (echo)
- Return type:
str
# todo [script example]
- plans.qutils.get_shalstab(file_slope, file_flowacc, output_folder, suffix, cellsize=30, soil_phi=15, soil_z=1, soil_p=1600, soil_c=1, water_p=997)[source]#
- plans.qutils.get_tps(file_tpi, file_upa, file_output, upa_min=0.01, upa_max=2, tpi_v=-2, tpi_r=10)[source]#
Get the drainage boolean mask using TPS method
- Parameters:
file_tpi (str) – path to tpi raster file
file_upa (str) – path to upa raster file
file_output (str) – path to output raster file
tpi_v (float) – TPI threshold for valley definition
upa_min (float) – Upslope area for exclusion zone, in sqkm (0.01 = 1 ha)
upa_max (float) – Upslope area for inclusion zone, in sqkm (0.01 = 1 ha)
- Returns:
file path of output (echo)
- Return type:
str
- plans.qutils.get_twi(file_slope, file_upa, file_output)[source]#
Get the TWI map from Slope and Upslope Area
- Parameters:
file_slope (str) – file path to Slope raster map
file_upa (str) – file path to Upslope Area raster map
file_output (str) – file path to output raster
- Returns:
file path of output (echo)
- Return type:
str
- plans.qutils.get_htwi(file_ftwi, file_fhand, file_output, hand_w)[source]#
Get the HAND-enhanced TWI map from HAND, TWI and HAND weight
- Parameters:
file_ftwi (str) – path to TWI raster (expected to be fuzzified)
file_fhand (str) – path to HAND raster (expected to be fuzzified)
file_output (str) – path to output raster
hand_w (int) – weight of HAND from 0 to 100
- Returns:
file path of output (echo)
- Return type:
str
# todo [script example]
- plans.qutils.get_htwi_list(file_ftwi, file_fhand, folder_output, ls_weights=[0, 50, 100])[source]#
Get a list of HTWI based on many weights
- Parameters:
file_ftwi (str) – path to TWI raster (expected to be fuzzified)
file_fhand (str) – path to HAND raster (expected to be fuzzified)
folder_output – path to output folder
ls_weights (list) – list of weights to use for each raster
- Returns:
None
- Return type:
None
# todo [script example]
- plans.qutils.get_hand(folder_project, file_dem_filled, file_dem_sample=None, file_drainage=None, hand_min_upa=0.01)[source]#
Get the HAND raster map from a DEM. The DEM must be hydrologically consistent (no sinks). PLANS-based folder structure is expected
- Parameters:
folder_project (str) – path to output folder
file_dem_filled (str) – file path to filled DEM (no sinks)
file_dem_sample (str) – file path to original DEM for sampling HAND
file_drainage (str) – [optional] file path to drainage boolean map
hand_min_upa (float) – minimal upslope area threshhold for HAND in sq km (0.01 sqkm = 1ha)
- Returns:
dictionary with raster output paths. keys: “ldd”, “material”, “accflux”, “threshold”, “drainage”, “hs_outlets_scalar”, “hs_outlets_nominal”, “hillslopes”, “hs_zmin”, “hand”
- Return type:
dict
# todo [script example]
- plans.qutils.setup_topo(folder_project, file_src_dem, crs_target, db_aoi, layer_aoi, db_rivers, layer_river_lines, layer_river_polygons=None, crs_source='4326', target_cellsize=30, translate_to_ascii=False)[source]#
Setup protocols for getting
topo
datasets. PLANS-based folder structure is expected- Parameters:
folder_project (str) – path to main plans project folder
file_src_dem (str) – file path to larger source DEM raster – expected to be in WGS-84 (4326)
crs_target (str) – QGIS code (number only) for the target CRS
db_aoi (str) – path to AOI layer geopackage database
layer_aoi (str) – name of Area Of Interest layer (polygon).
db_rivers (str) – path to rivers layer geopackage database
layer_river_lines (str) – name of main rivers lines layer.
layer_river_polygons (str) – name of main rivers polygon layer (optional).
crs_source (str) – QGIS code (number only) for the source CRS. default is WGS-84 (4326)
translate_to_ascii (bool) – flag to convert to asc format
- Returns:
dictionary of output resources
- Return type:
dict
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function qutils.setup_topo( folder_project="/path/to/folder", file_src_dem="/path/to/src_dem.tif", crs_target="5641", db_aoi="/path/to/aoi_database.gpkg", layer_aoi="layer", db_rivers="/path/to/river_database.gpkg", layer_river_lines="river_lines", layer_river_polygons="river_pols", crs_source="4326", )
- plans.qutils.get_topo(folder_project, file_dem, db_rivers, layer_rivers, wedge_width=3, wedge_depth=10, hand_threshold=5, use_saga=False, fill_xxl=False, translate_to_ascii=False, style_folder=None)[source]#
Get all
topo
datasets for running PLANS. PLANS-based folder structure is expected.Warning
run
setup_topo()
first.- Parameters:
folder_project (str) – path to project folder
file_dem (str) – file path to larger DEM dataset raster – expected to be in WGS-84
db_rivers (str) – path to geopackage database
layer_rivers (str) – name of main rivers layers (lines) in target CRS.
wedge_width (int) – carving dem parameter – width parameter in unit cells
wedge_depth (float) – carving dem parameter – depth parameter in dem units (e.g., meters)
hand_threshold (int) – [hand parameter in sqkm] threshhold for HAND in number of maximum hillslope area
translate_to_ascii (bool) – flag to convert to asc format
style_folder (str) – path to folder containing style files
- Returns:
None
- Return type:
None
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function qutils.get_topo( folder_project="/path/to/folder", # change paths! file_dem="/path/to/dem.tif", db_rivers="/path/to/database.gpkg", layer_rivers='rivers', wedge_width=3, wedge_depth=10, hand_threshold=5, translate_to_ascii=False, )
- plans.qutils.retrieve_lulc(folder_src, folder_project, crs_target, crs_src, file_target, file_style_src=None)[source]#
Retrieve lulc series maps from a source folder. PLANS-based folder structure is expected.
- Parameters:
folder_src (str) – folder path to source maps – Files are expected to end with _YYYY-MM-DD.tif
folder_project (str) – path to project folder
crs_target (str) – Code for CRS of target map
crs_src (str) – Code for CRS of sourced maps
file_target (str) – path to target map (eg, dem file)
file_style_src (str) – path to QML style (optional)
- Returns:
None
- Return type:
None
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function qutils.retrieve_lulc( folder_src="/path/to/lulc/folder", # change paths! folder_project="/path/to/project", crs_target="5641", crs_src="4326", file_target="/path/to/dem.tif", file_style_src="/path/to/lulc_mapbiomas.qml" )
- plans.qutils.convert_lulc(folder_src, folder_project, file_conversion_table, prefix_src='src_fill', id_src='id_mapbiomas', file_style=None)[source]#
Convert lulc values from source map. PLANS-based folder structure is expected.
- Parameters:
folder_src (str) – path to sourced maps
folder_project (str) – path to project
file_conversion_table (str) – path to CSV table for conversion ids
prefix_src (str) – prefix in source maps
id_src (str) – name of column storing the id field for source maps
file_style (str) – path to converted style QML file
- Returns:
None
- Return type:
None
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function qutils.convert_lulc( folder_src="/path/to/folder", # change paths! folder_project=/path/to/folder, file_conversion_table="/path/to/folder/lulc_conversion.csv", prefix_src="src_fill", id_src="id_mapbiomas", file_style="/path/to/folder/lulc.qml" )
- plans.qutils.get_lulc(folder_src, folder_project, crs_target, file_target, file_conversion_table, crs_src='4326', id_src='id_mapbiomas', file_style_src=None, file_style=None, db_roads=None, layer_roads='roads')[source]#
Get lulc maps from source and convert to plans-format. PLANS-based folder structure is expected.
- Parameters:
folder_src (str) – folder path to source maps – Files are expected to end with _YYYY-MM-DD.tif
folder_project (str) – path to project folder
crs_target (str) – Code for CRS of target map
crs_src (str) – Code for CRS of sourced maps
file_target (str) – path to target map (eg, dem file)
file_style_src (str) – path to QML style (optional)
file_conversion_table (str) – path to CSV table for conversion ids
id_src (str) – name of column storing the id field for source maps
file_style (str) – path to converted style QML file
- Returns:
None
- Return type:
None
- plans.qutils.get_basins(folder_project, db_outlets, layer_outlets, code_outlets, crs_target)[source]#
- plans.qutils.get_rain_old(output_folder, src_folder, input_db, target_file, target_crs, layer_aoi='aoi', layer_rain_gauges='rain')[source]#
Get
rain
datasets for running PLANS.- Parameters:
output_folder (str) – path to output folder
src_folder (str) – path to source folder for all csv series files. Files must be named by rain_<alias>.csv
input_db (str) – path to geopackage database with rain gauge layer
target_file (str) – file path to target raster (e.g., the
DEM
raster)target_crs (str) – EPSG code (number only) for the target CRS
layer_aoi (str) – name of Area Of Interest layer (polygon). Default is “aoi”
layer_rain_gauges (str) –
layer name of rain gauges layer. Required fields in layer:
Id
: int, required. Unique number id.Name
: str, required. Simple name.Alias
: str, required. Short nickname.Rain_File
: str, required. Path to data time seriescsv
source file.X
: float, optional. Recomputed from layer. Longitude in WGS 84 Datum (EPSG4326).Y
: float, optional. Recomputed from layer. Latitude in WGS 84 Datum (EPSG4326).Code
: str, opSource
: str, requiredDescription
: str, requiredColor
: str, optional
- Returns:
None
- Return type:
None
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function qutils.get_rain( output_folder="path/to/rain", # change paths! src_folder="path/to/src", target_file="path/to/dem.asc", target_crs="31982", input_db="path/to/my_db.gpkg", layer_aoi="aoi", layer_rain_gauges="rain" )
- plans.qutils.get_lulc_old(list_main_files, list_dates, lulc_table, output_folder, target_file, crs_target, db_roads=None, layer_roads_dirty='roads_dirty', layer_roads_paved='roads_paved', source_crs='4326', qml_file=None, translate=True)[source]#
Get LULC maps from a larger main LULC dataset.
- Parameters:
list_main_files (list) – list of file paths to main lulc rasters
list_dates (list) – list of dates (YYYY-MM-DD) for each main raster
lulc_table (str) – path to parameter table file. Expected columns: Id, Name, Alias and Color
output_folder (str) – path to output folder
target_file (str) – file path to target raster (e.g., the DEM raster)
crs_target (str) – EPSG code (number only) for the target CRS
db_roads (str) – [optional] path to geopackage database with road layers
layer_roads_dirty (str) – layer name of dirty roads
layer_roads_paved (str) – layer name of paved roads
source_crs (str) – EPSG code (number only) for the source CRS. default is WGS-84 (4326)
qml_file (str) – [optional] file path to QML style file
- Returns:
None
- Return type:
None
Script example (for QGIS):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # set files lst_files = [ "path/to/lulc_2015.tif", "path/to/lulc_2015.tif", "path/to/lulc_2016.tif", ] # set dates lst_dates = [ "2014-01-01", "2015-01-01", "2016-01-01", ] # run function qutils.get_lulc( list_main_files=lst_files, list_dates=lst_dates, output_folder="path/to/output", target_file="/path/to/raster.tif", crs_target="5641", db_roads="path/to/input_db.gpkg", layer_roads_dirty='roads_dirty', layer_roads_paved='roads_paved', source_crs='4326', qml_file="path/to/style.qml" )
- plans.qutils.get_basins_old(output_folder, input_db, ldd_file, target_crs, layer_stream_gauges='stream')[source]#
Get all
basins
datasets for running PLANS.- Parameters:
output_folder (str) – path to output folder
input_db (str) – path to geopackage database with streamflow gauges layer
ldd_file (str) – file path to LDD raster
target_crs (str) – EPSG code (number only) for the target CRS
layer_stream_gauges (str) –
layer name of streamflow gauges layer Required fields in layer:
Id
: int, required. Unique number id.Name
: str, required. Simple name.Alias
: str, required. Short nickname.X
: float, optional. Recomputed from layer. Longitude in WGS 84 Datum (EPSG4326).Y
: float, optional. Recomputed from layer. Latitude in WGS 84 Datum (EPSG4326).Code
: str, opSource
: str, requiredDescription
: str, requiredColor
: str, optionalStage_File
: str, required. Path to data time seriescsv
source file.Flow_File
: str, required. Path to data time seriescsv
source file.
- Returns:
None
- Return type:
None
Script example (for QGIS Python Console):
# plans source code must be pasted to QGIS plugins directory from plans import qutils # call function qutils.get_basins( output_folder="path/to/basins", ldd_file="path/to/ldd.asc", target_crs="31982", input_db="path/to/my_db.gpkg", layer_stream_gauges="stream" )