Skip to content

Commit

Permalink
First working raster algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric Ribreux committed Nov 4, 2017
1 parent 483a4b9 commit 9a52e34
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
32 changes: 15 additions & 17 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -59,16 +59,9 @@
QgsProcessingOutputHtml)
from qgis.utils import iface

#from processing.core.GeoAlgorithm import GeoAlgorithm (replaced by QgsProcessingAlgorithm)
from processing.core.ProcessingConfig import ProcessingConfig
#from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException (replaced by QgsProcessingException).

from processing.core.parameters import (getParameterFromString)
#from processing.core.outputs import (getOutputFromString,
# OutputRaster,
# OutputVector,
# OutputFile,
# OutputHTML)

from .Grass7Utils import Grass7Utils

Expand Down Expand Up @@ -183,7 +176,6 @@ def initAlgorithm(self, config=None):
def defineCharacteristicsFromFile(self):
"""
Create algorithm parameters and outputs from a text file.
"""
with open(self.descriptionFile) as lines:
# First line of the file is the Grass algorithm name
Expand Down Expand Up @@ -308,6 +300,7 @@ def grabDefaultGrassParameters(self, parameters, context):
self.region = self.parameterAsExtent(parameters,
self.GRASS_REGION_EXTENT_PARAMETER,
context)
QgsMessageLog.logMessage('processAlgorithm self.region: {}'.format(self.region.isEmpty()), 'Grass7', QgsMessageLog.INFO)
# GRASS cell size
self.cellSize = self.parameterAsString(parameters,
self.GRASS_REGION_CELLSIZE_PARAMETER,
Expand Down Expand Up @@ -426,24 +419,25 @@ def processInputs(self, parameters, context):
if not paramName in parameters:
continue
layer = self.parameterAsRasterLayer(parameters, paramName, context)
layerSrc = self.parameterAsCompatibleSourceLayerPath(parameters, paramName, context,
QgsVectorFileWriter.supportedFormatExtensions())

layerSrc = parameters[paramName]
# Check if the layer hasn't already been exported in, for
# example, previous GRASS calls in this session
if paramName in self.exportedLayers:
continue
else:
layers.append(layer)
self.setSessionProjectionFromLayer(layer)
self.commands.append(self.exportRasterLayer(paramName, layerSrc))
# Vector inputs needs to be imported into temp GRASS DB
if isinstance(param, QgsProcessingParameterVectorLayer):
if not paramName in parameters:
continue
layer = self.parameterAsVectorLayer(parameters, paramName, context)
layerSrc = self.parameterAsCompatibleSourceLayerPath(parameters, paramName, context,
QgsVectorFileWriter.supportedFormatExtensions())

layerSrc = self.parameterAsCompatibleSourceLayerPath(
parameters, paramName, context,
self.provider().supportedOutputVectorLayerExtensions()
)

if paramName in self.exportedLayers:
continue
else:
Expand Down Expand Up @@ -482,11 +476,11 @@ def processInputs(self, parameters, context):
self.setSessionProjectionFromProject()

# Build GRASS region
if not self.region:
if self.region.isEmpty():
self.region = QgsProcessingUtils.combineLayerExtents(layers)
command = 'g.region n={} s={} e={} w={}'.format(
self.region.yMaximum(), self.region.yMinimum(),
self.region.xMinimum(), self.region.xMaximum()
self.region.xMaximum(), self.region.xMinimum()
)
# TODO Handle cell size
#if not self.cellSize:
Expand Down Expand Up @@ -625,7 +619,7 @@ def processOutputs(self, parameters, context):
self.commands.append(command)
self.outputCommands.append(command)
else:
command += ' {} output="{}"'.format(uniqName, fileName)
command += '{} output="{}"'.format(uniqName, fileName)
self.commands.append(command)
self.outputCommands.append(command)

Expand Down Expand Up @@ -712,6 +706,10 @@ def setSessionProjectionFromLayer(self, layer):
Grass7Utils.projectionSet = True

def exportRasterLayer(self, layerKey, layerSrc):
"""
Creates a dedicated command to load a raster into
temporary GRASS DB.
"""
# TODO: handle multiple bands
destFilename = 'a' + os.path.basename(self.getTempFilename())
self.exportedLayers[layerKey] = destFilename
Expand Down
17 changes: 15 additions & 2 deletions python/plugins/processing/algs/grass7/Grass7AlgorithmProvider.py
Expand Up @@ -30,13 +30,15 @@
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsApplication,
QgsProcessingProvider,
QgsVectorFileWriter,
QgsMessageLog,
QgsProcessingUtils)
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.core.ProcessingConfig import (ProcessingConfig, Setting)
from .Grass7Utils import Grass7Utils
from .Grass7Algorithm import Grass7Algorithm
from processing.tools.system import isWindows, isMac
#from .nviz7 import nviz7
from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
Expand Down Expand Up @@ -124,7 +126,18 @@ def svgIconPath(self):
return QgsApplication.iconPath("providerGrass.svg")

def supportedOutputVectorLayerExtensions(self):
return ['shp']
# We use the same extensions than QGIS because:
# - QGIS is using OGR like GRASS
# - There are very chances than OGR version used in GRASS is
# different from QGIS OGR version.
return QgsVectorFileWriter.supportedFormatExtensions()

def supportedOutputRasterLayerExtensions(self):
# We use the same extensions than GDAL because:
# - GRASS is also using GDAL for raster imports.
# - Chances that GRASS is compiled with another version of
# GDAL than QGIS are very limited!
return GdalUtils.getSupportedRasterExtensions()

def canBeActivated(self):
return not bool(Grass7Utils.checkGrass7IsInstalled())
Expand Down

0 comments on commit 9a52e34

Please sign in to comment.