0

In the layout, I would like to display two layers and for this I need their combined maximum extent. So far I've got:

import os
import processing
from qgis.core import *
from qgis.gui import *
import qgis.utils
import shutil
from qgis.PyQt import QtGui


project = QgsProject.instance()
project.setCrs(QgsCoordinateReferenceSystem(21781))

iface.mainWindow().blockSignals(True)


# add vector layer
shplayer = iface.addVectorLayer("C:\\MyData\\A300.shp", "A300", "ogr")
shplayer.setCrs(QgsCoordinateReferenceSystem(2056))
project.addMapLayer(shplayer)

# add raster layer 1
rlayer1 = iface.addRasterLayer("C:\\MyData\\Area_1.asc", "Area1")
rlayer1.setCrs(QgsCoordinateReferenceSystem(2056))
project.addMapLayer(rlayer1)

# find max extent of all layers
ext_r = rlayer1.extent().toString().replace(":",",").split(',') #(xmin, xmax, ymin, ymax)
ext_shp = shplayer.extent().toString().replace(":",",").split(',')
ext_all = [min(ext_r1[0],ext_shp [0]), max(ext_r1[1],ext_shp [1]), min(ext_r1[2],ext_shp [2]), max(ext_r1[3],ext_shp [3])]



#-- Create custom caneva
manager = project.layoutManager()
layout_name = 'my_custom_layout'
layout_list = manager.printLayouts()

layout = QgsPrintLayout(project)
layout.setName(layout_name)
layout.initializeDefaults()
manager.addLayout(layout)

#--- create map item (shapefile, raster 1)

map1 = QgsLayoutItemMap(layout) # create Layout item
map1.setRect(QRectF(10, 10, 210, 90)) # set the map item rectangle
map1.setPos(10,10) # map item upper left corner position
map1.setFrameEnabled(True) # to have a map black border

ms = QgsMapSettings()
ms.setLayers([shplayer, rlayer1]) # set layers to be mapped


# !! here I'd like the max extent of both layers! QgsRectangle(rlayer1.extent() + shplayer.extent())
map1_extent = QgsRectangle(float(ext_all[0]), float(ext_all[2]), float(ext_all[1]), float(ext_all[3])) # must be (xmin, ymin, xmax, ymax) but does not work!
map1_extent.scale(1.0)
ms.setExtent(map1_extent)
map1.setExtent(map1_extent)

layout.addLayoutItem(map1)
map1.attemptMove(QgsLayoutPoint(10,10))
map1.attemptResize(QgsLayoutSize(210,90))


# export map in .pdf
exporter = QgsLayoutExporter(layout)
exporter.exportToPdf("C:\\mapstest.pdf", QgsLayoutExporter.PdfExportSettings())


project.write()

How can I fix this?

Cheers

1

I prepare this for your case, the QgsRectangle. have a function that allow combine the extends, the name of the function is this, combineExtentWith

you can use in your code like this:

ext = rlayer1.extent()
ext_shp = shplayer.extent()
ext.combineExtentWith(ext_shp)

in this case the object ext will be ext_all, you have to place here:

map_extent = ext

because this is a QgsRectangle, I prove your code with this changes and worked.

  • "combineExtentWith" did the trick! Thanks a lot :-) – SaskiaG 2 days ago
  • You are welcome ;) – Jhon Galindo 23 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.