Skip to content
Permalink
Browse files

Handle raster files wihtout extension as GTiff + fix some ext scripts

  • Loading branch information
Médéric RIBREUX
Médéric RIBREUX committed Dec 27, 2017
1 parent 1f8fbf3 commit 8b691e87aa5dc488d2fe69a1c6554a280e0a2100
@@ -540,9 +540,10 @@ def getRasterFormatFromFilename(filename):
"""
ext = os.path.splitext(filename)[1].lower()
ext = ext.lstrip('.')
supported = GdalUtils.getSupportedRasters()
for name in list(supported.keys()):
exts = supported[name]
if ext in exts:
return name
if ext:
supported = GdalUtils.getSupportedRasters()
for name in list(supported.keys()):
exts = supported[name]
if ext in exts:
return name
return 'GTiff'
@@ -25,56 +25,30 @@

__revision__ = '$Format:%H$'

from processing.algs.grass7.Grass7Utils import Grass7Utils

def processInputs(alg):
def processInputs(alg, parameters, context):
# We need to import all the bands and to preserve color table
raster = alg.getParameterValue('map')
if raster in list(alg.exportedLayers.keys()):
if 'input' in alg.exportedLayers:
return

alg.setSessionProjectionFromLayer(raster, alg.commands)
destFilename = alg.getTempFilename()
alg.exportedLayers[raster] = destFilename
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
alg.commands.append(command)
# We need to import all the bands and color tables of the input raster
alg.loadRasterLayerFromParameter('map', parameters, context, False, None)
alg.postInputs()

alg.setSessionProjectionFromProject(alg.commands)

region = str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(',')
command = 'g.region'
command += ' -a'
command += ' n=' + str(regionCoords[3])
command += ' s=' + str(regionCoords[2])
command += ' e=' + str(regionCoords[1])
command += ' w=' + str(regionCoords[0])
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
alg.commands.append(command)


def processCommand(alg, parameters):
def processCommand(alg, parameters, context):
# We need to remove output
output = alg.getOutputFromName('output')
alg.exportedLayers[output.value] = output.name + alg.uniqueSuffix
alg.removeOutputFromName('output')
alg.processCommand()
alg.addOutput(output)
alg.processCommand(parameters, context, True)


def processOutputs(alg):
# We need to export the raster with all its bands and its color table
output = alg.getOutputValue('output')
raster = alg.getParameterFromName('map')
def processOutputs(alg, parameters, context):
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)

# Get the list of rasters matching the basename
command = "r.out.gdal -t input={} output=\"{}\" createopt=\"TFW=YES,COMPRESS=LZW\"".format(
alg.exportedLayers[raster.value], output)
alg.commands.append(command)
alg.outputCommands.append(command)
# We need to export the raster with all its bands and its color table
fileName = alg.parameterAsOutputLayer(parameters, 'output', context)
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
grassName = alg.exportedLayers['map']
alg.exportRasterLayer(grassName, fileName, True,
outFormat, createOpt, metaOpt)

This file was deleted.

@@ -25,12 +25,20 @@

__revision__ = '$Format:%H$'

from . import r_mask


def processCommand(alg, parameters, context):
r_mask.processCommand(alg, parameters, context)
# Remove input
alg.removeParameter('input')
alg.processCommand(parameters, context, True)


def processOutputs(alg, parameters, context):
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)

def processOutputs(alg):
r_mask.processOutputs(alg, parameters, context)
# We need to export the raster with all its bands and its color table
fileName = alg.parameterAsOutputLayer(parameters, 'output', context)
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
grassName = alg.exportedLayers['input']
alg.exportRasterLayer(grassName, fileName, True,
outFormat, createOpt, metaOpt)
@@ -25,12 +25,19 @@

__revision__ = '$Format:%H$'

from . import r_mask


def processCommand(alg, parameters, context):
r_mask.processCommand(alg, parameters, context)
# Remove input
alg.removeParameter('input')
alg.processCommand(parameters, context, True)


def processOutputs(alg, parameters, context):
r_mask.processOutputs(alg, parameters, context)
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)

# We need to export the raster with all its bands and its color table
fileName = alg.parameterAsOutputLayer(parameters, 'output', context)
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
grassName = alg.exportedLayers['input']
alg.exportRasterLayer(grassName, fileName, True,
outFormat, createOpt, metaOpt)
@@ -29,32 +29,26 @@
from processing.tools.system import getTempFilename


def checkParameterValuesBeforeExecuting(alg):
def checkParameterValuesBeforeExecuting(alg, parameters, context):
""" Verify if we have the right parameters """
if alg.getParameterValue(u'rules') and alg.getParameterValue(u'txtrules'):
if (alg.parameterAsString(parameters, 'rules', context)
and alg.parameterAsString(parameters, 'txtrules', context)):
return alg.tr("You need to set either a rules file or write directly the rules!")

return None


def processCommand(alg, parameters):
def processCommand(alg, parameters, context):
""" Handle inline rules """
txtRules = alg.getParameterValue(u'txtrules')
txtRules = alg.parameterAsString(parameters, 'txtrules', context)
if txtRules:
# Creates a temporary txt file
tempRulesName = getTempFilename()

# Inject rules into temporary txt file
with open(tempRulesName, "w") as tempRules:
tempRules.write(txtRules)
alg.removeParameter('txtrules')
parameters['rules'] = tempRulesName

raster = alg.getParameterValue(u'input')
output = alg.getOutputFromName(u'output')
alg.exportedLayers[output.value] = output.name + alg.uniqueSuffix
if raster:
raster = alg.exportedLayers[raster]
command = u"r.reclass input={} rules=- output={} --overwrite < {}".format(
raster, output.name + alg.uniqueSuffix, tempRulesName)
alg.commands.append(command)
else:
alg.processCommand()
alg.processCommand(parameters, context)
@@ -26,11 +26,11 @@
__revision__ = '$Format:%H$'


def checkParameterValuesBeforeExecuting(alg):
def checkParameterValuesBeforeExecuting(alg, parameters, context):
""" Verify if we have the right parameters """
radius = alg.getParameterValue(u'radius')
x_radius = alg.getParameterValue(u'x_radius')
y_radius = alg.getParameterValue(u'y_radius')
radius = alg.parameterAsString(parameters, 'radius', context)
x_radius = alg.parameterAsString(parameters, 'x_radius', context)
y_radius = alg.parameterAsString(parameters, 'y_radius', context)

if (not radius and not x_radius and not y_radius) or (radius and (x_radius or y_radius)):
return alg.tr("You need to set either radius or x_radius and y_radius!")
@@ -26,55 +26,13 @@
__revision__ = '$Format:%H$'


def checkParameterValuesBeforeExecuting(alg):
""" Verify if we have the right parameters """
if alg.getParameterValue('brighten') and alg.getParameterValue('bgcolor'):
return alg.tr("You need to set either a brighten percentage or a NULL color!")
return None


def processInputs(alg):
def processInputs(alg, parameters, context):
# We need to import all the bands and color tables of the input rasters
shade = alg.getParameterValue('shade')
color = alg.getParameterValue('color')
if color in list(alg.exportedLayers.keys()):
return

for raster, method in (shade, 'r.external'), (color, 'r.in.gdal'):
alg.setSessionProjectionFromLayer(raster, alg.commands)

destFilename = alg.getTempFilename()
alg.exportedLayers[raster] = destFilename
command = '{} input={} output={} --overwrite -o'.format(method, raster, destFilename)
alg.commands.append(command)

alg.setSessionProjectionFromProject(alg.commands)

region = str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(',')
command = 'g.region'
command += ' -a'
command += ' n=' + str(regionCoords[3])
command += ' s=' + str(regionCoords[2])
command += ' e=' + str(regionCoords[1])
command += ' w=' + str(regionCoords[0])
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
if cellsize:
command += ' res=' + str(cellsize)
else:
command += ' res=' + str(alg.getDefaultCellsize(parameters, context))
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
alg.commands.append(command)


def processOutputs(alg):
# Keep color table ?
output = alg.getOutputValue(u'output')
command = u"r.out.gdal -t createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format(
alg.exportedLayers[output],
output
)
alg.commands.append(command)
alg.outputCommands.append(command)
alg.loadRasterLayerFromParameter('shade', parameters, context,
False, None)
alg.loadRasterLayerFromParameter('color', parameters, context,
False, None)

def processOutputs(alg, parameters, context):
# Keep color table
alg.exportRasterLayerFromParameter('output', parameters, context, True)
@@ -26,7 +26,7 @@
__revision__ = '$Format:%H$'

from qgis.core import QgsProcessingParameterString

from processing.algs.grass7.Grass7Utils import Grass7Utils

def processCommand(alg, parameters, context):
# We had a new "output" parameter
@@ -45,8 +45,13 @@ def processCommand(alg, parameters, context):


def processOutputs(alg, parameters, context):
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)

# Export the results from correctedoutput
grassName = 'correctedoutput{}'.format(alg.uniqueSuffix)
fileName = alg.parameterAsOutputLayer(
parameters, 'routput', context)
alg.exportRasterLayer(grassName, fileName)
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
alg.exportRasterLayer(grassName, fileName, True,
outFormat, createOpt, metaOpt)
@@ -25,7 +25,8 @@

__revision__ = '$Format:%H$'

from processing.core.parameters import getParameterFromString
from qgis.core import QgsProcessingParameterString
from processing.algs.grass7.Grass7Utils import Grass7Utils
import os


@@ -35,31 +36,26 @@ def processCommand(alg, parameters, context):
outputs = []
for i in range(0, int(quantiles)):
outputs.append('output_{}'.format(i))
param = QgsProcessingParameterString(
'output', 'virtual output',
','.join(outputs), False, False)
alg.addParameter(param)

output = getParameterFromString('ParameterString|output|Output Rasters|None|False|True')
output.value = ','.join(outputs)
alg.addParameter(output)

output_dir = alg.getOutputFromName('output_dir')
alg.removeOutputFromName('output_dir')

# Launch the algorithm
alg.processCommand(parameters, context)

# We re-add the previous output
alg.addOutput(output_dir)
# Removes outputs
alg.processCommand(parameters, context, True)


def processOutputs(alg, parameters, context):
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
outputDir = alg.parameterAsString(parameters, 'output_dir', context)
outputParam = alg.parameterAsString(parameters, 'output', context)
outputs = outputParam.split(',')

# We need to export each of the output
output_dir = alg.getOutputValue('output_dir')
outputParam = alg.getParameterFromName('output')
outputs = outputParam.value.split(',')
alg.parameters.remove(outputParam)
for output in outputs:
command = u"r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format(
output,
os.path.join(output_dir, output + '.tif')
)
alg.commands.append(command)
alg.outputCommands.append(command)
fileName = os.path.join(outputDir, output)
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
alg.exportRasterLayer(output, fileName, True,
outFormat, createOpt, metaOpt)

0 comments on commit 8b691e8

Please sign in to comment.
You can’t perform that action at this time.