1

I have the following PyQGIS script (see below) which works fine except from one issue: The input vector layer I would like to use is a temporary file. How can I call a temporary layer (which does not have a defined location) in PyQGIS?

##importing power plants vector layer
cpp = [call temporary layer here]

##importing population raster layer
pop = "C:/AIRPOLIM-ES/popKE.tif"

from qgis.analysis import QgsZonalStatistics

polygonLayer = QgsVectorLayer(cpp, 'zonepolygons', "ogr")     
QgsProject.instance().addMapLayer(polygonLayer)

rasterLayer = QgsRasterLayer(pop, "Raster Layer")
QgsProject.instance().addMapLayer(rasterLayer)

zoneStat = QgsZonalStatistics (polygonLayer, rasterLayer , 'pre-', 1,QgsZonalStatistics.Sum)
zoneStat.calculateStatistics(None)
  • It doesn't matter if it is temporary or not, try using the layer name: layer = QgsProject.instance().mapLayersByName("NAME_OF_YOUR_TEMPORARY_LAYER") – Kadir Şahbaz Jul 18 at 9:42
  • A temporary file should have a defined location, usually stored in the TEMP folder. You could try using polygonLayer = QgsVectorLayer(cpp.source(), 'zonepolygons', "ogr") – Joseph Jul 18 at 9:55

Your Answer

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

Browse other questions tagged or ask your own question.