I have a temporary vector file from a qgis processing.run() (basically a line shapefile temporary output):
gis_osm_roads_free_1_lyr = results['Gis_osm_roads_free_1']
I have a map_item:
map_item = layout.itemById('Map 1')
I have a list of aspect ratios:
aspect_ratios = [(350, 300),(130,180), (180, 240), (150, 200), (150, 210)]
There are more of aspect ratio values but these are just for the question.
With all of these above I want to export a map with all of those different aspect ratios, a map that has gis_osm_roads_free_1_lyr in it.
So there is a function that sets the aspect ratio and then exports the map:
for aspect_ratio in aspect_ratios:
page_width, page_height = aspect_ratio
page_size = QgsLayoutSize(page_width, page_height)
layout.pageCollection().pages()[0].setPageSize(page_size)
ZoomExtent(page_height, scale, canvas, map_item)
# Create QgsLayoutSize for map item size
map_size = QgsLayoutSize(page_width, page_height)
map_item.attemptResize(map_size)
The issue:
"gis_osm_roads_free_1_lyr" presented in the map item is not perfectly fit to my map_item.
Even if I zoom to extent with PyQGIS it fits my layer width wise (but for all my aspect ratios width wise is always perfectly fit). Height wise I always have empty spaces above and below the layer.
Question: How can I zoom automatically in order that my layer fits perfectly into map_item aspect ratio, height wise. It does not matter if I lose some of the data due to the zoom, but I would like that the layer covers perfectly the map_item height wise.