I'm trying to arrange some map items onto a print layout. Let's start with the map item itself.
Here is my code to add a map item to the print layout. Specifically I have questions about the setExtent() and attemptResize() functions.
In this code below I set the extent with setExtent() before setting the size with attemptResize()
#This creates a new print layout
project = QgsProject.instance()
manager = project.layoutManager()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName('My Layout')
manager.addLayout(layout)
#This is where I configure layout items
map = QgsLayoutItemMap(layout)
rectangle = QgsRectangle(-1350312, -21811, 1741463, 116086) #These are coordinates around my desired map extent
map.setExtent(rectangle)
map.attemptResize(QgsLayoutSize(250, 180, QgsUnitTypes.LayoutMillimeters))
#adds map to print layout
layout.addLayoutItem(map)
This produces the following Print Layout. Notice that the map is "rendering" (Why????). But the sizing of the item is what I want. If I try to export this as a .pdf or image, the final product is blank.
According to this thread, I need to set the map item size before I set the extent. Here is the code, where I have simply switched the order of setExtent() and attemptResize():
#This creates a new print layout
project = QgsProject.instance()
manager = project.layoutManager()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName('My Layout11')
manager.addLayout(layout)
map = QgsLayoutItemMap(layout)
map.attemptResize(QgsLayoutSize(250, 180, QgsUnitTypes.LayoutMillimeters))
rectangle = QgsRectangle(-1350312, -21811, 1741463, 116086)
map.setExtent(rectangle)
layout.addLayoutItem(map)
and I get the following print layout. If I zoom in closely, the extent is still correct. But as you can see the image is very small and I have no idea what the attemptResize() function is even doing here, as the units of where the object is on the canvas makes no sense:
What am I not understanding correctly??