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

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


I think you're right Larry. I'd like to avoid files, though.

Regards,

Germán

2014-12-09 18:17 GMT-05:00 Larry Shaffer <larrys at dakotacarto.com>:

> HI,
>
> Another thing you might try is this command line option:
>
> [--code path] run the given python file on load
>
> Then, you could possibly load the PostGIS layer just as you would from the
> PyQGIS console, only it would be commands inside of a Python file.
>
> You could do other things as well, like verify the PostgreSQL server is
> up, etc.
>
> Please note: just guessing here, as I have not fully tested this yet.
>
> Regards,
>
> Larry Shaffer
> Dakota Cartography
> Black Hills, South Dakota
>
> On Tue, Dec 9, 2014 at 1:39 PM, Richard Duivenvoorde <rdmailings at duif.net>
> wrote:
>
>> 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
>> _______________________________________________
>> Qgis-user mailing list
>> Qgis-user at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/qgis-user
>>
>
>


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

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/7bd2b259/attachment.html>


More information about the Qgis-user mailing list