1

I've got a qgis project with a lot of layers. In the past they were established as postgis-polygon-layers/-tables.

I updated my postgres/postgis-database recently.

Now the updated database stores the tables as MultiPolygon tables but QGIS does not recognize the modified type with the old connections. The change is only recognized if the tables are imported new into the project. It's the same with using qlr's with "Polygon"-connection-details.

I'd like to use the MultiPolygon-format to have less trouble cutting polygons into pieces.... So it would be nice to change the data-source-connection from Polygon to MultiPolygon.

As there are more than a hundered layers with relations to other layers in the project it would be a huge effort to replace the "old" Polygon layers with "new" MultiPolygon-layers and establish all the relataions, symbology ... manually

Is it possible to change the connection-details in a efficient, secure way (like python script / editing the qgs-file (replace all "Polygon" text with "MultiPolygon")) ?

I tried to change every layer-child of the layertree with the following code, but the connection is not changed.

If you run the code a second time the code does not enter the if-clause - so I thing some changes are applied but not enough.

What do I have to do to apply my changes in the layer-tree ?

from PyQt5.QtCore import * 
from qgis.gui import QgsMessageBar    

def editConnectionDeeperInstance(laTrGr):
    pos = 0
    for child in laTrGr.children():
        if isinstance(child, QgsLayerTreeGroup):
            editConnectionDeeperInstance(child)
        elif isinstance(child, QgsLayerTreeLayer) and child.layer():
            l=child.layer()
            provider = l.dataProvider()
            if provider.name() == 'postgres':
                uri = provider.dataSourceUri()
                for t in tablesToEdit:
                    if re.search(t,uri) and re.search("type=Polygon",uri):
                        print('changed:',child.name(),'-', t)
                        new_uri=x = re.sub("type=Polygon", "type=MultiPolygon", uri)
                        # print(new_uri)
                        provider.setDataSourceUri(new_uri)
                        provider.reloadData()  


tablesToEdit=["""public"."table1""","""public"."table2""" ]
laTrRo = QgsProject.instance().layerTreeRoot()
editConnectionDeeperInstance(laTrRo)
  • Editing the qgs file seems like a good route to pursue. I suggest you make a backup copy of your project, then open the original in a text editor. Notepad++ works pretty well for making qgs files readable (of course there are many other options, choose one you like). Search for one of the layers by its layer name, and see what text you'll need to replace. It will probably be replacing "Polygon" with "MultiPolygon" as you guessed. Next search the document for that original text, to see if it exists elsewhere. If it does, you can't just do a simple find-and-replace without breaking the project. – csk Jan 14 at 20:16
  • Depending on how many layers you have, you can then either manually make that text replacement - find each layer by searching for its name, then do the replacement. Or if you have too many layers to do it manually, then you're probably going to need a coding solution. This forum has a rule that if you want a coding solution you must make a first attempt and include that in your question (to avoid using this as a free coding resource). But sometimes people will help anyway, as long as you demonstrated that you did make some attempt on your own. If you take the steps I suggested and add that... – csk Jan 14 at 20:19
  • ...information to your question, someone may take pity and code it for you. It's probably as simple as looping through a list of layer names, finding the first instance of "Polygon" (or whatever) that occurs after the layer name, and replacing it with "MultiPolygon" (or whatever). Or there may be another, non-coded solution that I haven't thought of. – csk Jan 14 at 20:21

Your Answer

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

Browse other questions tagged or ask your own question.