I'm trying to build a processing script to automatize the loading of specific vector layers from postgis db. I wrote the following code lines which worked fine in python console :
tablename = "SUIVI_LH"
user = "avteam"
mdp = "password"
from qgis.core import QgsVectorLayer, QgsDataSourceUri
uri = QgsDataSourceUri()
# set host name, port, database name, username and password
uri.setConnection("172.16.0.116", "5432", "foresale", user, mdp)
# set database schema, table name and geometry column
uri.setDataSource ("infra", tablename, "geom")
# define a name for your layer (tablename by default) and "postgres" as data provider
vlayer=QgsVectorLayer(uri.uri(), tablename, "postgres")
if not vlayer.isValid:
print ('Couche non valide. Vérifiez la validité des paramètres de connexion du script')
else:
QgsProject.instance().addMapLayer(vlayer)
However I can't find the right method to make it a processing script. I tried those code lines from here : https://stackoverflow.com/questions/62510556/pyqgis-adding-layer-from-postgis-database-and-work-whith-it-in-qgis-interface
from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
from qgis.utils import *
def run_script(iface):
uri = QgsDataSourceUri()
uri.setConnection("172.16.0.116", "5432", "foresale",
"avteam", "password")
uri.setDataSource("infra", "LH", "geom")
layer = QgsVectorLayer(uri.uri(), "LH", "postgres")
if not layer.isValid():
print("Layer %s did not load" %layer.name())
QgsProject.instance().addMapLayer(layer)
run_script()
I also tried this solution for multiple vectors layers which is even better : Zoom on a PostGIS vector layer in a new project with PyQGIS?
from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
from qgis.utils import *
def loadPgLayer(instance, vl, path, name, data_filter=""):
"""load a postgis vector layer"""
vl.setDataSource(path, name, "postgres")
if vl.isValid() and vl.setSubsetString(data_filter):
instance.addMapLayer(vl)
canvas = iface.mapCanvas()
instance = QgsProject.instance()
uri = QgsDataSourceUri()
uri_params = dict(aHost="172.16.0.116",
aPort="5432",
aDatabase="foresale",
aUsername="avteam",
aPassword="password")
uri.setConnection(**uri_params)
vl_LH = QgsVectorLayer()
vl_SUIVI_LH = QgsVectorLayer()
name = "LH"
uri.setDataSource("infra", name, "geom")
path = uri.uri(False)
loadPgLayer(instance, vl_LH, path, name)
name = "SUIVI_LH"
uri.setDataSource("infra", name, "geom")
path = uri.uri(False)
loadPgLayer(instance, vl_SUIVI_LH, path, name)
canvas.refresh()
canvas.waitWhileRendering()
Both scripts run but do not produce outputs. Any ideas why ?
Log :
QGIS version: 3.16.6-Hannover
QGIS code revision: bfd36fddc9
Qt version: 5.11.2
GDAL version: 3.1.4
GEOS version: 3.8.1-CAPI-1.13.3
PROJ version: Rel. 6.3.2, May 1st, 2020
Processing algorithm…
Algorithm 'model' starting…
Input parameters:
{ }
Model processed OK. Executed 0 algorithms total in 0 s.
Execution completed in 0.02 seconds
Results:
{}
Loading resulting layers
Algorithm 'model' finished