<div dir="ltr">Hi Richard, <div><br></div><div>thanks a lot for your response. However, I think we aren't talking about the same :) </div><div style>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.</div><div style><br></div><div style>From the GNU/Linux terminal, when I enter "qgis --help" I get:</div><div style><br></div><div style>========</div><div style>Usage: /usr/bin/qgis.bin [OPTION] [FILE]<br></div><div style><div> OPTION:</div><div><span class="" style="white-space:pre"> </span>[--snapshot filename]<span class="" style="white-space:pre"> </span>emit snapshot of loaded datasets to given file</div><div><span class="" style="white-space:pre"> </span>[--width width]<span class="" style="white-space:pre"> </span>width of snapshot to emit</div><div><span class="" style="white-space:pre"> </span>[--height height]<span class="" style="white-space:pre"> </span>height of snapshot to emit</div><div><span class="" style="white-space:pre"> </span>[--lang language]<span class="" style="white-space:pre"> </span>use language for interface text</div><div><span class="" style="white-space:pre"> </span>[--project projectfile]<span class="" style="white-space:pre"> </span>load the given QGIS project</div><div><span class="" style="white-space:pre"> </span>[--extent xmin,ymin,xmax,ymax]<span class="" style="white-space:pre"> </span>set initial map extent</div><div><span class="" style="white-space:pre"> </span>[--nologo]<span class="" style="white-space:pre"> </span>hide splash screen</div><div><span class="" style="white-space:pre"> </span>[--noplugins]<span class="" style="white-space:pre"> </span>don't restore plugins on startup</div><div><span class="" style="white-space:pre"> </span>[--nocustomization]<span class="" style="white-space:pre"> </span>don't apply GUI customization</div><div><span class="" style="white-space:pre"> </span>[--customizationfile]<span class="" style="white-space:pre"> </span>use the given ini file as GUI customization</div><div><span class="" style="white-space:pre"> </span>[--optionspath path]<span class="" style="white-space:pre"> </span>use the given QSettings path</div><div><span class="" style="white-space:pre"> </span>[--configpath path]<span class="" style="white-space:pre"> </span>use the given path for all user configuration</div><div><span class="" style="white-space:pre"> </span>[--code path]<span class="" style="white-space:pre"> </span>run the given python file on load</div><div><span class="" style="white-space:pre"> </span>[--defaultui]<span class="" style="white-space:pre"> </span>start by resetting user ui settings to default</div><div><span class="" style="white-space:pre"> </span>[--help]<span class="" style="white-space:pre"> </span>this text</div><div><br></div><div> FILE:</div><div> Files specified on the command line can include rasters,</div><div> vectors, and QGIS project files (.qgs): </div><div> 1. Rasters - supported formats include GeoTiff, DEM </div><div> and others supported by GDAL</div><div> 2. Vectors - supported formats include ESRI Shapefiles</div><div> and others supported by OGR and PostgreSQL layers using</div><div> the PostGIS extension</div><div>========<br></div><div><br></div></div><div style>The FILE section is what I'm (and Uggla was) asking about.</div><div style>From that help info, it seems to be possible to load PostGIS layers when triggering the qgis command from the GNU/Linux command line.</div><div style>However, I cannot get it to work properly, and as soon as QGIS starts, I get a "Layer is not valid error."</div><div style><br></div><div style>Regards, </div><div style><br></div><div style>Germán </div><div style><br></div><div style><br></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">2014-12-09 6:02 GMT-05:00 Richard Duivenvoorde <span dir="ltr"><<a href="mailto:rdmailings@duif.net" target="_blank">rdmailings@duif.net</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi Germán,<br>
<br>
it took some time for me too to make it work :-)<br>
<br>
This one was easy, and working immediately:<br>
<br>
uri = QgsDataSourceURI()<br>
# host, port, dbase, user, password<br>
uri.setConnection("localhost", "5432", "bag", "geo", "xxx")<br>
# schema, table, geometry, whereclause<br>
uri.setDataSource("bag8mrt2014", "provincie", "geovlak", "")<br>
# layername, providername<br>
vlayer = QgsVectorLayer(uri.uri(), "test", "postgres")<br>
# just checking, should return True<br>
vlayer.isValid()<br>
# add it to mapcanvas<br>
QgsMapLayerRegistry.instance().addMapLayer(layer)<br>
<br>
but the one you want to use, with a uri string, took me some time to<br>
figure out. Best is to run QGIS in debug mode in terminal, to see if it<br>
is a query problem ( which you get when you do sql=''), or other problems.<br>
<br>
But for the above postgis layer, this is working as an uri example:<br>
<br>
l = iface.addVectorLayer('dbname=bag host=localhost port=5432 user=geo<br>
password=xxx sslmode=disable key=gid estimatedmetadata=true srid=28992<br>
type=MULTIPOLYGON table="bag8mrt2014"."provincie" (geovlak) sql=',<br>
"test", "postgres")<br>
<br>
'problems' you face when you use this:<br>
- do NOT use quotes, EXCEPT for the schema/table value<br>
- the table value should be double quotes, so the full string is single<br>
- do NOT forget the (geovlak) <- geometry! part as it returns a valid<br>
layer then, but you do not see anything.<br>
<br>
above uri string is actually taken from the debug output of QGIS when<br>
you load this layer via QGIS itself, and then stripped from quotes.<br>
<br>
hope this helps!<br>
<br>
Regards,<br>
<br>
Richard Duivenvoorde<br>
<span class=""><br>
<br>
<br>
<br>
On 09-12-14 00:59, Germán Carrillo wrote:<br>
> Hi all,<br>
><br>
> could you please let me know of a working command that allows one to<br>
> load a PostGIS layer into QGIS at startup?<br>
><br>
> I've tried with the information provided in the PyQGIS Cookbook [1] with<br>
> no success.<br>
><br>
> Specifically, I tried with the uri.uri() string, something<br>
> like: "dbname='buscador' host=localhost port=4326 user='postgres'<br>
> password='postgres' table='public'.'parques_nacionales' (geom) sql="<br>
> But get a "Layer is not valid error."<br>
><br>
> I'm using QGIS v.2.6 on GNU/Linux.<br>
><br>
> Regards,<br>
><br>
> Germán<br>
> --<br>
> [1]<br>
> <a href="http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html" target="_blank">http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html</a><br>
><br>
> 2013-12-05 8:19 GMT-05:00 Richard Duivenvoorde <<a href="mailto:rdmailings@duif.net">rdmailings@duif.net</a><br>
</span>> <mailto:<a href="mailto:rdmailings@duif.net">rdmailings@duif.net</a>>>:<br>
<span class="">><br>
> On 05-12-13 11:33, Uggla Henrik wrote:<br>
> > Is there no documentation about this? The extension mentioned is Postgis itself, not a specific Qgis extension, right?<br>
><br>
> Off course there is documentation, did you not read it first ;-)<br>
><br>
> <a href="http://www.qgis.org/en/docs/pyqgis_developer_cookbook/loadlayer.html#vector-layers" target="_blank">http://www.qgis.org/en/docs/pyqgis_developer_cookbook/loadlayer.html#vector-layers</a><br>
><br>
> Regards,<br>
><br>
> Richard Duivenvoorde<br>
><br>
> _______________________________________________<br>
> Qgis-user mailing list<br>
</span>> <a href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a> <mailto:<a href="mailto:Qgis-user@lists.osgeo.org">Qgis-user@lists.osgeo.org</a>><br>
<div class="HOEnZb"><div class="h5">> <a href="http://lists.osgeo.org/mailman/listinfo/qgis-user" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
><br>
><br>
><br>
><br>
> --<br>
> -----------<br>
> |\__<br>
> (:>__)(<br>
> |/<br>
><br>
> Soluciones Geoinformáticas Libres<br>
> <a href="http://geotux.tuxfamily.org/" target="_blank">http://geotux.tuxfamily.org/</a><br>
> <a href="http://twitter.com/GeoTux2" target="_blank">http://twitter.com/GeoTux2</a><br>
<br>
</div></div></blockquote></div><br>-- <br><div class="gmail_signature">-----------<br> |\__ <br>(:>__)(<br> |/ <br><br>Soluciones Geoinformáticas Libres <br><a href="http://geotux.tuxfamily.org/">http://geotux.tuxfamily.org/</a><br><a href="http://twitter.com/GeoTux2">http://twitter.com/GeoTux2</a><br></div>
</div></div>