Skip to content

Commit

Permalink
Add geometry and id field parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
anitagraser committed May 29, 2018
1 parent 1ed5bb5 commit 70afe2d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
Expand Up @@ -47,6 +47,8 @@ class PostGISExecuteAndLoadSQL(QgisAlgorithm):
DATABASE = 'DATABASE'
SQL = 'SQL'
OUTPUT = 'OUTPUT'
ID_FIELD = 'ID_FIELD'
GEOMETRY_FIELD = 'GEOMETRY_FIELD'

def group(self):
return self.tr('Database')
Expand All @@ -69,6 +71,15 @@ def initAlgorithm(self, config=None):
self.SQL,
self.tr('SQL query (must return unique id and geom field)'),
multiLine=True))
self.addParameter(QgsProcessingParameterString(
self.ID_FIELD,
self.tr('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"),
Expand All @@ -82,10 +93,12 @@ def displayName(self):

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", "", "id")
uri.setDataSource("", "(" + sql + ")", geom_field, "", id_field)

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

Expand Down

0 comments on commit 70afe2d

Please sign in to comment.