1

I have a Python script that runs and creates statistics using zonal statistics sum tool. The python script adds each layer to the map successfully:

# Add Base Map
OSMBase = "....\\OSM_Base.xml"
rlayer2 = QgsRasterLayer(OSMBase, "OSM Base Map")
if not rlayer2.isValid():
    print("Layer failed to load!")
QgsProject.instance().addMapLayer(rlayer2)

# Add Population Raster
path_to_tif = "....\\LAC_Population.tif"
rlayer = QgsRasterLayer(path_to_tif, "LAC_Population")
if not rlayer.isValid():
    print("Layer failed to load!")
QgsProject.instance().addMapLayer(rlayer)

# Add Intensity
path_to_intensity = "..../mi.shp"
vlayer = QgsVectorLayer(path_to_intensity, "Intensity", "ogr")
if not vlayer.isValid():
    print("Layer failed to load!")
else:
    QgsProject.instance().addMapLayer(vlayer)

print(QgsProject.instance().mapLayers())
...............................................................

The base map is added as an .xml file, there is a population raster .tif file as well as an intensity shapefile. Ultimately, I run population statistics with the shapefile, to roughly work out the affected population within each intensity (as can be seen on the maps below). When I use the QGIS Python Console and the print composer, then exporting the layout as a .png file, the map looks like this and it all works perfectly.

enter image description here

However, when I use PyQGIS and add the data, run statistics, change symbology, create a new layout/print composer, add map features and then export the map, the exported map does not print successfully. I can see that the statistics and symbology has been completed but the raster base map is not seen on the map. I know that the map has been added to QGIS and the layout, because the blue water is displayed and if the file was not loaded properly, there would be white instead. I have tried to change the CRS of the layer but still no success. I have also tried moving the layers up/down but no success. The scale is wrong too but I think this is due to the base map not loading properly.

Does anyone know why the raster layer is not appearing in my map and why the export of the map does not include this layer using Python?

Could it potentially be down to the export resolution?

It is weird as I have another script that runs a similar process and has the same base map and it export perfectly.

enter image description here

A WeTransfer link to download the three files is - https://we.tl/t-xgrHBo8Fos

2
  • Maybe wait until the distant rasterlayer has finished to load with a code like iface.mapCanvas().waitWhileRendering() ? Mar 3, 2021 at 14:21
  • Due to 'iface.' not able to work outside of the QGIS Python Console, I instead used QgsMapCanvas().waitWhileRendering() after I created the layout and added the map to the layout but still no success. The layer is still not loaded in
    – LiamHems
    Mar 3, 2021 at 14:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.