2

I would like to print the raster statistics from its header. This can be done in gdal using the commands.

gdalinfo dataset.tif

or in Python bindings

src = gdal.Open('dataset.tif')
info = gdal.Info(src)
print(info)

Is there a similar workflow in rasterio to print all raster information?

import rasterio 

with rasterio.open('dataset.tif') as src:
   # print info

I don't want to use numpy to get raster statistics since it would be computationally expensive in large data.

1 Answer 1

2

Try this:

import rasterio
f = "C:\GIS\athreebandraster.jpg"
dataset = rasterio.open(f)

meta=dataset.meta
for k,v in meta.items():
    print(k,': ', v)
print("\n")

for band in range(1, meta["count"]+1):
    stats = dataset.statistics(bidx=band, approx=True) #min, max, mean, std
    print(f"Stats for band {band}: ", stats)
dataset.close()

Prints:

driver :  GTiff
dtype :  uint8
nodata :  None
width :  7000
height :  15700
count :  3
crs :  EPSG:3006
transform :  | 100.00, 0.00, 240000.00|
| 0.00,-100.00, 7675000.00|
| 0.00, 0.00, 1.00|


Stats for band 1:  Statistics(min=0.0, max=254.0, mean=207.26144671202, std=50.968567671799)
Stats for band 2:  Statistics(min=0.0, max=254.0, mean=220.19104648526, std=51.082257094833)
Stats for band 3:  Statistics(min=0.0, max=254.0, mean=214.21241836735, std=58.629669435145)
2
  • Hi @BERA, does it compute statistics whenever its called?
    – Nikko
    Commented Sep 7, 2023 at 7:48
  • 1
    Thank you very mcuh!
    – Nikko
    Commented Sep 7, 2023 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.