5

I can load a PostGIS layer on QGIS' map canvas but "Toggle Editing" button comes "inactive". How can I load vector layer as editable?

The code given below loads PostGIS layer on map canvas & "Toggle Editing" button comes "active" but once I run the script below in Python console; when I press "Toggle Editing" button, QGIS gives me an info message: "Start editing failed: Provider cannot be opened for editing":

sql = "(SELECT * FROM tablename)"
uri = QgsDataSourceURI()
uri.setConnection("localhost", "5432", "database", "postgres", "password")
uri.setDataSource("", sql, "geom", "", "gid")
vlayer = QgsVectorLayer(uri.uri(), "abc", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

# should the layer be set as "active" ???

vlayer.dataProvider()
vlayer.dataProvider().capabilities()
vlayer.startEditing()
iface.actionToggleEditing().setEnabled(True)

Can someone please give me a hint on how to load an editable layer programmatically?

1

3 Answers 3

3

To load a shapefile into QGIS and set the layer as "editable", I executed the code below in the Python Concole:

LayerName =iface.addVectorLayer("/Path/To/Your/Shapefile.shp", "name", "ogr")
qgis.utils.iface.setActiveLayer(LayerName)
qgis.utils.iface.actionToggleEditing().trigger()

For PostGIS tables this blog post may help to set up the SELECT statement in order to load the PostGIS table successfully. Once the layer has been loaded into QGIS I assume the code below will enable/disable editing:

qgis.utils.iface.setActiveLayer(LayerName)
qgis.utils.iface.actionToggleEditing().trigger()

I have not tried the PostGIS example yet. This is my first post on GIS StackExchange so I hope it helps.

1
  • I can already load PostGIS tables successfully but they don't come "editable". Your solution is right for .SHP files. Presumably, because of OGR data provider. But my provider is "postgres" and I couldn't still find the solution... Commented May 2, 2015 at 10:43
2

This post might help in your case. At least I had problems with PostGIS primary key missing and causing QGIS not to be able to edit the data stored in PostGIS.

Unable to edit PostGIS DB using QGIS 2.10, works on 2.8

-1

You are incorrectly setting the data source. Also sql string is not needed. The below code should work for you.

sql=""
uri=QgsDataSourceURI()
uri.setConnection("host","port","dbname","user","password",QgsDataSourceURI.SSLdisable)
uri.setDataSource("schema","table_name","geometry_column","primary_key")
uri.setSrid("srid")
vlayer=QgsVectorLayer(uri.uri(),"abc","dbname")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
1
  • 1
    Could you type the code out itself instead of posting a screenshot please? This would make it easier (and far quicker) for anyone wanting to test it as they can copy/paste the code :)
    – Joseph
    Commented Nov 24, 2015 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.