1

I would like to compute statistics for a multiband raster. How can I process?

This is my code:

python
    if os.path.isfile(_tmp_path_raster):
            dataset_raster = gdal.Open(_tmp_path_raster, GA_ReadOnly)
            bands = dataset_raster.RasterCountount()
            #doc : https://gis.stackexchange.com/questions/29064/how-to-get-gdal-to-create-statistics-for-gtiff-in-python
            for band in range (1, bands+1):
                band_stats = dataset_raster.GetRasterBand(band).GetStatistics(0, 1)
                bd= dataset_raster.GetRasterBand(band)
                bda= bd.ReadAsArray()
                a=np.array(bda)
                med_=np.median(a)
                print(med_)         
                _res += str(f) + ';' + str(band_stats[0]) + ';' + str(band_stats[1]) + ';' + str(band_stats[2]) + ';' + str(band_stats[3]) + ';' + str(med_) + '\n'

and I would like to have a result like this

Text enter image description here

Vince
  • 20,017
  • 15
  • 45
  • 64
sarah1904
  • 11
  • 2
  • It looks like you are nearly there! Does the code you shared output what you expect? Can you share that output? Do you need a solution using the rasterio library - or would you like to stick with the GDAL + numpy code you've shown here? – Charlie Parr Apr 04 '22 at 17:25
  • i don't have what i want yet, i woulf like to use gdal and numpy ; my rcode compute statisics only for one band , and i have two – sarah1904 Apr 04 '22 at 20:47

0 Answers0