More

PyQGIS - load PostGIS layer as editable

PyQGIS - load PostGIS layer as editable


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?


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.


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


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)

Using the new Microsoft building footprint dataset in QGIS

I converted it to a geopackage using ogr2ogr. Works fine.

I hacked together a python script to chop the footprints into counties and rectangles, and was easily able to put them into QGIS.

I managed to load the NY dataset into QGIS by first loading it into PostgreSQL via ogr2ogr.

Checkout these ipython notebooks for loading the data into PostGIS https://github.com/gboeing/ms-bldg-footprints

Exactly the solution I was looking for.

I’ve tried with no luck. Downloaded the geojson of my state (MO) from github. Tried simply adding it to a blank map to view it and it froze. Then tried ogr2ogr to make a shapefile, again my computer froze. Last try is to import it into my Postgis database.

ESRI has a tile layer they host of it if anyone is interested. Wondering if the data is too much for my computer to handle?

You just need to split the geojson file size and it will work. I use it all the time.

Are you saying the tile layer should be openable in QGIS?

Looks like people have forked the project to make the filesize more manageable. I've never heard of this and need building footprints all the time.

I was able to ogr2ogr the data into a shapefile without problem for Nevada but Arizona had many more buildings and was giving me a lot of trouble. I eventually just left my frozen computer on over the weekend and when I came in on Monday it had successfully converted the data.

I just pulled the geosjon straight in. I only used the Florida subset and it took a very long time to open.

Don't load the whole thing. Florida alone is 6 million polys

Here is the link to how Ian Dees processed them on an AWS m5.xlarge, obviously no need to go that serious but interesting to see how he processed it in terminal with some Python.

That does seem interesting but a little over my pay grade. I have a large state, Texas and managed to split with geojsplit. Is there a slick way to us it's Key switch to try and split it up by geographical location, such as bounding box? When I split it up starting from the beginning, I find myself still needing to load all the split files to get get all the buildings in the area I am interested. I have 100 files and at around 30 I started running out of memory.

Is there a simple way to parse it for polygons within a given area? Hopefully via the command line. Like cat or grep results output to a new file?


Watch the video: QGIS Python PyQGIS - Loading and symbolizing raster layers