# -*- coding: utf-8 -*-
"""
/***************************************************************************
plTools
A QGIS plugin
The plugin adds a set of algorithms from plTools
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2024-01-10
copyright : (C) 2024 by Pawel Netzel
email : pawel@netzel.pl
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'Pawel Netzel'
__date__ = '2024-01-10'
__copyright__ = '(C) 2024 by Pawel Netzel'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingException,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterRasterLayer)
from .algo_base import plToolsBaseAlgorithm
class forestfragment_Algorithm(plToolsBaseAlgorithm):
COMMAND = 'plforestfragment'
OUTPUT = 'OUTPUT'
INPUT = 'INPUT'
RADIUS1 = 'RADIUS1'
RADIUS2 = 'RADIUS2'
RADIUS3 = 'RADIUS3'
RADIUS4 = 'RADIUS4'
RADIUS5 = 'RADIUS5'
THREADS = 'THREADS'
FOREST = 'FOREST'
def initAlgorithm(self, config):
super().initAlgorithm(config)
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,self.tr('Input raster layer')))
self.addParameter(QgsProcessingParameterNumber(self.FOREST,self.tr('Value of forest category'),defaultValue=1,optional=True))
self.addParameter(QgsProcessingParameterNumber(self.RADIUS1,self.tr('Diameter for scale 1'),defaultValue=7,optional=False))
self.addParameter(QgsProcessingParameterNumber(self.RADIUS2,self.tr('Diameter for scale 2'),optional=True))
self.addParameter(QgsProcessingParameterNumber(self.RADIUS3,self.tr('Diameter for scale 3'),optional=True))
self.addParameter(QgsProcessingParameterNumber(self.RADIUS4,self.tr('Diameter for scale 4'),optional=True))
self.addParameter(QgsProcessingParameterNumber(self.RADIUS5,self.tr('Diameter for scale 5'),optional=True))
self.addParameter(QgsProcessingParameterNumber(self.THREADS,self.tr('Number of threads'),defaultValue=1,optional=True))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,self.tr('Forest fragmentation index')))
def processAlgorithm(self, parameters, context, feedback):
arguments = [self.COMMAND]
inp = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if inp:
arguments.append('-i')
arguments.append(inp.source())
rad1 = self.parameterAsInt(parameters, self.RADIUS1, context)
if rad1:
arguments.append('-r')
arguments.append(str(rad1))
rad2 = self.parameterAsInt(parameters, self.RADIUS2, context)
if rad2:
arguments.append('-r')
arguments.append(str(rad2))
rad3 = self.parameterAsInt(parameters, self.RADIUS3, context)
if rad3:
arguments.append('-r')
arguments.append(str(rad3))
rad4 = self.parameterAsInt(parameters, self.RADIUS4, context)
if rad4:
arguments.append('-r')
arguments.append(str(rad4))
rad5 = self.parameterAsInt(parameters, self.RADIUS5, context)
if rad5:
arguments.append('-r')
arguments.append(str(rad5))
forest = self.parameterAsInt(parameters, self.FOREST, context)
if forest:
arguments.append('-f')
arguments.append(str(forest))
thr = self.parameterAsInt(parameters, self.THREADS, context)
if thr:
arguments.append('-t')
arguments.append(str(thr))
output = self.parameterAsOutputLayer(parameters,self.OUTPUT,context)
arguments.append('-o')
arguments.append(output)
self.runplTools(arguments, feedback)
return {self.OUTPUT: output}
def name(self):
return 'Forest fragmentation index'
def groupId(self):
return ''
def createInstance(self):
return forestfragment_Algorithm()
def shortHelpString(self):
return '''
plForestFragment is a new approach to assessing the fragmentation of forests in a given area based on the similarity of a forested area's pattern to a pattern of a fully forested area. Jensen-Shannon similarity is used to create a measure of forest fragmentation. The proposed approach allows both the calculation of forest fragmentation at a given scale and the calculation of a multiscale fragmentation assessment as a single index.
The module uses a categorical raster layer as an input. It identifies forests by selecting one category. By default, it is a category of value 1 (the user can choose a different value).
The implemented approach was described in the paper.
The web page of the software is HERE.
'''