mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
[processing] vector outputs can be saved in any GDAL-supported format
Thanks to Médéric RIBREUX
This commit is contained in:
parent
58e535103c
commit
cf599685cb
@ -89,6 +89,8 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
GRASS_REGION_ALIGN_TO_RESOLUTION = 'GRASS_REGION_ALIGN_TO_RESOLUTION'
|
GRASS_REGION_ALIGN_TO_RESOLUTION = 'GRASS_REGION_ALIGN_TO_RESOLUTION'
|
||||||
GRASS_RASTER_FORMAT_OPT = 'GRASS_RASTER_FORMAT_OPT'
|
GRASS_RASTER_FORMAT_OPT = 'GRASS_RASTER_FORMAT_OPT'
|
||||||
GRASS_RASTER_FORMAT_META = 'GRASS_RASTER_FORMAT_META'
|
GRASS_RASTER_FORMAT_META = 'GRASS_RASTER_FORMAT_META'
|
||||||
|
GRASS_VECTOR_DSCO = 'GRASS_VECTOR_DSCO'
|
||||||
|
GRASS_VECTOR_LCO = 'GRASS_VECTOR_LCO'
|
||||||
|
|
||||||
OUTPUT_TYPES = ['auto', 'point', 'line', 'area']
|
OUTPUT_TYPES = ['auto', 'point', 'line', 'area']
|
||||||
QGIS_OUTPUT_TYPES = {QgsProcessing.TypeVectorAnyGeometry: 'auto',
|
QGIS_OUTPUT_TYPES = {QgsProcessing.TypeVectorAnyGeometry: 'auto',
|
||||||
@ -287,12 +289,31 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
self.params.append(param)
|
self.params.append(param)
|
||||||
|
|
||||||
if vectorOutputs:
|
if vectorOutputs:
|
||||||
|
# Add an optional output type
|
||||||
param = QgsProcessingParameterEnum(self.GRASS_OUTPUT_TYPE_PARAMETER,
|
param = QgsProcessingParameterEnum(self.GRASS_OUTPUT_TYPE_PARAMETER,
|
||||||
self.tr('v.out.ogr output type'),
|
self.tr('v.out.ogr output type'),
|
||||||
self.OUTPUT_TYPES)
|
self.OUTPUT_TYPES)
|
||||||
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
||||||
self.params.append(param)
|
self.params.append(param)
|
||||||
|
|
||||||
|
# Add a DSCO parameter for format export
|
||||||
|
param = QgsProcessingParameterString(
|
||||||
|
self.GRASS_VECTOR_DSCO,
|
||||||
|
self.tr('v.out.ogr output data source options (dsco)'),
|
||||||
|
multiLine=True, optional=True
|
||||||
|
)
|
||||||
|
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
||||||
|
self.params.append(param)
|
||||||
|
|
||||||
|
# Add a LCO parameter for format export
|
||||||
|
param = QgsProcessingParameterString(
|
||||||
|
self.GRASS_VECTOR_LCO,
|
||||||
|
self.tr('v.out.ogr output layer options (lco)'),
|
||||||
|
multiLine=True, optional=True
|
||||||
|
)
|
||||||
|
param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
||||||
|
self.params.append(param)
|
||||||
|
|
||||||
def getDefaultCellSize(self):
|
def getDefaultCellSize(self):
|
||||||
"""
|
"""
|
||||||
Determine a default cell size from all the raster layers.
|
Determine a default cell size from all the raster layers.
|
||||||
@ -502,7 +523,9 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
self.GRASS_OUTPUT_TYPE_PARAMETER,
|
self.GRASS_OUTPUT_TYPE_PARAMETER,
|
||||||
self.GRASS_REGION_ALIGN_TO_RESOLUTION,
|
self.GRASS_REGION_ALIGN_TO_RESOLUTION,
|
||||||
self.GRASS_RASTER_FORMAT_OPT,
|
self.GRASS_RASTER_FORMAT_OPT,
|
||||||
self.GRASS_RASTER_FORMAT_META]:
|
self.GRASS_RASTER_FORMAT_META,
|
||||||
|
self.GRASS_VECTOR_DSCO,
|
||||||
|
self.GRASS_VECTOR_LCO]:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Raster and vector layers
|
# Raster and vector layers
|
||||||
@ -629,9 +652,6 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
|
|
||||||
def processOutputs(self, parameters, context, feedback):
|
def processOutputs(self, parameters, context, feedback):
|
||||||
"""Prepare the GRASS v.out.ogr commands"""
|
"""Prepare the GRASS v.out.ogr commands"""
|
||||||
# TODO: support multiple raster formats.
|
|
||||||
# TODO: support multiple vector formats.
|
|
||||||
|
|
||||||
# Determine general vector output type
|
# Determine general vector output type
|
||||||
self.vectorOutputType(parameters, context)
|
self.vectorOutputType(parameters, context)
|
||||||
|
|
||||||
@ -820,16 +840,19 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
destFilename)
|
destFilename)
|
||||||
self.commands.append(command)
|
self.commands.append(command)
|
||||||
|
|
||||||
def exportVectorLayerFromParameter(self, name, parameters, context):
|
def exportVectorLayerFromParameter(self, name, parameters, context, layer=None, nocats=False):
|
||||||
"""
|
"""
|
||||||
Creates a dedicated command to export a raster from
|
Creates a dedicated command to export a vector from
|
||||||
temporary GRASS DB into a file via gdal.
|
a QgsProcessingParameter.
|
||||||
:param grassName: name of the parameter
|
:param name: name of the parameter.
|
||||||
:param fileName: file path of raster layer
|
:param context: parameters context.
|
||||||
:param colorTable: preserve color Table.
|
:param layer: for vector with multiples layers, exports only one layer.
|
||||||
|
:param nocats: do not export GRASS categories.
|
||||||
"""
|
"""
|
||||||
fileName = os.path.normpath(
|
fileName = os.path.normpath(
|
||||||
self.parameterAsOutputLayer(parameters, name, context))
|
self.parameterAsOutputLayer(parameters, name, context))
|
||||||
|
grassName = '{}{}'.format(name, self.uniqueSuffix)
|
||||||
|
|
||||||
# Find if there is a dataType
|
# Find if there is a dataType
|
||||||
dataType = self.outType
|
dataType = self.outType
|
||||||
if self.outType == 'auto':
|
if self.outType == 'auto':
|
||||||
@ -839,28 +862,34 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
|
|||||||
if layerType in self.QGIS_OUTPUT_TYPES:
|
if layerType in self.QGIS_OUTPUT_TYPES:
|
||||||
dataType = self.QGIS_OUTPUT_TYPES[layerType]
|
dataType = self.QGIS_OUTPUT_TYPES[layerType]
|
||||||
|
|
||||||
grassName = '{}{}'.format(name, self.uniqueSuffix)
|
outFormat = QgsVectorFileWriter.driverForExtension(os.path.splitext(fileName)[1])
|
||||||
self.exportVectorLayer(grassName, fileName, dataType)
|
dsco = self.parameterAsString(parameters, self.GRASS_VECTOR_DSCO, context)
|
||||||
|
lco = self.parameterAsString(parameters, self.GRASS_VECTOR_LCO, context)
|
||||||
|
self.exportVectorLayer(grassName, fileName, layer, nocats, dataType, outFormat, dsco, lco)
|
||||||
|
|
||||||
def exportVectorLayer(self, grassName, fileName, dataType='auto', layer=None, nocats=False):
|
def exportVectorLayer(self, grassName, fileName, layer=None, nocats=False, dataType='auto',
|
||||||
|
outFormat='ESRI_Shapefile', dsco=None, lco=None):
|
||||||
"""
|
"""
|
||||||
Creates a dedicated command to export a vector from
|
Creates a dedicated command to export a vector from
|
||||||
temporary GRASS DB into a file via ogr.
|
temporary GRASS DB into a file via OGR.
|
||||||
:param grassName: name of the parameter.
|
:param grassName: name of the vector to export.
|
||||||
:param fileName: file path of raster layer.
|
:param fileName: file path of vector layer.
|
||||||
:param dataType: GRASS data type for exporting data.
|
:param dataType: export only this type of data.
|
||||||
:param layer: In GRASS a vector can have multiple layers.
|
:param layer: for vector with multiples layers, exports only one layer.
|
||||||
:param nocats: Also export features without category if True.
|
:param nocats: do not export GRASS categories.
|
||||||
|
:param outFormat: file format for export.
|
||||||
|
:param dsco: datasource creation options for format.
|
||||||
|
:param lco: layer creation options for format.
|
||||||
"""
|
"""
|
||||||
for cmd in [self.commands, self.outputCommands]:
|
for cmd in [self.commands, self.outputCommands]:
|
||||||
cmd.append(
|
cmd.append(
|
||||||
'v.out.ogr{0} type={1} {2} input="{3}" output="{4}" {5}'.format(
|
'v.out.ogr{0} type="{1}" input="{2}" output="{3}" format="{4}" {5}{6}{7} --overwrite'.format(
|
||||||
' -c' if nocats else '',
|
'' if nocats else ' -c',
|
||||||
dataType,
|
dataType, grassName, fileName,
|
||||||
|
outFormat,
|
||||||
'layer={}'.format(layer) if layer else '',
|
'layer={}'.format(layer) if layer else '',
|
||||||
grassName,
|
' dsco="{}"'.format(dsco) if dsco else '',
|
||||||
fileName,
|
' lco="{}"'.format(lco) if lco else ''
|
||||||
'format=ESRI_Shapefile --overwrite'
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user