Skip to content

Commit

Permalink
Merge pull request #7064 from anitagraser/patch-1
Browse files Browse the repository at this point in the history
[FEATURE] Create PostGISExecuteAndLoadSQL.py
  • Loading branch information
m-kuhn committed Jun 25, 2018
2 parents fd8344b + 4a48f50 commit 7acfe03
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -356,6 +356,9 @@ qgis:spatialiteexecutesql: >
qgis:postgisexecutesql: >
This algorithm performs a SQL database query on a PostGIS database connected to QGIS.

qgis:postgisexecuteandloadsql: >
This algorithm performs a SQL database query on a PostGIS database connected to QGIS and loads the query results as a new layer.

qgis:randomextract: >
This algorithm takes a vector layer and generates a new one that contains only a subset of the features in the input layer.

Expand Down
117 changes: 117 additions & 0 deletions python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
@@ -0,0 +1,117 @@
# -*- 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,
QgsProcessing,
QgsProcessingException,
QgsProcessingOutputVectorLayer,
QgsProcessingContext,
QgsProcessingFeedback)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import postgis


class PostGISExecuteAndLoadSQL(QgisAlgorithm):

DATABASE = 'DATABASE'
SQL = 'SQL'
OUTPUT = 'OUTPUT'
ID_FIELD = 'ID_FIELD'
GEOMETRY_FIELD = 'GEOMETRY_FIELD'

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'),
multiLine=True))
self.addParameter(QgsProcessingParameterString(
self.ID_FIELD,
self.tr('Unique ID field name'),
defaultValue='id'))
self.addParameter(QgsProcessingParameterString(
self.GEOMETRY_FIELD,
self.tr('Geometry field name'),
defaultValue='geom',
optional=True))
self.addOutput(QgsProcessingOutputVectorLayer(
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)
id_field = self.parameterAsString(parameters, self.ID_FIELD, context)
geom_field = self.parameterAsString(
parameters, self.GEOMETRY_FIELD, context)
uri = postgis.uri_from_name(connection)
sql = self.parameterAsString(parameters, self.SQL, context)
sql = sql.replace('\n', ' ')
uri.setDataSource("", "(" + sql + ")", geom_field, "", id_field)

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

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()}
2 changes: 2 additions & 0 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -101,6 +101,7 @@
from .Polygonize import Polygonize
from .PolygonsToLines import PolygonsToLines
from .PostGISExecuteSQL import PostGISExecuteSQL
from .PostGISExecuteAndLoadSQL import PostGISExecuteAndLoadSQL
from .RandomExtract import RandomExtract
from .RandomExtractWithinSubsets import RandomExtractWithinSubsets
from .RandomPointsAlongLines import RandomPointsAlongLines
Expand Down Expand Up @@ -216,6 +217,7 @@ def getAlgs(self):
Polygonize(),
PolygonsToLines(),
PostGISExecuteSQL(),
PostGISExecuteAndLoadSQL(),
RandomExtract(),
RandomExtractWithinSubsets(),
RandomPointsAlongLines(),
Expand Down

0 comments on commit 7acfe03

Please sign in to comment.