27
27
28
28
__revision__ = '$Format:%H$'
29
29
30
- from qgis .core import (Qgis , QgsProcessingException , QgsProcessingParameterString , QgsApplication , QgsVectorLayer , QgsProject , QgsProcessingParameterFeatureSink , QgsProcessing , QgsFeatureRequest , QgsFeature , QgsFeatureSink , QgsProcessingUtils , QgsProcessingException , QgsProcessingOutputVectorLayer , QgsProcessingContext , QgsProcessingFeedback )
30
+ from qgis .core import (Qgis ,
31
+ QgsProcessingException ,
32
+ QgsProcessingParameterString ,
33
+ QgsApplication ,
34
+ QgsVectorLayer ,
35
+ QgsProject ,
36
+ QgsProcessing ,
37
+ QgsProcessingException ,
38
+ QgsProcessingOutputVectorLayer ,
39
+ QgsProcessingContext ,
40
+ QgsProcessingFeedback )
31
41
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
32
42
from processing .tools import postgis
33
43
@@ -55,10 +65,14 @@ def initAlgorithm(self, config=None):
55
65
'widget_wrapper' : {
56
66
'class' : 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper' }})
57
67
self .addParameter (db_param )
58
- self .addParameter (QgsProcessingParameterString (self .SQL , self .tr ('SQL query (must return unique id and geom field)' ), multiLine = True ))
59
-
60
- self .addOutput (QgsProcessingOutputVectorLayer (self .OUTPUT ,self .tr ("Output layer" ),QgsProcessing .TypeVectorAnyGeometry ))
61
-
68
+ self .addParameter (QgsProcessingParameterString (
69
+ self .SQL ,
70
+ self .tr ('SQL query (must return unique id and geom field)' ),
71
+ multiLine = True ))
72
+ self .addOutput (QgsProcessingOutputVectorLayer (
73
+ self .OUTPUT ,
74
+ self .tr ("Output layer" ),
75
+ QgsProcessing .TypeVectorAnyGeometry ))
62
76
63
77
def name (self ):
64
78
return 'postgisexecuteandloadsql'
@@ -69,23 +83,22 @@ def displayName(self):
69
83
def processAlgorithm (self , parameters , context , feedback ):
70
84
connection = self .parameterAsString (parameters , self .DATABASE , context )
71
85
uri = postgis .uri_from_name (connection )
72
- sql = self .parameterAsString (parameters , self .SQL , context ). replace ( ' \n ' , ' ' )
73
- QgsApplication . messageLog (). logMessage ( str ( sql ), level = Qgis . Info )
74
- uri .setDataSource ("" ,"(" + sql + ")" , "geom" , "" ,"id" )
75
-
86
+ sql = self .parameterAsString (parameters , self .SQL , context )
87
+ sql = sql . replace ( ' \n ' , ' ' )
88
+ uri .setDataSource ("" , "(" + sql + ")" , "geom" , "" , "id" )
89
+
76
90
vlayer = QgsVectorLayer (uri .uri (), "layername" , "postgres" )
77
-
78
- if vlayer is None :
79
- raise QgsProcessingException (self .tr ("Got None instead of vector layer object." ))
80
- return {}
81
- else :
82
- QgsApplication .messageLog ().logMessage ("Valid layer: " + str (vlayer .isValid ()), level = Qgis .Info )
83
-
84
- if not vlayer .isValid ():
85
- raise QgsProcessingException (self .tr ("This layer is invalid! Please check the PostGIS log for error messages." ))
86
- return {}
87
-
88
- context .temporaryLayerStore ().addMapLayer (vlayer )
89
- context .addLayerToLoadOnCompletion ( vlayer .id (), QgsProcessingContext .LayerDetails ( 'SQL layer' , context .project (), self .OUTPUT ) )
90
-
91
- return { self .OUTPUT : vlayer .id () }
91
+
92
+ if not vlayer .isValid ():
93
+ raise QgsProcessingException (self .tr ("""This layer is invalid!
94
+ Please check the PostGIS log for error messages.""" ))
95
+
96
+ context .temporaryLayerStore ().addMapLayer (vlayer )
97
+ context .addLayerToLoadOnCompletion (
98
+ vlayer .id (),
99
+ QgsProcessingContext .LayerDetails ('SQL layer' ,
100
+ context .project (),
101
+ self .OUTPUT ))
102
+
103
+ return {self .OUTPUT : vlayer .id ()}
104
+
0 commit comments