[Qgis-user] PyQGIS to add PostGIS raster layer?

Alessandro Pasotti apasotti at gmail.com
Thu Apr 30 00:58:00 PDT 2020


On Thu, Apr 30, 2020 at 9:46 AM Yann Voté <ygversil at lilo.org> wrote:
>
> Hi,
>
> Something like this should do.
>
> pg_layer = QgsVectorLayer(
>      "host=db.priv.example.org port=5432 authcfg=expauth srid=4326 "
>      "dbname=exampledb table='schema'.'table_name' (geometry) "
>      "type=MULTIPOLYGON sql=",
>      baseName='table_name',
>      providerLib='postgres',
> )
> QgsProject.instance().addMapLayer(pg_layer)
>
> Of course, adapt with your values. In particular, authcfg is the 7-chars
> identifier of the auth configuration that contains your login and
> password (or cert) to authenticate to PostgreSQL.
>
> See https://qgis.org/pyqgis/master/core/QgsVectorLayer.html

This won't work with rasters, you need to create a QgsRasterLayer, not
a vector one.

Coming to PG rasters, in QGIS 3.12 there are two providers that
support PG rasters:
- GDAL data provider
- native "postgresraster" data provider

GDAL data source uri uses the well known good old syntax to connect to
the raster (you can also test the connection from the command line
with "gdalinfo ....").

The "postgresraster" provider is totally new in 3.12 and you can have
a look to the tests for PyQGIS use cases:
https://github.com/qgis/QGIS/blob/master/tests/src/python/test_provider_postgresraster.py#L82

Hope this helps.


>
> See also
> https://qgis.org/pyqgis/master/core/QgsDataSourceUri.html#qgis.core.QgsDataSourceUri
> for more options to build database connection strings.
>
> Regards.
>
> Yann
>
>
> Le 30/04/2020 à 00:24, thayeray a écrit :
> > Is it possible to add a PostGIS raster layer to a project using PyQGIS?
> > The PyQGIS Developer Cookbook mentions adding rasters from files,
> > geopackages and WCS services.
> > If I try using a QgsDataSourceUri by feeding it a raster column instead of a
> > geometry column, the PostGIS error that is generated shows that is not going
> > to work because raster is not a geometry:
> >
> > WARNING    Erroneous query: SELECT 0, array_agg(DISTINCT
> > st_srid("rast")::text || ':' || UPPER(geometrytype("rast"))) FROM
> > "public"."indicators__g_economic_rebal_r_62ft" returned 7 [ERROR: function
> > geometrytype(raster) does not exist
> >
> > Any ideas of what I can do? Do I just have to give up on PostGIS rasters and
> > just turn it into WCS on the server?
> >
> >
> >
> >
> > --
> > Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html
> > _______________________________________________
> > Qgis-user mailing list
> > Qgis-user at lists.osgeo.org
> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> >
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user



-- 
Alessandro Pasotti
w3:   www.itopen.it


More information about the Qgis-user mailing list