Utilities¶
Plugin utilities
CustomJsonEncoder ¶
FileUtils ¶
Provides functionality for commonly used file-related operations.
create_comparison_reports_dir
staticmethod
¶
Creates a comparison reports subdirectory under the base directory. Skips creation of the subdirectory if it already exists.
Source code in src/cplus_plugin/utils.py
create_ncs_carbon_dir
staticmethod
¶
Creates an NCS subdirectory for carbon layers under BASE_DIR. Skips creation of the subdirectory if it already exists.
Source code in src/cplus_plugin/utils.py
create_ncs_pathways_dir
staticmethod
¶
Creates an NCS subdirectory under BASE_DIR. Skips creation of the subdirectory if it already exists.
Source code in src/cplus_plugin/utils.py
create_new_dir
staticmethod
¶
Creates new file directory if it doesn't exist
Source code in src/cplus_plugin/utils.py
create_new_file
staticmethod
¶
Creates new file
create_npv_pwls_dir
staticmethod
¶
Creates an NPV PWL subdirectory under PWL child directory in the base directory. Skips creation of the subdirectory if it already exists.
Source code in src/cplus_plugin/utils.py
create_pwls_dir ¶
Creates priority weighting layers subdirectory under BASE_DIR. Skips creation of the subdirectory if it already exists.
Source code in src/cplus_plugin/utils.py
get_fonts_dir
staticmethod
¶
Returns the fonts directory in the plugin.
Returns:
Type | Description |
---|---|
str
|
Fonts directory. |
get_icon
staticmethod
¶
Creates an icon based on the icon name in the 'icons' folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
File name which should include the extension. |
required |
Returns:
Type | Description |
---|---|
QtGui.QIcon
|
Icon object matching the file name. |
Source code in src/cplus_plugin/utils.py
get_icon_path
staticmethod
¶
Gets the full path of the icon with the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
File name which should include the extension. |
required |
Returns:
Type | Description |
---|---|
str
|
The full path to the icon in the plugin. |
Source code in src/cplus_plugin/utils.py
get_pixmap
staticmethod
¶
Creates a pixmap based on the file name in the 'icons' folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
File name which should include the extension. |
required |
Returns:
Type | Description |
---|---|
QtGui.QPixmap
|
Pixmap object matching the file name. |
Source code in src/cplus_plugin/utils.py
plugin_dir
staticmethod
¶
Returns the root directory of the plugin.
Returns:
Type | Description |
---|---|
str
|
Root directory of the plugin. |
report_template_path
staticmethod
¶
Get the absolute path to the template file with the given name. Caller needs to verify that the file actually exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
Template file name including the extension. If none is specified then it will use |
None
|
Returns:
Type | Description |
---|---|
str
|
The absolute path to the template file with the given name. |
Source code in src/cplus_plugin/utils.py
align_rasters ¶
align_rasters(input_raster_source, reference_raster_source, extent=None, output_dir=None, rescale_values=False, resample_method=0)
Based from work on https://github.com/inasafe/inasafe/pull/2070 Aligns the passed raster files source and save the results into new files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_raster_source |
str
|
Input layer source |
required |
reference_raster_source |
str
|
Reference layer source |
required |
extent |
list
|
Clip extent |
None
|
output_dir |
str
|
Absolute path of the output directory for the snapped layers |
None
|
rescale_values |
bool
|
Whether to rescale pixel values |
False
|
resample_method |
QgsAlignRaster.ResampleAlg
|
Method to use when resampling |
0
|
Source code in src/cplus_plugin/utils.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|
calculate_raster_area ¶
Calculates the area of value pixels for the given band in a raster layer.
This varies from 'calculate_raster_area_by_pixel_value' in that it gives the total area instead of grouping by pixel value.
Please note that this function will run in the main application thread hence for best results, it is recommended to execute it in a background process if part of a bigger workflow.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
QgsRasterLayer
|
Input layer whose area for value pixels is to be calculated. |
required |
band_number |
int
|
Band number to compute area, default is band one. |
1
|
feedback |
QgsProcessingFeedback
|
Feedback object for progress during area calculation. |
None
|
Returns:
Type | Description |
---|---|
float
|
The total area of value pixels of the raster else -1 if the raster is invalid or if it is empty. Pixels with NoData value are not included in the computation. |
Source code in src/cplus_plugin/utils.py
calculate_raster_area_by_pixel_value ¶
Calculates the area of value pixels for the given band in a raster layer and groups the area by the pixel value.
Please note that this function will run in the main application thread hence for best results, it is recommended to execute it in a background process if part of a bigger workflow.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
QgsRasterLayer
|
Input layer whose area for value pixels is to be calculated. |
required |
band_number |
int
|
Band number to compute area, default is band one. |
1
|
feedback |
QgsProcessingFeedback
|
Feedback object for progress during area calculation. |
None
|
Returns:
Type | Description |
---|---|
float
|
A dictionary containing the pixel value as the key and the corresponding area in hectares as the value for all the pixels in the raster otherwise returns a empty dictionary if the raster is invalid or if it is empty. |
Source code in src/cplus_plugin/utils.py
clean_filename ¶
Creates a safe filename by removing operating system invalid filename characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
File name |
required |
Source code in src/cplus_plugin/utils.py
compress_raster ¶
compress_raster(input_path, output_path=None, compression_type='DEFLATE', compress_level=6, nodata_value=None, output_format='GTiff', create_options=None, additional_options=None)
Compresses a raster file using GDAL and optionally replace old NoData pixel values with a new one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_path |
str
|
Path to the input raster file |
required |
output_path |
str
|
Path to the input raster file. If none the ouput will saved to a temporary file |
None
|
compression_type |
str
|
Compression algorithm (e.g., 'DEFLATE', 'LZW', 'PACKBITS', 'JPEG', 'NONE') |
'DEFLATE'
|
compress_level |
int
|
Compression level (1-9 for DEFLATE/LZW, 1-100 for JPEG) |
6
|
nodata_value |
float
|
Value to set as nodata (default: None). If None, retain the input nodatavalue |
None
|
output_format |
str
|
Output format (default: 'GTiff' for GeoTIFF) |
'GTiff'
|
create_options |
list
|
Additional GDAL creation options as a list |
None
|
additional_options |
list
|
dditional GDAL options as a list |
None
|
Returns:
Type | Description |
---|---|
str | None
|
Path to the temporary file if successful, None if failed |
Source code in src/cplus_plugin/utils.py
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 |
|
contains_font_family ¶
Checks if the specified font family exists in the font database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
font_family |
str
|
Name of the font family to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the font family exists, else False. |
Source code in src/cplus_plugin/utils.py
convert_size ¶
Convert byte size to human readable text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size_bytes |
int
|
byte sizse |
required |
Returns:
Type | Description |
---|---|
str
|
human readable text |
Source code in src/cplus_plugin/utils.py
function_help_to_html ¶
Creates a HTML string containing the detailed help of an expression function.
The specific HTML formatting is deduced from the code here: https://github.com/qgis/QGIS/blob/master/src/core/expression/qgsexpression.cpp#L565
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function_name |
str
|
Name of the expression function. |
required |
description |
str
|
Detailed description of the function. |
required |
arguments |
List[tuple]
|
List containing the arguments. Each argument should consist of a tuple containing three elements i.e. argument name, description and bool where True will indicate the argument is optional. Take note of the order as mandatory arguments should be first in the list. |
None
|
examples |
List[tuple]
|
Examples of using the function. Each item in the list should be a tuple containing an example expression and the corresponding return value. |
None
|
Returns:
Type | Description |
---|---|
str
|
The expression function's help in HTML for use in, for example, an expression builder. |
Source code in src/cplus_plugin/utils.py
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 |
|
generate_random_color ¶
Generate a random color object using a system-seeded deterministic approach.
Returns:
Type | Description |
---|---|
QColor
|
A random generated color. |
Source code in src/cplus_plugin/utils.py
get_layer_type ¶
Get layer type code from file path
Source code in src/cplus_plugin/utils.py
get_plugin_version ¶
Returns the current plugin version as saved in the metadata.txt plugin file.
Returns:
Type | Description |
---|---|
str
|
Plugin version |
Source code in src/cplus_plugin/utils.py
get_report_font ¶
Uses the default font family name to create a font for use in the report.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
float
|
The font point size, default is 11. |
11.0
|
bold |
bool
|
True for bold font else False which is the default. |
False
|
italic |
bool
|
True for font to be in italics else False which is the default. |
False
|
Returns:
Type | Description |
---|---|
QtGui.QFont
|
Font to use in a report. |
Source code in src/cplus_plugin/utils.py
install_font ¶
Installs the font families in the specified folder name under the plugin's 'fonts' folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_name |
str
|
Directory name, under the 'fonts' folder, which contains the font families to be installed. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the font(s) were successfully installed, else False if the directory name does not exist or if the given font families already exist in the application's font database. |
Source code in src/cplus_plugin/utils.py
log ¶
Logs the message into QGIS logs using qgis_cplus as the default log instance. If notify_user is True, user will be notified about the log.
Parameters:
Source code in src/cplus_plugin/utils.py
md5 ¶
Get md5 checksum off a file
open_documentation ¶
Opens documentation website in the default browser
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
URL link to documentation site (e.g. gh pages site) |
None
|
Source code in src/cplus_plugin/utils.py
todict ¶
Convert any object to dictionary
Source code in src/cplus_plugin/utils.py
tr ¶
Get the translation for a string using Qt translation API. We implement this ourselves since we do not inherit QObject.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str, QString
|
String for translation. |
required |
Returns:
Type | Description |
---|---|
QString
|
Translated version of message. |
Source code in src/cplus_plugin/utils.py
transform_extent ¶
Transforms the passed extent into the destination crs
:param extent: Target extent
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_crs |
QgsCoordinateReferenceSystem
|
Source CRS of the passed extent |
required |
dest_crs |
QgsCoordinateReferenceSystem
|
Destination CRS |
required |
Source code in src/cplus_plugin/utils.py
zip_shapefile ¶
Zip shapefile to an object with same name.
For example, the .shp filename is test_file.shp
, then the zip file
name would be test_file.zip
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shapefile_path |
str
|
Path of the shapefile |
required |
Returns:
Type | Description |
---|---|
str
|
Zip file path if the specified |