Pawel Netzel - software
Repositories
Help
Report an Issue
plTools
Code
Commits
Branches
Tags
Search
Tree:
4406deb
Branches
Tags
master
plTools
plTools_plugin
algo_plmapcalc.py
Initial commit
netzel
commited
4406deb
at 2024-02-21 23:00:39
algo_plmapcalc.py
Blame
History
Raw
# -*- 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, QgsProcessingParameterBoolean, QgsProcessingParameterEnum, QgsProcessingParameterString, QgsProcessingParameterFile, QgsProcessingParameterRasterDestination, QgsProcessingParameterMultipleLayers) from .algo_base import plToolsBaseAlgorithm class mapcalc_Algorithm(plToolsBaseAlgorithm): COMMAND = 'plmapcalc' OUTPUT1 = 'OUTPUT1' OUTPUT2 = 'OUTPUT2' OUTPUT3 = 'OUTPUT3' OUTPUT4 = 'OUTPUT4' OUTPUT5 = 'OUTPUT5' OUTTYP1 = 'OUTTYP1' OUTTYP2 = 'OUTTYP2' OUTTYP3 = 'OUTTYP3' OUTTYP4 = 'OUTTYP4' OUTTYP5 = 'OUTTYP5' ENUMTYPES = ['Byte','Int16','Int32','Float32','Float64'] OUTNULL1 = 'OUTNULL1' OUTNULL2 = 'OUTNULL2' OUTNULL3 = 'OUTNULL3' OUTNULL4 = 'OUTNULL4' OUTNULL5 = 'OUTNULL5' OUTCOMP1 = 'OUTCOMP1' OUTCOMP2 = 'OUTCOMP2' OUTCOMP3 = 'OUTCOMP3' OUTCOMP4 = 'OUTCOMP4' OUTCOMP5 = 'OUTCOMP5' ENUMCOMP = ['NONE','DEFLATE','LZW'] INPUTS = 'INPUTS' CODEBEGIN = 'CODEBEGIN' CODERUN = 'CODERUN' CODEEND = 'CODEEND' MEMCOUNT = 'MEMCOUNT' MEMZERO = 'MEMZERO' MEMREAD = 'MEMREAD' MEMSAVE = 'MEMSAVE' def initAlgorithm(self, config): super().initAlgorithm(config) self.addParameter(QgsProcessingParameterMultipleLayers(self.INPUTS,self.tr('Input raster layers'),QgsProcessing.TypeRaster,optional=False)) self.addParameter(QgsProcessingParameterString(self.CODEBEGIN,self.tr('Code BEGIN'),optional=True)) self.addParameter(QgsProcessingParameterString(self.CODERUN,self.tr('Code EXECUTE'),optional=False)) self.addParameter(QgsProcessingParameterString(self.CODEEND,self.tr('Code END'),optional=True)) self.addParameter(QgsProcessingParameterNumber(self.MEMCOUNT,self.tr('Memory buffer size'),optional=True)) self.addParameter(QgsProcessingParameterBoolean(self.MEMZERO,self.tr('Save with zeros'),defaultValue=False)) self.addParameter(QgsProcessingParameterFile(self.MEMREAD,self.tr('File to read into memory buffer'),optional=True)) self.addParameter(QgsProcessingParameterFile(self.MEMSAVE,self.tr('File to store memory buffer'),optional=True)) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT1,self.tr('Output layer 1'),optional=True,createByDefault=False)) self.addParameter(QgsProcessingParameterEnum(self.OUTTYP1,self.tr('Data type 1'),self.ENUMTYPES,defaultValue=4,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterNumber(self.OUTNULL1,self.tr('No-data value 1'),defaultValue=-9999,optional=True)) self.addParameter(QgsProcessingParameterEnum(self.OUTCOMP1,self.tr('Compression 1'),self.ENUMCOMP,defaultValue=0,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT2,self.tr('Output layer 2'),optional=True,createByDefault=False)) self.addParameter(QgsProcessingParameterEnum(self.OUTTYP2,self.tr('Data type 2'),self.ENUMTYPES,defaultValue=4,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterNumber(self.OUTNULL2,self.tr('No-data value 2'),defaultValue=-9999,optional=True)) self.addParameter(QgsProcessingParameterEnum(self.OUTCOMP2,self.tr('Compression 2'),self.ENUMCOMP,defaultValue=0,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT3,self.tr('Output layer 3'),optional=True,createByDefault=False)) self.addParameter(QgsProcessingParameterEnum(self.OUTTYP3,self.tr('Data type 3'),self.ENUMTYPES,defaultValue=4,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterNumber(self.OUTNULL3,self.tr('No-data value 3'),defaultValue=-9999,optional=True)) self.addParameter(QgsProcessingParameterEnum(self.OUTCOMP3,self.tr('Compression 3'),self.ENUMCOMP,defaultValue=0,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT4,self.tr('Output layer 4'),optional=True,createByDefault=False)) self.addParameter(QgsProcessingParameterEnum(self.OUTTYP4,self.tr('Data type 4'),self.ENUMTYPES,defaultValue=4,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterNumber(self.OUTNULL4,self.tr('No-data value 4'),defaultValue=-9999,optional=True)) self.addParameter(QgsProcessingParameterEnum(self.OUTCOMP4,self.tr('Compression 4'),self.ENUMCOMP,defaultValue=0,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT5,self.tr('Output layer 5'),optional=True,createByDefault=False)) self.addParameter(QgsProcessingParameterEnum(self.OUTTYP5,self.tr('Data type 5'),self.ENUMTYPES,defaultValue=4,allowMultiple=False,optional=True)) self.addParameter(QgsProcessingParameterNumber(self.OUTNULL5,self.tr('No-data value 5'),defaultValue=-9999,optional=True)) self.addParameter(QgsProcessingParameterEnum(self.OUTCOMP5,self.tr('Compression 5'),self.ENUMCOMP,defaultValue=0,allowMultiple=False,optional=True)) def processAlgorithm(self, parameters, context, feedback): arguments = [self.COMMAND,'-f'] inputs = self.parameterAsLayerList(parameters, self.INPUTS, context) if inputs is not None: for layer in inputs: arguments.append('-i') arguments.append(layer.source()) memcount = self.parameterAsInt(parameters, self.MEMCOUNT, context) if memcount: arguments.append('-m') arguments.append(str(memcount)) memzero = self.parameterAsBoolean(parameters, self.MEMZERO, context) if memzero: arguments.append('-0') memread = self.parameterAsFile(parameters, self.MEMREAD, context) if memread: arguments.append('-r') arguments.append(memread) memsave = self.parameterAsFileOutput(parameters, self.MEMSAVE, context) if memsave: arguments.append('-s') arguments.append(memsave) codebegin = self.parameterAsString(parameters, self.CODEBEGIN, context) if codebegin: arguments.append('--execute-begin="{}"'.format(codebegin)) coderun = self.parameterAsString(parameters, self.CODERUN, context) if coderun: arguments.append('--execute="{}"'.format(coderun)) codeend = self.parameterAsString(parameters, self.CODEEND, context) if codeend: arguments.append('--execute-end="{}"'.format(codeend)) output1 = self.parameterAsOutputLayer(parameters, self.OUTPUT1, context) if output1: outtyp1 = self.parameterAsEnum(parameters, self.OUTTYP1, context) outnull1 = self.parameterAsInt(parameters, self.OUTNULL1, context) outcomp1 = self.parameterAsEnum(parameters, self.OUTCOMP1, context) arguments.append('-o {}:{}:{}:{}'.format(output1,self.ENUMTYPES[outtyp1],outnull1,self.ENUMCOMP[outcomp1])) output2 = self.parameterAsOutputLayer(parameters, self.OUTPUT2, context) if output2: outtyp2 = self.parameterAsEnum(parameters, self.OUTTYP2, context) outnull2 = self.parameterAsInt(parameters, self.OUTNULL2, context) outcomp2 = self.parameterAsEnum(parameters, self.OUTCOMP2, context) arguments.append('-o {}:{}:{}:{}'.format(output2,self.ENUMTYPES[outtyp2],outnull2,self.ENUMCOMP[outcomp2])) output3 = self.parameterAsOutputLayer(parameters, self.OUTPUT3, context) if output3: outtyp3 = self.parameterAsEnum(parameters, self.OUTTYP3, context) outnull3 = self.parameterAsInt(parameters, self.OUTNULL3, context) outcomp3 = self.parameterAsEnum(parameters, self.OUTCOMP3, context) arguments.append('-o {}:{}:{}:{}'.format(output3,self.ENUMTYPES[outtyp3],outnull3,self.ENUMCOMP[outcomp3])) output4 = self.parameterAsOutputLayer(parameters, self.OUTPUT4, context) if output4: outtyp4 = self.parameterAsEnum(parameters, self.OUTTYP4, context) outnull4 = self.parameterAsInt(parameters, self.OUTNULL4, context) outcomp4 = self.parameterAsEnum(parameters, self.OUTCOMP4, context) arguments.append('-o {}:{}:{}:{}'.format(output4,self.ENUMTYPES[outtyp4],outnull4,self.ENUMCOMP[outcomp4])) output5 = self.parameterAsOutputLayer(parameters, self.OUTPUT5, context) if output5: outtyp5 = self.parameterAsEnum(parameters, self.OUTTYP5, context) outnull5 = self.parameterAsInt(parameters, self.OUTNULL5, context) outcomp5 = self.parameterAsEnum(parameters, self.OUTCOMP5, context) arguments.append('-o {}:{}:{}:{}'.format(output5,self.ENUMTYPES[outtyp5],outnull5,self.ENUMCOMP[outcomp5])) if not self.getEnv('PLMAPCALCPATH'): arguments.append('--path='+self.quoteString(self.getEnv('PLTOOLSPATH')+'/tcc')) self.runplTools(arguments, feedback) return {self.OUTPUT1: output1} def name(self): return 'plMapcalc - raster map calculator' def groupId(self): return '' def createInstance(self): return mapcalc_Algorithm() def shortHelpString(self): return ''' plMapcalc is another raster map calculator. It uses a TCC compiler to compile calculation rules. The program uses GDAL to access raster data. plMapcalc produces GeoTIFF files as an output. Analysis of plMapcalc was published in the <a href="https://doi.org/10.1016/j.cageo.2021.104824">paper</a>. The web page of the software containing help, manual, and examples is <a href="http://plmapcalc.netzel.pl">HERE</a>. The GUI exposes only part of the possible options. These options cover most of the needs. To use the full potential of the program, run it from the plTools Console. '''