[Qgis-user] How load Postgis layer from command line?

Richard Duivenvoorde rdmailings at duif.net
Tue Dec 9 12:39:01 PST 2014


On 09-12-14 16:41, Germán Carrillo wrote:
> Hi Richard, 
> 
> thanks a lot for your response. However, I think we aren't talking about
> the same :) 
> I understand this thread was opened to ask about how to open a PostGIS
> Layer from the OS (say GNU/Linux) command line at QGIS startup.

ah, sorry sorry :-(

well, I take this as a challenge...
(short version: go to the end, either use a vrt file or a qgs file)

looking into the code:

https://github.com/qgis/QGIS/blob/master/src/app/qgisapp.cpp#L4206

it looks like you can only use ogr provider for this...

here:

http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html


there is an example to use a MySQL uri... so I tried that one first.
All is based on ogr, so we need the ogr driver:

http://www.gdal.org/drv_pg.html

tried stuff like:

PG:'dbname=bag host=localhost port=5432 user=geo
password=xxx sslmode=disable key=gid estimatedmetadata=true srid=28992
type=MULTIPOLYGON table="bag8mrt2014"."provincie" (geovlak) sql='

but all that I got working was a simple:

ogrinfo -ro PG:'host=localhost user=geo password=gwwvgeo dbname=bag
sslmode=disable'

but using that kind of uri's did not make QGIS load, it kept showing:

Unable to load /tmp/PG:host=localhost user=geo password=xxx dbname=bag
sslmode=disable

apparently QGIS ALWAYS prepends the basepath (/tmp for me) in front of
the 'filename'. It has to be a real file ??

THEN: ok, so it has to be ogr AND a file...
There is gdal's Virtual Format:

http://www.gdal.org/drv_vrt.html

so with some trial and error I prepared the following vrt file, for
exact the same data as the earlier email:

<OGRVRTDataSource>

<OGRVRTLayer name="test">
<SrcDataSource>PG:host=localhost user=geo password=*** dbname=bag
sslmode=disable</SrcDataSource>
<SrcSQL>select * from bag8mrt2014.provincie</SrcSQL>
<LayerSRS>epsg:28992</LayerSRS>
</OGRVRTLayer>

</OGRVRTDataSource>

saved it as mypgconnection.vrt, and NOW I can load it with:

qgis mypgconnection.vrt  \o/

another option, is to create a qgs project file with that postgresql
connection and fire up qgis with that one

qgis mypgproject.qgs

you could maybe create the qgs file dynamically to have your
schema/table names in place

ok, it's all a little hacking, but hopefully of use :-)

Regards,

Richard Duivenvoorde



More information about the Qgis-user mailing list