31
31
32
32
from osgeo import ogr
33
33
from qgis .core import (QgsProcessingFeedback ,
34
- QgsApplication )
35
- from processing .tools import dataobjects
34
+ QgsProcessingParameterMultipleLayers ,
35
+ QgsProcessingParameterBoolean ,
36
+ QgsProcessing ,
37
+ QgsProcessingParameterVectorDestination ,
38
+ QgsProcessingOutputString ,
39
+ QgsProcessingException )
36
40
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
37
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
38
- from processing .core .parameters import ParameterMultipleInput
39
- from processing .core .parameters import ParameterBoolean
40
- from processing .core .outputs import OutputFile
41
- from processing .core .outputs import OutputString
42
41
43
42
44
43
class Datasources2Vrt (QgisAlgorithm ):
45
- DATASOURCES = 'DATASOURCES '
44
+ INPUT = 'INPUT '
46
45
UNIONED = 'UNIONED'
47
46
48
- VRT_FILE = 'VRT_FILE '
47
+ OUTPUT = 'OUTPUT '
49
48
VRT_STRING = 'VRT_STRING'
50
49
51
50
def group (self ):
@@ -55,17 +54,32 @@ def __init__(self):
55
54
super ().__init__ ()
56
55
57
56
def initAlgorithm (self , config = None ):
58
- self .addParameter (ParameterMultipleInput (self .DATASOURCES ,
59
- self .tr ('Input datasources' ),
60
- dataobjects .TYPE_TABLE ))
61
- self .addParameter (ParameterBoolean (self .UNIONED ,
62
- self .tr ('Create "unioned" VRT' ),
63
- default = False ))
64
-
65
- self .addOutput (OutputFile (self .VRT_FILE ,
66
- self .tr ('Virtual vector' ), ext = 'vrt' ))
67
- self .addOutput (OutputString (self .VRT_STRING ,
68
- self .tr ('Virtual string' )))
57
+ self .addParameter (QgsProcessingParameterMultipleLayers (self .INPUT ,
58
+ self .tr ('Input datasources' ),
59
+ QgsProcessing .TypeTable ))
60
+ self .addParameter (QgsProcessingParameterBoolean (self .UNIONED ,
61
+ self .tr ('Create "unioned" VRT' ),
62
+ defaultValue = False ))
63
+
64
+ class ParameterVectorVrtDestination (QgsProcessingParameterVectorDestination ):
65
+
66
+ def __init__ (self , name , description ):
67
+ super ().__init__ (name , description )
68
+
69
+ def clone (self ):
70
+ copy = ParameterVectorVrtDestination (self .name (), self .description ())
71
+ return copy
72
+
73
+ def type (self ):
74
+ return 'vrt_vector_destination'
75
+
76
+ def defaultFileExtension (self ):
77
+ return 'vrt'
78
+
79
+ self .addParameter (ParameterVectorVrtDestination (self .OUTPUT ,
80
+ self .tr ('Virtual vector' )))
81
+ self .addOutput (QgsProcessingOutputString (self .VRT_STRING ,
82
+ self .tr ('Virtual string' )))
69
83
70
84
def name (self ):
71
85
return 'buildvirtualvector'
@@ -74,20 +88,17 @@ def displayName(self):
74
88
return self .tr ('Build virtual vector' )
75
89
76
90
def processAlgorithm (self , parameters , context , feedback ):
77
- input_layers = self .getParameterValue (self .DATASOURCES )
78
- unioned = self .getParameterValue (self .UNIONED )
79
- vrtPath = self .getOutputValue (self .VRT_FILE )
80
-
81
- layers = input_layers .split (';' )
91
+ input_layers = self .parameterAsLayerList (parameters , self .INPUT , context )
92
+ unioned = self .parameterAsBool (parameters , self .UNIONED , context )
93
+ vrtPath = self .parameterAsOutputLayer (parameters , self .OUTPUT , context )
82
94
83
- vrtString = self .mergeDataSources2Vrt (layers ,
95
+ vrtString = self .mergeDataSources2Vrt (input_layers ,
84
96
vrtPath ,
85
97
union = unioned ,
86
98
relative = False ,
87
99
schema = False ,
88
100
feedback = feedback )
89
-
90
- self .setOutputValue (self .VRT_STRING , vrtString )
101
+ return {self .OUTPUT : vrtPath , self .VRT_STRING : vrtString }
91
102
92
103
def mergeDataSources2Vrt (self , dataSources , outFile , union = False , relative = False ,
93
104
schema = False , feedback = None ):
@@ -107,12 +118,16 @@ def mergeDataSources2Vrt(self, dataSources, outFile, union=False, relative=False
107
118
vrt += '<OGRVRTUnionLayer name="UnionedLayer">'
108
119
109
120
total = 100.0 / len (dataSources ) if dataSources else 1
110
- for current , inFile in enumerate (dataSources ):
121
+ for current , layer in enumerate (dataSources ):
122
+ if feedback .isCanceled ():
123
+ break
124
+
111
125
feedback .setProgress (int (current * total ))
112
126
127
+ inFile = layer .source ()
113
128
srcDS = ogr .Open (inFile , 0 )
114
129
if srcDS is None :
115
- raise GeoAlgorithmExecutionException (
130
+ raise QgsProcessingException (
116
131
self .tr ('Invalid datasource: {}' .format (inFile )))
117
132
118
133
if schema :
0 commit comments