3

I am trying to build a QGIS 3.8 model where the same inputs are used each time and only the clipping layer extent changes.

I can do this using shapefiles and editing the script to use the same input layer directly but now the job I have been tasked is to do the same thing, only using layers directly from a PostGIS database.

I have seen the following code from another question here which allows me to load PostGIS layers through the Python console but cannot get the layer to act as the input layer itself by changing the Default value in the processing script to the variable uri.

tablename2 = "Sewers_2015 - Outfall"
geometrycol = "geom"

from qgis.core import QgsVectorLayer, QgsDataSourceUri
uri = QgsDataSourceUri()
uri.setConnection("10.0.0.6", "5432", "Outfall", "David", "Pass")
uri.setDataSource ("Outfall", tablename, geometrycol)
vlayer=QgsVectorLayer (uri .uri(False), tablename, "postgres")
QgsProject.instance().addMapLayer(vlayer)

When adding this code to the top of my Clip script it only loads in the PostGIS layer to my workspace but doesnt come up as the default inpput to be clipped.

Here is my full script

user2= 'david.laverty'
tablename = "Sewers_2015 - Outfall"
geometrycol = "geom"
import os
from os import path
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsProject
import processing

from qgis.core import QgsVectorLayer, QgsDataSourceUri
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "Data", "David", "Pass")
uri.setDataSource ("Schema", tablename, geometrycol)
vlayer=QgsVectorLayer (uri .uri(False), tablename, "postgres")
QgsProject.instance().addMapLayer(vlayer)


class SaveToFileTest1(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('siteboundary', 'site boundary', types=[QgsProcessing.TypeVectorPolygon], defaultValue=None))
        self.addParameter(QgsProcessingParameterVectorLayer('vectors2016', 'Outfall_2016', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=uri))
        self.addParameter(QgsProcessingParameterFeatureSink('Clipped', 'clipped', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=os.path.join(r'C:\Users', user2, r'Desktop\pyGIS Testing\clip_test.shp')))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # Clip
        alg_params = {
            'INPUT': parameters['outfall2016'],
            'OVERLAY': parameters['siteboundary'],
            'OUTPUT': parameters['Clipped']
        }
        outputs['Clip'] = processing.run('native:clip', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Clipped'] = outputs['Clip']['OUTPUT']
        return results

    def name(self):
        return 'save to file test1'

    def displayName(self):
        return 'save to file test1'

    def group(self):
        return 'DL_testing'

    def groupId(self):
        return 'DL_testing'

    def createInstance(self):
        return SaveToFileTest1()

So my question is can a PostGIS layer be set as the Inpupt for a QGIS model using a Python script or am I attempting something that cannot be done as of yet in QGIS.

I'm running QGIS 3.8 on Windows 10 with a PostgreSQL 9.6 install running locally on my machine.

  • if you want to open the layer from Postgis into QGis, you do this by linking your Postgis database to your QGis project – Basile Aug 5 at 13:10
  • i would like to use a PostGIS layer from my DB as the hard linked input to my model. I do not wish to open the PostGIS layer in my project. I would just like the processed result to load when clipped using my model and boundary extent – David Laverty Aug 5 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.