I am using Python in QGIS to calculate zonal (sum) statistics for an array of raster layers using the following code:

polygonLayer  = QgsVectorLayer('E:/folder/' + 'gadm0.shp','','ogr')
rasterFilePath = "E:/folder" + r'/merged' + str(x) + '_R.tif'

zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, str(x), 1, QgsZonalStatistics.Sum)

However, during the code I get some warnings like "value x not successfully written for feature z, possibly due to too larger number with respect to field width". I guess this is owing to the fact that shapefiles have a limit in the number of digits each field can contain.

When I open the attribute table of the output shapefile, I notice that:

1) a number of features have 0 as the output value, which does not make sense and thus I guess are those affected by the error. 2) successfully written features have way too many decimal digits than those I need.

How can I round the sum statistic to avoid data loss and useless decimal digits?

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.