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

Germán Carrillo carrillo.german at gmail.com
Tue Dec 9 20:44:29 PST 2014


Thanks Richard for your time. Comments between lines.

2014-12-09 15:39 GMT-05:00 Richard Duivenvoorde <rdmailings at duif.net>:

> 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...
>
> Good catch!


> 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.
>

I sent the MySQL example to Martin :D
Do you see now why I was trying with the uri.uri()?


> 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 ??
>

Same here, QGIS prepends the user's folder.


>
> 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
>
>
Yes, actually my main requirement is to generate string connections
dynamically. To be honest, I don't think writing to a file is the best way
to achieve such task.


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

Thanks again!

Don't you think --help gives users misleading information? It says that you
can load PostGIS layers in the FILE parameter and, apparently, there is no
way of doing that.

The use case I have in mind is to select spatial tables from pgAdmin3 and
open them in QGIS.

Regards,

Germán

-- 
-----------
   |\__
(:>__)(
   |/

Soluciones Geoinformáticas Libres
http://geotux.tuxfamily.org/
http://twitter.com/GeoTux2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20141209/7ca6cb9c/attachment.html>


More information about the Qgis-user mailing list