Skip to content
Permalink
Browse files

Cleanup + pep8

  • Loading branch information
anitagraser committed May 28, 2018
1 parent dd811a2 commit f3146537a2db076fbdf3d5b9545e67b86bfdce2f
Showing with 37 additions and 24 deletions.
  1. +37 −24 python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
@@ -27,7 +27,17 @@

__revision__ = '$Format:%H$'

from qgis.core import (Qgis, QgsProcessingException, QgsProcessingParameterString, QgsApplication, QgsVectorLayer, QgsProject, QgsProcessingParameterFeatureSink, QgsProcessing, QgsFeatureRequest, QgsFeature, QgsFeatureSink, QgsProcessingUtils, QgsProcessingException, QgsProcessingOutputVectorLayer, QgsProcessingContext, QgsProcessingFeedback)
from qgis.core import (Qgis,
QgsProcessingException,
QgsProcessingParameterString,
QgsApplication,
QgsVectorLayer,
QgsProject,
QgsProcessing,
QgsProcessingException,
QgsProcessingOutputVectorLayer,
QgsProcessingContext,
QgsProcessingFeedback)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import postgis

@@ -55,10 +65,14 @@ def initAlgorithm(self, config=None):
'widget_wrapper': {
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
self.addParameter(db_param)
self.addParameter(QgsProcessingParameterString(self.SQL, self.tr('SQL query (must return unique id and geom field)'), multiLine=True))

self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT,self.tr("Output layer"),QgsProcessing.TypeVectorAnyGeometry))

self.addParameter(QgsProcessingParameterString(
self.SQL,
self.tr('SQL query (must return unique id and geom field)'),
multiLine=True))
self.addOutput(QgsProcessingOutputVectorLayer(
self.OUTPUT,
self.tr("Output layer"),
QgsProcessing.TypeVectorAnyGeometry))

def name(self):
return 'postgisexecuteandloadsql'
@@ -69,23 +83,22 @@ def displayName(self):
def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsString(parameters, self.DATABASE, context)
uri = postgis.uri_from_name(connection)
sql = self.parameterAsString(parameters, self.SQL, context).replace('\n', ' ')
QgsApplication.messageLog().logMessage(str(sql), level=Qgis.Info)
uri.setDataSource("","("+sql+")", "geom", "","id")
sql = self.parameterAsString(parameters, self.SQL, context)
sql = sql.replace('\n', ' ')
uri.setDataSource("", "("+sql+")", "geom", "", "id")

vlayer = QgsVectorLayer(uri.uri(), "layername", "postgres")

if vlayer is None:
raise QgsProcessingException(self.tr("Got None instead of vector layer object."))
return {}
else:
QgsApplication.messageLog().logMessage("Valid layer: "+str(vlayer.isValid()), level=Qgis.Info)

if not vlayer.isValid():
raise QgsProcessingException(self.tr("This layer is invalid! Please check the PostGIS log for error messages."))
return {}

context.temporaryLayerStore().addMapLayer(vlayer)
context.addLayerToLoadOnCompletion( vlayer.id(), QgsProcessingContext.LayerDetails( 'SQL layer', context.project(), self.OUTPUT ) )

return { self.OUTPUT: vlayer.id() }

if not vlayer.isValid():
raise QgsProcessingException(self.tr("""This layer is invalid!
Please check the PostGIS log for error messages."""))

context.temporaryLayerStore().addMapLayer(vlayer)
context.addLayerToLoadOnCompletion(
vlayer.id(),
QgsProcessingContext.LayerDetails('SQL layer',
context.project(),
self.OUTPUT))

return {self.OUTPUT: vlayer.id()}

0 comments on commit f314653

Please sign in to comment.
You can’t perform that action at this time.