1

I am using the latest version of QGIS 3.34 and trying to dynamically edit and update properties for Elevation Profiles in QGIS Map Atlas Layouts.

I am hoping to update layer and display symbols and other settings for a number of Elevation Profiles (see Class documentation QgsLayoutItemElevationProfile items) in a Map Atlas Layout when moving from one atlas page to another, (for over a 100 pages per atlas).

In order to refer to each of the different elevation profile items to change their settings I have been trying to find a way to refer to the displayname or the id (manually set in item id of the Elevation Profile). It looks like this is accessed using displayname or id using the class QgsLayoutItem.

After extensive searching for what seems relatively new functionality I cannot find any python examples on the use and updating of an QgsLayoutItemElevationProfile

I have tried many attempts to establish the name of each Elevation Profile. I have tried using selectedLayoutItems from the class QgsLayout but it returns an object identifier that I cant work out how to use.

Any examples on how to refer to Elevation Profiles by qgslayoutitem displayname or id and to update their properties?

1 Answer 1

0

Without seeing how you create your layout and its items, it's hard to give an exact answer.

However, you can use the .displayName() method, but you must first rename the elevation profile in the layout.

enter image description here

You can loop through all the items and check their class and display name, like so:

p = QgsProject.instance()
manager = p.layoutManager()
layout = manager.layouts()[0]                  # adjust index accordingly
# or
# layout = manager.layoutByName('Layout 1')    # adjust name accordingly

items = layout.items()

elev_profile = None
for item in items:
    if isinstance(item, QgsLayoutItemElevationProfile) and item.displayName() == 'my elevation profile':
        elev_profile = item
        break

elev_profile
# >>> <qgis._core.QgsLayoutItemElevationProfile object at 0x00000163FE685C10>

Or by using list comprehension:

elev_profile = [item for item in items if isinstance(item, QgsLayoutItemElevationProfile) and item.displayName() == 'my elevation profile'][0]

elev_profile
# >>> <qgis._core.QgsLayoutItemElevationProfile object at 0x00000163FE685C10>

If you create the QgsLayoutItemElevationProfile using PyQGIS then you can assign an ID (and/or display name) directly:

elev_profile = QgsLayoutItemElevationProfile(layout)
elev_profile.setId('my elevation profile id')
elev_profile.setDisplayName('my elevation profile')
5
  • Hi Mark. Thank you for your prompt, thorough and helpful reply. I have been able to create a new objects already in the way you describe. It will be helpful to set the name in code as you have shown.
    – dawrightau
    Nov 15, 2023 at 11:39
  • I have also previously generated a object reference similar to 0x00000163FE685C10 under your elev_profile. - per my original question what I still don't know and can't see from your examples (or any others on the Web) is how to refer directly to the elevation profile item using this object reference or by the assigned name / item_id and then update a property for example, the symbology of an elevation layer such as changing the line colour and width or setting a value in an expression.
    – dawrightau
    Nov 15, 2023 at 11:52
  • Don't worry too much about the 0x00000163FE685C10 number. That just refers to its position in memory. You now have reference to elevation profile object with the name elev_profile. You can use that directly. E.g. elev_profile.layers() will give you the list of layers participating in the profile.
    – Matt
    Nov 15, 2023 at 12:11
  • Thanks for the clarification Mark. Makes sense, will see if I can now apply. Again much appreciated.
    – dawrightau
    Nov 15, 2023 at 17:47
  • That worked great thanks Mark, Enabled me to progress whereas time previously had stopped:)
    – dawrightau
    Nov 16, 2023 at 2:00

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.