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 )
30
+ from qgis .core import (Qgis , QgsProcessingException , QgsProcessingParameterString , QgsApplication , QgsVectorLayer , QgsProject , QgsProcessingParameterFeatureSink , QgsProcessing , QgsFeatureRequest , QgsFeature , QgsFeatureSink , QgsProcessingUtils , QgsProcessingException , QgsProcessingOutputVectorLayer , QgsProcessingContext , QgsProcessingFeedback )
31
31
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
32
32
from processing .tools import postgis
33
33
@@ -56,10 +56,9 @@ def initAlgorithm(self, config=None):
56
56
'class' : 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper' }})
57
57
self .addParameter (db_param )
58
58
self .addParameter (QgsProcessingParameterString (self .SQL , self .tr ('SQL query (must return unique id and geom field)' ), multiLine = True ))
59
- self .addParameter (QgsProcessingParameterFeatureSink (
60
- self .OUTPUT ,
61
- self .tr ("Output layer" ),
62
- QgsProcessing .TypeVectorAnyGeometry ))
59
+
60
+ self .addOutput (QgsProcessingOutputVectorLayer (self .OUTPUT ,self .tr ("Output layer" ),QgsProcessing .TypeVectorAnyGeometry ))
61
+
63
62
64
63
def name (self ):
65
64
return 'postgisexecuteandloadsql'
@@ -69,22 +68,24 @@ def displayName(self):
69
68
70
69
def processAlgorithm (self , parameters , context , feedback ):
71
70
connection = self .parameterAsString (parameters , self .DATABASE , context )
72
- #db = postgis.GeoDB.from_name(connection)
73
71
uri = postgis .uri_from_name (connection )
74
72
sql = self .parameterAsString (parameters , self .SQL , context ).replace ('\n ' , ' ' )
75
73
QgsApplication .messageLog ().logMessage (str (sql ), level = Qgis .Info )
76
74
uri .setDataSource ("" ,"(" + sql + ")" , "geom" , "" ,"id" )
75
+
77
76
vlayer = QgsVectorLayer (uri .uri (), "layername" , "postgres" )
78
- QgsApplication .messageLog ().logMessage ("Valid layer: " + str (vlayer .isValid ()), level = Qgis .Info )
79
- #QgsProject.instance().addMapLayer(vlayer)
80
- (sink , dest_id ) = self .parameterAsSink (parameters , self .OUTPUT , context ,
81
- vlayer .fields (), vlayer .wkbType (), vlayer .sourceCrs ())
82
-
83
- features = vlayer .getFeatures (QgsFeatureRequest ())
84
- for feat in features :
85
- out_feat = QgsFeature ()
86
- out_feat .setGeometry (feat .geometry ())
87
- out_feat .setAttributes (feat .attributes ())
88
- sink .addFeature (out_feat , QgsFeatureSink .FastInsert )
89
-
90
- return {self .OUTPUT : dest_id }
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 () }
0 commit comments