Skip to main content
edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
added 66 characters in body
Source Link

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.

What I have tested:

canvas = iface.mapCanvas()
scale = canvas.scale()
scale_factor = page_height / canvas.height() # Used canvas height because layer extent equal to canvas extent.
new_scale = scale * scale_factor
canvas.zoomScale(new_scale)
map_item.refresh()

The formula for "new_scale" has no good correlation. For some cases it zooms too much and the scale factor value should be until top and bottom extent of layer match top and bottom extent of map_item.

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.

What I have tested:

canvas = iface.mapCanvas()
scale = canvas.scale()
scale_factor = page_height / canvas.height()
new_scale = scale * scale_factor
canvas.zoomScale(new_scale)
map_item.refresh()

The formula for "new_scale" has no good correlation. For some cases it zooms too much and the scale factor value should be until top and bottom extent of layer match top and bottom extent of map_item.

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.

What I have tested:

canvas = iface.mapCanvas()
scale = canvas.scale()
scale_factor = page_height / canvas.height() # Used canvas height because layer extent equal to canvas extent.
new_scale = scale * scale_factor
canvas.zoomScale(new_scale)
map_item.refresh()

The formula for "new_scale" has no good correlation. For some cases it zooms too much and the scale factor value should be until top and bottom extent of layer match top and bottom extent of map_item.

added 411 characters in body
Source Link

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: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: HowQuestion:

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.

What I have tested:

canvas = iface.mapCanvas()
scale = canvas.scale()
scale_factor = page_height / canvas.height()
new_scale = scale * scale_factor
canvas.zoomScale(new_scale)
map_item.refresh()

The formula for "new_scale" has no good correlation. For some cases it zooms too much and the scale factor value should be until top and bottom extent of layer match top and bottom extent of map_item.

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.

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.

What I have tested:

canvas = iface.mapCanvas()
scale = canvas.scale()
scale_factor = page_height / canvas.height()
new_scale = scale * scale_factor
canvas.zoomScale(new_scale)
map_item.refresh()

The formula for "new_scale" has no good correlation. For some cases it zooms too much and the scale factor value should be until top and bottom extent of layer match top and bottom extent of map_item.

Source Link
Loading