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
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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 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 |
|
calculate_raster_value_area
Calculates the area of value pixels for the given band in a raster layer.
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
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
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:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
The log message |
required |
name |
str
|
Name of te log instance, qgis_cplus is the default |
'qgis_cplus'
|
info |
bool
|
Whether the message is about info or a warning |
True
|
notify |
bool
|
Whether to notify user about the log |
True
|
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 |