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

Germán Carrillo carrillo.german at gmail.com
Tue Dec 9 07:41:49 PST 2014


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.

>From the GNU/Linux terminal, when I enter "qgis --help" I get:

========
Usage: /usr/bin/qgis.bin [OPTION] [FILE]
  OPTION:
[--snapshot filename] emit snapshot of loaded datasets to given file
[--width width] width of snapshot to emit
[--height height] height of snapshot to emit
[--lang language] use language for interface text
[--project projectfile] load the given QGIS project
[--extent xmin,ymin,xmax,ymax] set initial map extent
[--nologo] hide splash screen
[--noplugins] don't restore plugins on startup
[--nocustomization] don't apply GUI customization
[--customizationfile] use the given ini file as GUI customization
[--optionspath path] use the given QSettings path
[--configpath path] use the given path for all user configuration
[--code path] run the given python file on load
[--defaultui] start by resetting user ui settings to default
[--help] this text

  FILE:
    Files specified on the command line can include rasters,
    vectors, and QGIS project files (.qgs):
     1. Rasters - supported formats include GeoTiff, DEM
        and others supported by GDAL
     2. Vectors - supported formats include ESRI Shapefiles
        and others supported by OGR and PostgreSQL layers using
        the PostGIS extension
========

The FILE section is what I'm (and Uggla was) asking about.
>From that help info, it seems to be possible to load PostGIS layers when
triggering the qgis command from the GNU/Linux command line.
However, I cannot get it to work properly, and as soon as QGIS starts, I
get a "Layer is not valid error."

Regards,

Germán




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

>
> Hi Germán,
>
> it took some time for me too to make it work :-)
>
> This one was easy, and working immediately:
>
> uri = QgsDataSourceURI()
> #                  host,       port,   dbase, user, password
> uri.setConnection("localhost", "5432", "bag", "geo", "xxx")
> #                 schema,      table,     geometry,  whereclause
> uri.setDataSource("bag8mrt2014", "provincie", "geovlak", "")
> #                                  layername, providername
> vlayer = QgsVectorLayer(uri.uri(), "test", "postgres")
> # just checking, should return True
> vlayer.isValid()
> # add it to mapcanvas
> QgsMapLayerRegistry.instance().addMapLayer(layer)
>
> but the one you want to use, with a uri string, took me some time to
> figure out. Best is to run QGIS in debug mode in terminal, to see if it
> is a query problem ( which you get when you do sql=''), or other problems.
>
> But for the above postgis layer, this is working as an uri example:
>
> l = iface.addVectorLayer('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=',
> "test", "postgres")
>
> 'problems' you face when you use this:
> - do NOT use quotes, EXCEPT for the schema/table value
> - the table value should be double quotes, so the full string is single
> - do NOT forget the (geovlak) <- geometry! part as it returns a valid
> layer then, but you do not see anything.
>
> above uri string is actually taken from the debug output of QGIS when
> you load this layer via QGIS itself, and then stripped from quotes.
>
> hope this helps!
>
> Regards,
>
> Richard Duivenvoorde
>
>
>
>
> On 09-12-14 00:59, Germán Carrillo wrote:
> > Hi all,
> >
> > could you please let me know of a working command that allows one to
> > load a PostGIS layer into QGIS at startup?
> >
> > I've tried with the information provided in the PyQGIS Cookbook [1] with
> > no success.
> >
> > Specifically, I tried with the uri.uri() string, something
> > like: "dbname='buscador' host=localhost port=4326 user='postgres'
> > password='postgres' table='public'.'parques_nacionales' (geom) sql="
> > But get a "Layer is not valid error."
> >
> > I'm using QGIS v.2.6 on GNU/Linux.
> >
> > Regards,
> >
> > Germán
> > --
> > [1]
> >
> http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html
> >
> > 2013-12-05 8:19 GMT-05:00 Richard Duivenvoorde <rdmailings at duif.net
> > <mailto:rdmailings at duif.net>>:
> >
> >     On 05-12-13 11:33, Uggla Henrik wrote:
> >     > Is there no documentation about this? The extension mentioned is
> Postgis itself, not a specific Qgis extension, right?
> >
> >     Off course there is documentation, did you not read it first ;-)
> >
> >
> http://www.qgis.org/en/docs/pyqgis_developer_cookbook/loadlayer.html#vector-layers
> >
> >     Regards,
> >
> >     Richard Duivenvoorde
> >
> >     _______________________________________________
> >     Qgis-user mailing list
> >     Qgis-user at lists.osgeo.org <mailto: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
>
>
-- 
-----------
   |\__
(:>__)(
   |/

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


More information about the Qgis-user mailing list