Merge pull request #1843 from radosuav/processing_sublayers

[Processing] Add support for input rasters with sublayers.
This commit is contained in:
volaya 2015-01-21 12:53:58 +01:00
commit c1f60e3876
5 changed files with 74 additions and 11 deletions

View File

@ -472,11 +472,14 @@ class GrassAlgorithm(GeoAlgorithm):
def exportRasterLayer(self, layer):
destFilename = self.getTempFilename()
self.exportedLayers[layer] = destFilename
command = 'r.external'
if bool(re.match('netcdf', layer, re.I)) or bool(re.match('hdf', layer, re.I)):
command = 'r.in.gdal'
else:
command = 'r.external -r'
command += ' input="' + layer + '"'
command += ' band=1'
command += ' output=' + destFilename
command += ' --overwrite -o -r'
command += ' --overwrite -o'
return command
def getTempFilename(self):

View File

@ -32,7 +32,6 @@ from qgis.core import *
from processing.tools.system import *
from processing.tools import dataobjects
def getParameterFromString(s):
tokens = s.split("|")
params = [t if unicode(t) != "None" else None for t in tokens[1:]]
@ -463,8 +462,9 @@ class ParameterRange(Parameter):
class ParameterRaster(ParameterDataObject):
def __init__(self, name='', description='', optional=False):
def __init__(self, name='', description='', optional=False, showSublayersDialog = True):
ParameterDataObject.__init__(self, name, description)
self.showSublayersDialog = parseBool(showSublayersDialog)
self.optional = parseBool(optional)
self.value = None
self.exported = None
@ -515,7 +515,12 @@ class ParameterRaster(ParameterDataObject):
if layer.name() == self.value:
self.value = unicode(layer.dataProvider().dataSourceUri())
return True
return os.path.exists(self.value)
if os.path.exists(self.value) or QgsRasterLayer(self.value).isValid():
return True
else:
# Layer could not be found
return False
def getFileFilter(self):
exts = dataobjects.getSupportedOutputRasterLayerExtensions()

View File

@ -127,13 +127,13 @@ class BatchInputSelectionPanel(QWidget):
self.tr('All files(*.*);;') + self.param.getFileFilter())
if ret:
files = list(ret)
if len(files) == 1:
settings.setValue('/Processing/LastInputPath',
settings.setValue('/Processing/LastInputPath',
os.path.dirname(unicode(files[0])))
for i, filename in enumerate(files):
files[i] = dataobjects.getRasterSublayer(filename, self.param)
if len(files) == 1:
self.text.setText(files[0])
else:
settings.setValue('/Processing/LastInputPath',
os.path.dirname(unicode(files[0])))
if isinstance(self.param, ParameterMultipleInput):
self.text.setText(';'.join(unicode(f) for f in files))
else:

View File

@ -29,6 +29,7 @@ import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from processing.tools import dataobjects
from processing.ui.ui_widgetLayerSelector import Ui_Form
@ -65,10 +66,12 @@ class InputLayerSelectorPanel(QWidget, Ui_Form):
filename = QFileDialog.getOpenFileName(self, self.tr('Select file'),
path, self.tr('All files (*.*);;') + self.param.getFileFilter())
if filename:
self.cmbText.addItem(filename, filename)
self.cmbText.setCurrentIndex(self.cmbText.count() - 1)
settings.setValue('/Processing/LastInputPath',
os.path.dirname(unicode(filename)))
filename = dataobjects.getRasterSublayer(filename, self.param)
self.cmbText.addItem(filename, filename)
self.cmbText.setCurrentIndex(self.cmbText.count() - 1)
def getValue(self):
return self.cmbText.itemData(self.cmbText.currentIndex())

View File

@ -26,7 +26,9 @@ __copyright__ = '(C) 2012, Victor Olaya'
__revision__ = '$Format:%H$'
from os import path
import re
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface
@ -373,3 +375,53 @@ def exportTable(table):
return filename[:-3] + 'dbf'
else:
return filename
def getRasterSublayer(path, param):
layer = QgsRasterLayer(path)
try:
# If the layer is a raster layer and has multiple sublayers, let the user chose one.
# # Based on QgisApp::askUserForGDALSublayers
if layer and param.showSublayersDialog and layer.dataProvider().name() == "gdal" and len(layer.subLayers()) > 1:
layers = []
subLayerNum = 0
# simplify raster sublayer name
for subLayer in layer.subLayers():
# if netcdf/hdf use all text after filename
if bool(re.match('netcdf', subLayer, re.I)) or bool(re.match('hdf', subLayer, re.I)):
subLayer = subLayer.split(path)[1]
subLayer = subLayer[1:]
else:
# remove driver name and file name
subLayer.replace(subLayer.split(":")[0], "")
subLayer.replace(path, "")
# remove any : or " left over
if subLayer.startswith(":"):
subLayer = subLayer[1:]
if subLayer.startswith("\""):
subLayer = subLayer[1:]
if subLayer.endswith(":"):
subLayer = subLayer[:-1]
if subLayer.endswith("\""):
subLayer = subLayer[:-1]
layers.append(str(subLayerNum)+"|"+subLayer)
subLayerNum = subLayerNum + 1
# Use QgsSublayersDialog
# Would be good if QgsSublayersDialog had an option to allow only one sublayer to be selected
chooseSublayersDialog = QgsSublayersDialog(QgsSublayersDialog.Gdal, "gdal")
chooseSublayersDialog.populateLayerTable( layers, "|" )
if chooseSublayersDialog.exec_():
return layer.subLayers()[chooseSublayersDialog.selectionIndexes()[0]]
else:
# If user pressed cancel then just return the input path
return path
else:
# If the sublayers selection dialog is not to be shown then just return the input path
return path
except:
# If the layer is not a raster layer, then just return the input path
return path