0

I have the following layout and map item:

layout = project.layoutManager().layoutByName('Test')

map_item = layout.itemById('Map 1')

map_item.zoomToExtent(canvas.extent())

I have the following extract by attribute algorithm:

alg_params = {
    'FIELD': 'highway',
    'INPUT': outputs['RenameField_roads']['OUTPUT'],
    'OPERATOR': 0,  # =
    'VALUE': 'motorway_link',
    'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['ExtractByAttributeMotorway_link_osm'] = processing.run('native:extractbyattribute', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Motorway_link_osm'] = outputs['ExtractByAttributeMotorway_link_osm']['OUTPUT']

For multiple layers I have tried to do something like this, but the export is blank:

temporary_layers = [
    results['Motorway_link_osm'],
    results['Motorway_osm'],
]

map_item.setLayers(temporary_layers)

As a possible solution, I thought to add a vector layer to the layers panel then export the map, but maybe it is a simpler solution.

I would like to add the temporary output vector layer to the 'Map 1' map item without having to import it to the layer panel in order to automatically export the map.

How can I do this process for this temporary output?

What if I have multiple temporary outputs to add to the map item in order to have them to the exported map?

1
  • 2
    Because you are using processing.run() calls as child algorithms, their outputs will be map layer id strings, not QgsVectorLayers, see this answer. You can get a map layer object like: motorway_link_lyr = context.getMapLayer(results['Motorway_link_osm']).
    – Ben W
    Commented Aug 12, 2023 at 12:18

1 Answer 1

0

As suggested by Ben W, it works with context.getMapLayer().

Solution:

  • Mentioning the map item:

    map_item = layout.itemById('Map 1')
    map_item.zoomToExtent(canvas.extent())
    
  • Adding vector layers into a list:

    motorway_link_lyr = context.getMapLayer(results['Motorway_link_osm'])
    
    motorway_osm_lyr = context.getMapLayer(results['Motorway_osm'])
    
    layers_to_add = [motorway_link_lyr, motorway_osm_lyr]
    
  • Adding vector layers to map item:

    map_item.setLayers(layers_to_add)
    

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.