Skip to content

Commit

Permalink
Cleanup + pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
anitagraser authored May 28, 2018
1 parent dd811a2 commit f314653
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand All @@ -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.