2

I would like to export a .pdf with 2 maps (with different layers). However, some layers do not appear in the map items!

I'd like:

  • In the first map: the shapefile over the raster 1, and these two over the basemap (A300 + Area1 + basemap)

  • In the second map: the shapefile over the raster 2, and these two over the basemap (A300 + Area2 + basemap)

Everything appears fine in the layout view (see image left) but in the .pdf only the basemap appears (see image right)! What is wrong? here is my code

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 basemap (raster)
basemap = "C:\\MyData\\LK25.vrt"
basemap_r = iface.addRasterLayer(basemap , "my_basemap")
basemap_r.setCrs(QgsCoordinateReferenceSystem(2056))

# 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)

# add raster layer 2
rlayer2 = iface.addRasterLayer("C:\\MyData\\Area_2.asc", "Area2")
rlayer2.setCrs(QgsCoordinateReferenceSystem(2056))
project.addMapLayer(rlayer2)


#-- 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 1 (shapefile, raster 1, basemap)

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

map1.setLayers([shplayer, rlayer1, basemap_r]) # set layers to be mapped
map1_extent = QgsRectangle(rlayer1.extent())
map1_extent.scale(1.0)
map1.setExtent(map1_extent)

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


#--- create map item 2 (shapefile, raster 2, basemap)

map2 = QgsLayoutItemMap(layout)
map2.setRect(QRectF(10, 110, 210, 90))
map2.setPos(10,110) 
map2.setFrameEnabled(True)

map2.setLayers([shplayer, rlayer1, basemap_r])
map2.setExtent(map1_extent)

layout.addLayoutItem(map2)
map2.attemptMove(QgsLayoutPoint(10,110, QgsUnitTypes.LayoutMillimeters))
map2.attemptResize(QgsLayoutSize(210,90,QgsUnitTypes.LayoutMillimeters))

layout.refresh()
project.write()

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



enter image description here

  • try changing the loading order of the layers qgis.org/api/… – Fran Raga Aug 19 at 10:46
  • I tried both orders and it does not work :-( The documentation says that the first layer in the list will be on the top, so the order of my list should be fine. I tried to take out the underlaying map, and there, none of my two other layers appear! Do you have another hint? – SaskiaG Aug 19 at 12:55
  • OK at the end I found out that the layers where in another coordinate system than the base map, that's why they did not appear here... sorry – SaskiaG Aug 28 at 9:34
1

You're not actually using your "ms" variable anyway, so setting the layers there has no effect...

What you need to do is call map.setLayers(). See https://qgis.org/pyqgis/3.8/core/QgsLayoutItemMap.html#qgis.core.QgsLayoutItemMap.setLayers

  • True I haven't see that... sorrry. I changed with map.setLayers() but it did not solve the problem. I realized that there is a problem in the .pdf export, because the maps render well in the layout, but not in the .pdf. I edited my questions. Do you have any hints why? – SaskiaG Aug 21 at 8:30

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.