# Map algebra in QGIS

How do you calculate standard deviation for each raster if you have set of rasters (100)? I was searching in raster calculator, but I didnt find it. Also google redirect me to using R or python. But I have just started learning.

So is there som easy way? Or I should really start learning python / R.

I have seen one post here. About Grass, but they was calculating stdev. across multiple rasters.

• Do you want to calculate the standard deviation across the entire raster, or in parts of it? Commented Oct 22, 2015 at 14:56
• @MikkelLydholmRasmussen i have split one raster to many small... So for parts....right now Im looking at RasterStats plugin, but it seems like it is not working on new qgia
– ABCD
Commented Oct 22, 2015 at 14:59
• You could look at Zonal Statistics. It does focus on using polygons, as zones, but you should be able to relatively easily create those polygons from your small rasters. See qgis.org/en/site/forusers/visualchangelog210/… Commented Oct 22, 2015 at 15:08

You don't need to write code if you have GDAL installed (if you have QGIS installed that should be the case, but you can install GDAL seperately if you want/need to)

This assumes you want a separate stddev for each image. If you want the stddev across ALL the images, you'll need to do a Raster Merge to combine them into a single raster.

The gdalinfo tool with its-stats option will spit out several summary stats for each band in a raster:-

... snip ...
Band 1 Block=1000x1 Type=Float32, ColorInterp=Undefined
Min=11.550 Max=26.510
Minimum=11.550, Maximum=26.510, Mean=14.233, StdDev=0.837
NoData Value=-9999
STATISTICS_MAXIMUM=26.510000228882
STATISTICS_MEAN=14.233161129209
STATISTICS_MINIMUM=11.550000190735
STATISTICS_STDDEV=0.83723688736979

If you're using Linux, a short script can be set up to extract the STDDEV for mutiple files. In this case I'm running against hundreds of small ascii grid tiles of Open Lidar Data, and getting a separate stddev for each. You can simply change this to *.tiff; GDAL supports lots of raster formats.

#!/bin/bash
for i in \$( ls *.asc); do
gdalinfo -stats \$i | grep STDDEV
done

You should be able to do something similar on Windows if you're using that.

One method which I used quite a while ago was to print the standard deviations in the Python Console in QGIS. I would first load all the raster layers (easier if they're kept in the same directory):

import os, glob

layers=[]
raster_path = "C:\Users\You\Desktop\Raster_folder\\"   # Path to directory containing rasters

for raster in glob.glob(raster_path + "*.tif"):
layer = QgsRasterLayer(raster, os.path.basename(raster))
layers.append(layer)