[QGIS-Developer] QGIS standalone script examples

Paul Wittle paul.wittle at dorsetcouncil.gov.uk
Mon May 24 00:20:42 PDT 2021


Hi Richard,

My actual script has some extra stuff in it because some of the layers use the authentication manager and I check for Oracle connections but it is literally as simple as the example on the documentation really except that instead the open and close the bit in the middle I'm trying to do is to add a layer.

For context; I'm working on an FME routine to convert MapInfo workspaces into QGIS projects (ignoring anything complicated like layouts; this is just for the layers).

So far I have successfully converted MapInfo tab files to GeoPackage and converted the MapInfo styles into QGIS styles then applied the styling as the default in the converted GeoPackage. This last step should have been easier but it is also the first bit I'm really doing in Python.

The logic is simple:
1) Copy default project template
2) Open the project in pyqgis
3) Read the given GeoPackage to work out how many layers
4a) Add the layer
4b) Add a layer group if multiple
5) Save the project
6) Repeat until all layers added
7) Return Project file with associated GeoPackage layers in a zip file.

I had to do a trick to read the GeoPackages as they are on UNC paths and I'm calling Python standalone via a Windows batch file:
        original_working_directory = os.getcwd()
        os.chdir(overlay_path)
                gpkg_layers = [l.GetName() for l in ogr.Open(relativeLayerToAdd)]
                os.chdir(original_working_directory)

However that all seems to be working and I'm then adding the layers like this (for groups):

        root = project_instance.layerTreeRoot()
                group_entry = root.insertGroup(0,layerToAddName)

                for layerName in gpkg_layers:
                        currentSubLayer = QgsVectorLayer(layerToAdd + "|layername=" + layerName, layerName, 'ogr')
                        project_instance.addMapLayer(currentSubLayer,False)
                        group_entry.insertChildNode(1,QgsLayerTreeLayer(currentSubLayer))

This is the read command:
        project_instance = QgsProject.instance()
        project_instance.setFileName(project_path)
        canvas = QgsMapCanvas()
        bridge = QgsLayerTreeMapCanvasBridge( \
                        project_instance.layerTreeRoot(), canvas)

        project_instance.read()

Finally I'm just using a standard write command:
        project_instance.writeProject.connect(canvas.writeProject)
        project_instance.write()
        app.exitQgis()

However as I say; the resulting project file is significantly smaller than the one that was read so it must be deleting things it shouldn't even be modifying; I assume because the bridge bit is not being used and because it is running standalone.

Is that enough information?

Thanks,
Paul

-----Original Message-----
From: Richard Duivenvoorde <rdmailings at duif.net>
Sent: 23 May 2021 20:32
To: Paul Wittle <paul.wittle at dorsetcouncil.gov.uk>; qgis-developer at lists.osgeo.org
Subject: Re: [QGIS-Developer] QGIS standalone script examples

Hi Paul,

Can you come up with a little info about your actual use-case? Because maybe people then have some alternative ideas. QGIS-Application/Canvas off course does a lot of extra stuff when you load data (like for example using some default crs or styling etc etc...) not sure if you would want to redo that all in your scripts (IF it would be doable at all).

But maybe we could come up with another solution? (like maybe create 'qlr(?)' QGIS Layer files, or projects which point to other (small) projects (a project can be part of another project).
Or maybe you could re-use some excisting project by using some naming scheme in your data or so?

Regards,

Richard Duivenvoorde

On 5/23/21 12:59 PM, Paul Wittle wrote:
> Hi folks,
>
>
>
> I’m trying to do a simple script to open an existing project, add a layer and then save the result but finding it hard to piece together the information I can find online.
>
>
>
> I’ve found various posts suggesting that you need to connect up the canvas or all the GUI information gets lost when you do a project.write() command but for some reason I can only find answers that contain snippets which usually seem to reference iface or canvas without showing how to initiate those in a standalone way.
>
>
>
> Particularly, if you look at https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/loadproject.html <https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/loadproject.html> you have a note about needing to set a bridge but that references canvas which if you copy/paste as per the details in that section alone will of course result in a ‘canvas undefined’ error.
>
>
>
> Is there a simple full example anywhere that shows how you can open a project file using a standalone python script, add one layer and save it without losing half the information in the project file?
>
>
>
> My best effort so far added the layer but resulted in existing layers
> being invalid when I opened the project in QGIS and my “improved”
> version…at the moment…seems to be almost halving the size of the
> project so I assume it is still deleting all the canvas information.
> Any hints would be appreciated 😊
>
>
>
> Thanks
>
> Paul
>
> This e-mail and any files transmitted with it are intended solely for
> the use of the individual or entity to whom they are addressed. It may
> contain unclassified but sensitive or protectively marked material and
> should be handled accordingly. Unless you are the named addressee (or
> authorised to receive it for the addressee) you may not copy or use
> it, or disclose it to anyone else. If you have received this
> transmission in error please notify the sender immediately. All
> traffic may be subject to recording and/or monitoring in accordance
> with relevant legislation. Any views expressed in this message are
> those of the individual sender, except where the sender specifies and
> with authority, states them to be the views of Dorset Council. Dorset
> Council does not accept service of documents by fax or other
> electronic means. Virus checking: Whilst all reasonable steps have
> been taken to ensure that this electronic communication and its
> attachments whether encoded, encrypted or otherwise supplied are free
> from computer viruses, Dorset Council accepts no liability in respect
> of any loss, cost, damage or expense suffered as a result of accessing
> this message or any of its attachments. For information on how Dorset
> Council processes your information, please see
> www.dorsetcouncil.gov.uk/data-protection
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>

This e-mail and any files transmitted with it are intended solely for the use of the individual or entity to whom they are addressed. It may contain unclassified but sensitive or protectively marked material and should be handled accordingly. Unless you are the named addressee (or authorised to receive it for the addressee) you may not copy or use it, or disclose it to anyone else. If you have received this transmission in error please notify the sender immediately. All traffic may be subject to recording and/or monitoring in accordance with relevant legislation. Any views expressed in this message are those of the individual sender, except where the sender specifies and with authority, states them to be the views of Dorset Council. Dorset Council does not accept service of documents by fax or other electronic means. Virus checking: Whilst all reasonable steps have been taken to ensure that this electronic communication and its attachments whether encoded, encrypted or otherwise supplied are free from computer viruses, Dorset Council accepts no liability in respect of any loss, cost, damage or expense suffered as a result of accessing this message or any of its attachments. For information on how Dorset Council processes your information, please see www.dorsetcouncil.gov.uk/data-protection


More information about the QGIS-Developer mailing list