I would like to know how we could do in QGis to get a list of object contained in my different opened layers: We have n .SHP files or PostgreSQL Views (they all contain at least an "object type" column and an "object id" column) opened in a QGis Project to make a map. When the map is done, I want to save my .QGS file on a server and then I want (by a Python program ?) to get a .CSV file with 3 columns (object type, object id, path of my .QGS file) - or even better to write the data directly into PostgreSQL.

Does anybody ses how to make that easily on QGis ?

  • I wanted to say .QGS file (we planned at the bennining to work with MapInfo where projects are .WOR, I haven't change all extensions in my new post, sorry) – max76 Oct 26 '15 at 8:20

Before you start you will want to decide what will be the "target", a file or a database. A file is a tiny bit simpler though psycopg2 makes handling postgres databases very easy as well.

with open('/home/you/data/out.csv', 'w') as f:

First you want to get a list of layers

    map_layers = QgsMapLayerRegistry.instance().mapLayers()

Then you want to iterate over every feature in each layer

    for layer in map_layers.values():
        for feat in layer.getFeatures():

And finally write whatever you want into the file:

            f.write('{}, {}, {}\n'.format(feat['object_type'], feat['object_id'], wor_file??))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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