@@ -89,6 +89,8 @@ class Grass7Algorithm(QgsProcessingAlgorithm):
89
89
GRASS_REGION_ALIGN_TO_RESOLUTION = 'GRASS_REGION_ALIGN_TO_RESOLUTION'
90
90
GRASS_RASTER_FORMAT_OPT = 'GRASS_RASTER_FORMAT_OPT'
91
91
GRASS_RASTER_FORMAT_META = 'GRASS_RASTER_FORMAT_META'
92
+ GRASS_VECTOR_DSCO = 'GRASS_VECTOR_DSCO'
93
+ GRASS_VECTOR_LCO = 'GRASS_VECTOR_LCO'
92
94
93
95
OUTPUT_TYPES = ['auto' , 'point' , 'line' , 'area' ]
94
96
QGIS_OUTPUT_TYPES = {QgsProcessing .TypeVectorAnyGeometry : 'auto' ,
@@ -287,12 +289,31 @@ def defineCharacteristicsFromFile(self):
287
289
self .params .append (param )
288
290
289
291
if vectorOutputs :
292
+ # Add an optional output type
290
293
param = QgsProcessingParameterEnum (self .GRASS_OUTPUT_TYPE_PARAMETER ,
291
294
self .tr ('v.out.ogr output type' ),
292
295
self .OUTPUT_TYPES )
293
296
param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
294
297
self .params .append (param )
295
298
299
+ # Add a DSCO parameter for format export
300
+ param = QgsProcessingParameterString (
301
+ self .GRASS_VECTOR_DSCO ,
302
+ self .tr ('v.out.ogr output data source options (dsco)' ),
303
+ multiLine = True , optional = True
304
+ )
305
+ param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
306
+ self .params .append (param )
307
+
308
+ # Add a LCO parameter for format export
309
+ param = QgsProcessingParameterString (
310
+ self .GRASS_VECTOR_LCO ,
311
+ self .tr ('v.out.ogr output layer options (lco)' ),
312
+ multiLine = True , optional = True
313
+ )
314
+ param .setFlags (param .flags () | QgsProcessingParameterDefinition .FlagAdvanced )
315
+ self .params .append (param )
316
+
296
317
def getDefaultCellSize (self ):
297
318
"""
298
319
Determine a default cell size from all the raster layers.
@@ -502,7 +523,9 @@ def processCommand(self, parameters, context, feedback, delOutputs=False):
502
523
self .GRASS_OUTPUT_TYPE_PARAMETER ,
503
524
self .GRASS_REGION_ALIGN_TO_RESOLUTION ,
504
525
self .GRASS_RASTER_FORMAT_OPT ,
505
- self .GRASS_RASTER_FORMAT_META ]:
526
+ self .GRASS_RASTER_FORMAT_META ,
527
+ self .GRASS_VECTOR_DSCO ,
528
+ self .GRASS_VECTOR_LCO ]:
506
529
continue
507
530
508
531
# Raster and vector layers
@@ -629,9 +652,6 @@ def vectorOutputType(self, parameters, context):
629
652
630
653
def processOutputs (self , parameters , context , feedback ):
631
654
"""Prepare the GRASS v.out.ogr commands"""
632
- # TODO: support multiple raster formats.
633
- # TODO: support multiple vector formats.
634
-
635
655
# Determine general vector output type
636
656
self .vectorOutputType (parameters , context )
637
657
@@ -820,16 +840,19 @@ def loadVectorLayer(self, name, layer, external=False):
820
840
destFilename )
821
841
self .commands .append (command )
822
842
823
- def exportVectorLayerFromParameter (self , name , parameters , context ):
843
+ def exportVectorLayerFromParameter (self , name , parameters , context , layer = None , nocats = False ):
824
844
"""
825
- Creates a dedicated command to export a raster from
826
- temporary GRASS DB into a file via gdal.
827
- :param grassName: name of the parameter
828
- :param fileName: file path of raster layer
829
- :param colorTable: preserve color Table.
845
+ Creates a dedicated command to export a vector from
846
+ a QgsProcessingParameter.
847
+ :param name: name of the parameter.
848
+ :param context: parameters context.
849
+ :param layer: for vector with multiples layers, exports only one layer.
850
+ :param nocats: do not export GRASS categories.
830
851
"""
831
852
fileName = os .path .normpath (
832
853
self .parameterAsOutputLayer (parameters , name , context ))
854
+ grassName = '{}{}' .format (name , self .uniqueSuffix )
855
+
833
856
# Find if there is a dataType
834
857
dataType = self .outType
835
858
if self .outType == 'auto' :
@@ -839,28 +862,34 @@ def exportVectorLayerFromParameter(self, name, parameters, context):
839
862
if layerType in self .QGIS_OUTPUT_TYPES :
840
863
dataType = self .QGIS_OUTPUT_TYPES [layerType ]
841
864
842
- grassName = '{}{}' .format (name , self .uniqueSuffix )
843
- self .exportVectorLayer (grassName , fileName , dataType )
865
+ outFormat = QgsVectorFileWriter .driverForExtension (os .path .splitext (fileName )[1 ]).replace (' ' , '_' )
866
+ dsco = self .parameterAsString (parameters , self .GRASS_VECTOR_DSCO , context )
867
+ lco = self .parameterAsString (parameters , self .GRASS_VECTOR_LCO , context )
868
+ self .exportVectorLayer (grassName , fileName , layer , nocats , dataType , outFormat , dsco , lco )
844
869
845
- def exportVectorLayer (self , grassName , fileName , dataType = 'auto' , layer = None , nocats = False ):
870
+ def exportVectorLayer (self , grassName , fileName , layer = None , nocats = False , dataType = 'auto' ,
871
+ outFormat = 'GPKG' , dsco = None , lco = None ):
846
872
"""
847
873
Creates a dedicated command to export a vector from
848
- temporary GRASS DB into a file via ogr.
849
- :param grassName: name of the parameter.
850
- :param fileName: file path of raster layer.
851
- :param dataType: GRASS data type for exporting data.
852
- :param layer: In GRASS a vector can have multiple layers.
853
- :param nocats: Also export features without category if True.
874
+ temporary GRASS DB into a file via OGR.
875
+ :param grassName: name of the vector to export.
876
+ :param fileName: file path of vector layer.
877
+ :param dataType: export only this type of data.
878
+ :param layer: for vector with multiples layers, exports only one layer.
879
+ :param nocats: do not export GRASS categories.
880
+ :param outFormat: file format for export.
881
+ :param dsco: datasource creation options for format.
882
+ :param lco: layer creation options for format.
854
883
"""
855
884
for cmd in [self .commands , self .outputCommands ]:
856
885
cmd .append (
857
- 'v.out.ogr{0} type={1} {2} input="{3}" output="{4}" {5}' .format (
858
- ' -c' if nocats else '' ,
859
- dataType ,
886
+ 'v.out.ogr{0} type="{1}" input="{2}" output="{3}" format="{4}" {5}{6}{7} --overwrite' .format (
887
+ '' if nocats else ' -c' ,
888
+ dataType , grassName , fileName ,
889
+ outFormat ,
860
890
'layer={}' .format (layer ) if layer else '' ,
861
- grassName ,
862
- fileName ,
863
- 'format=ESRI_Shapefile --overwrite'
891
+ ' dsco="{}"' .format (dsco ) if dsco else '' ,
892
+ ' lco="{}"' .format (lco ) if lco else ''
864
893
)
865
894
)
866
895
0 commit comments