Skip to content

Commit

Permalink
Create PostGISExecuteAndLoadSQL.py
Browse files Browse the repository at this point in the history
  • Loading branch information
anitagraser authored May 24, 2018
1 parent 12cb6fc commit d71643f
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PostGISExecuteAndLoadSQL.py
---------------------
Date : May 2018
Copyright : (C) 2018 by Anita Graser
Email : anitagraser at gmx dot at
---------------------
based on PostGISExecuteSQL.py by Victor Olaya and Carterix Geomatics
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Anita Graser'
__date__ = 'May 2018'
__copyright__ = '(C) 2018, Anita Graser'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

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


class PostGISExecuteAndLoadSQL(QgisAlgorithm):

DATABASE = 'DATABASE'
SQL = 'SQL'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Database')

def groupId(self):
return 'database'

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
db_param = QgsProcessingParameterString(
self.DATABASE,
self.tr('Database (connection name)'))
db_param.setMetadata({
'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.addParameter(QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr("Output layer"),
QgsProcessing.TypeVectorAnyGeometry))

def name(self):
return 'postgisexecuteandloadsql'

def displayName(self):
return self.tr('PostGIS execute and load SQL')

def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsString(parameters, self.DATABASE, context)
#db = postgis.GeoDB.from_name(connection)
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")
vlayer = QgsVectorLayer(uri.uri(), "layername", "postgres")
QgsApplication.messageLog().logMessage("Valid layer: "+str(vlayer.isValid()), level=Qgis.Info)
#QgsProject.instance().addMapLayer(vlayer)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
vlayer.fields(), vlayer.wkbType(), vlayer.sourceCrs())

features = vlayer.getFeatures(QgsFeatureRequest())
for feat in features:
out_feat = QgsFeature()
out_feat.setGeometry(feat.geometry())
out_feat.setAttributes(feat.attributes())
sink.addFeature(out_feat, QgsFeatureSink.FastInsert)

return {self.OUTPUT: dest_id}

0 comments on commit d71643f

Please sign in to comment.