1

Here is how the log shows for one raster image. I want to do this same thing for several rasters. The batch processing is not working.

QGIS version: 3.8.0-Zanzibar
QGIS code revision: 11aff65f10
Qt version: 5.11.2
GDAL version: 2.4.1
GEOS version: 3.7.2-CAPI-1.11.0 b55d2125
PROJ version: Rel. 5.2.0, September 15th, 2018
Processing algorithm…
Algorithm 'Zonal statistics' starting…
Input parameters:
{ 'COLUMN_PREFIX' : 'sum_201204', 'INPUT_RASTER' : 'C:/Users/ghosh/Downloads/vbd_China_fishingban_analysis/tif/VBD_npp_20120401-20120430_asia_qf1-2-3-8-10-pc_v23_c201806110200.n_detect.china_crop.tif', 'INPUT_VECTOR' : 'C:/Users/ghosh/Downloads/vbd_China_fishingban_analysis/vectors/china_eez_v9_banarea.shp', 'RASTER_BAND' : 1, 'STATS' : [1] }

Execution completed in 17.04 seconds
Results:
{'INPUT_VECTOR': <qgis._core.QgsVectorLayer object at 0x0000029580A2E4C8>}

Loading resulting layers Algorithm 'Zonal statistics' finished


This is the error for batch processing

Input parameters: {} Traceback (most recent call last):

File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\algs\qgis\ZonalStatistics.py", line 128, in prepareAlgorithm self.raster_interface = rasterLayer.dataProvider().clone() AttributeError: 'NoneType' object has no attribute 'dataProvider'

Batch execution completed in 0.02 seconds

  • 4
    Please Edit your code so it is legible and formatted so as to be syntactically valid – Vince Jul 10 at 22:58
  • 1
    In processing there is an option to run batch jobs like these. This is a far simpler solution if you are a beginner in python – kartoza-geek Jul 11 at 2:35
  • This is the error for batch processing - Input parameters: {} Traceback (most recent call last): File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\algs\qgis\ZonalStatistics.py", line 128, in prepareAlgorithm self.raster_interface = rasterLayer.dataProvider().clone() AttributeError: 'NoneType' object has no attribute 'dataProvider' Batch execution completed in 0.02 seconds – Tilottama Ghosh Jul 11 at 12:13
  • I added a pyqgis solution, but I am facing issue with batch processing in QGIS3.8. I am not able to fill the 'raster band' parameter in batch processing dialog. – spatialthoughts Jul 19 at 3:49
0

I wrote a tutorial exactly for this. As the Zonal Stats algorithm updates the existing layer instead of creating a new one, this algorithm is ideally run in a for-loop on one raster layer at a time.

Here's the relevant code snippet (you have to modify it slightly to work with your layer names). Assuming your raster layers start with name 'RASTER' and your vector layer is called 'VECTOR', the following should work

root = QgsProject.instance().layerTreeRoot()
for layer in root.children():
  if layer.name().startswith('RASTER'):
    prefix = layer.name()[-6:-4]
    params = {'INPUT_RASTER': layer.name(), 'RASTER_BAND': 1, 'INPUT_VECTOR': 'VECTOR', 'COLUMN_PREFIX': prefix+'_', 'STATS': 2}
    processing.run("qgis:zonalstatistics", params)

I tested it on 3.8 and it works. See the below tutorial for step by step instructions

http://www.qgistutorials.com/en/docs/3/processing_algorithms_pyqgis.html

  • Hi, Thanks for sharing the code and the great tutorial. I opened the raster files I have on QGIS 3.8, and also the vector file. My raster files start with 'VBD', and I replaced 'VECTOR' with the name of the shapefile I have. The code did not show any error but did not do anything either. Where am I going wrong? I will look forward to you reply. Thanks. – Tilottama Ghosh Jul 23 at 21:53
  • Did you check if a new attribute was added in the attribute table of the vector layer? I have seen that if the vector layer is from a provider that doesn't allow updating the table (such as a zip file), the algorithm doesn't give error but doesn't update the table also. Try to unzip and load the layer if that's the case. – spatialthoughts Jul 24 at 4:36
  • root = QgsProject.instance().layerTreeRoot() for layer in root.children(): if layer.name().startswith('VBD'): prefix = layer.name()[-6:-4] params = {'INPUT_RASTER': layer.name(), 'RASTER_BAND': 1, 'INPUT_VECTOR': 'fishingzonespoly_chinaeez_diff_ninedash_final2Copy.shp', 'COLUMN_PREFIX': prefix+'_', 'STATS': 2} processing.run("qgis:zonalstatistics", params) – Tilottama Ghosh Jul 24 at 21:04
  • Hi Spatialthoughts - this is how I had run it. I had all the raster layers and the vector layer open. The raster layers are named as 'VBD_npp_20120401-20120430_asia_qf1-2-3-8-10-pc_v23_c201806110200.n_detect.china_crop.tif' and I would like to include '201204' in the column attributes. Is the line of code 'prefix = layer.name()[-6:-4]' doing this? Also, I want only the 'sum' in zonal stats, does 'STATS': 2 help me compute this? I still could not get it to work. Thanks. – Tilottama Ghosh Jul 24 at 21:11

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.