[Qgis-user] Print layout rectangle not working properly

Nigel Berjak - General nigel at s3.co.za
Sun Nov 10 22:36:09 PST 2024


Hi Asim

Before delving into any coding issues, I wonder whether this may simply 
be related to the layout page size and the map element shape, which is 
not the same proportions as your extent that you are aiming to set. If 
you are not requiring a specific map element size and this can be 
variable, then perhaps resize the element based upon your coordinates 
rather than the layout dimensions, as you are doing in the final line. 
To test if my thinking is sound, stop your code after the line 
map.setExtent(rectangle) and repaint the layout. If this works, then try 
resize the map element before setting the coordinates and try again. I 
suspect that it will be fine using approach 1 but one of your coordinate 
ranges i.e. X or Y will be amended due to, as I mentioned, the map shape 
not conforming to the coordinates shape.

I hope this helps.

---
Regards,

Nigel Berjak
Please consider the environment before printing this email.

On 2024-11-08 22:33, Asim al-sofi via QGIS-User wrote:

> Hi all
> Iam trying to make a print layout using a defined rectangle.
> The print layout created using the rectangle is correct in the x 
> coordinates but in the y coordinates it is always smaller than the 
> defined rectangle.
> The code snippet is:
> 
> layout = QgsPrintLayout(project)
> layoutName = "PrintLayout"
> #initializes default settings for blank print layout canvas
> layout.initializeDefaults()
> layout.setName(layoutName)
> manager.addLayout(layout)
> map = QgsLayoutItemMap(layout)
> map.setRect(20, 20, 20, 20)
> #defines map extent using map coordinates
> rectangle = 
> QgsRectangle(float(bounds[0]),float(bounds[1]),float(bounds[2]),float(bounds[3]))# 
> x min y min, x max y max
> rectangle = 
> QgsRectangle(QgsPointXY(float(bounds[0]),float(bounds[1])),QgsPointXY(float(bounds[2]),float(bounds[3])))# 
> x min y min, x max y max
> map.setExtent(rectangle)
> layout.addLayoutItem(map)
> #Move & Resize map on print layout canvas
> map.attemptMove(QgsLayoutPoint(0, 0, QgsUnitTypes.LayoutMillimeters))
> map.attemptResize(QgsLayoutSize(297, 210, 
> QgsUnitTypes.LayoutMillimeters))
> 
> Any ideas what is wrong with my code?
> Thanks in advance
> Kind regards,
> Asim
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20241111/91e60ba8/attachment.htm>


More information about the QGIS-User mailing list