Skip to content

Instantly share code, notes, and snippets.

@srgorelik
Last active July 3, 2019 18:27
Show Gist options
  • Save srgorelik/85940e240a11298f209d762455faf7bc to your computer and use it in GitHub Desktop.
Save srgorelik/85940e240a11298f209d762455faf7bc to your computer and use it in GitHub Desktop.
Print descriptive statistics of a raster or numpy array.
#!/usr/bin/env python3
from osgeo import gdal
import numpy as np
import pandas as pd
def stats(raster, band = 1, nodata = None):
"""Print descriptive statistics of a raster or numpy array"""
if (type(raster) != str) and (type(raster) != np.ndarray):
print("Error: input must be a raster filepath or numpy array.")
return
elif (type(band) != int) or (band <= 0):
print("Error: band selection must be a positive integer.")
return
elif type(raster) == str:
file = gdal.Open(raster)
img = np.array(file.GetRasterBand(band).ReadAsArray())
if nodata is None: nodata = file.GetRasterBand(band).GetNoDataValue()
file = None
elif len(raster.shape) == 3:
if raster.shape[0] < band:
print("Error: there are only {} bands.".format(raster.shape[0]))
return
else:
img = raster[band - 1]
else:
img = raster
num_rows, num_cols = img.shape
img_data = img[img != nodata]
del img
img_min = '{0:.2f}'.format(img_data.min())
img_max = '{0:.2f}'.format(img_data.max())
img_mean = '{0:.2f}'.format(img_data.mean())
img_std = '{0:.2f}'.format(img_data.std())
stats = {
'Band': [band],
'Cols': [num_cols],
'Rows': [num_rows],
'Min': [img_min],
'Max': [img_max],
'Mean': [img_mean],
'Std': [img_std],
'NoData': [nodata]
}
df = pd.DataFrame.from_dict(stats)
df.index = ['']
print(df)
return
@srgorelik
Copy link
Author

srgorelik commented Oct 18, 2018

Examples

To use a GeoTIFF on disk as the input:

>>> f = '~/example.tif'
>>> stats(f)
  Band   Cols   Rows   Min     Max   Mean    Std NoData
     1  40000  40000  0.00  100.00  51.14  49.21   None
>>>

To use a numpy array as the input:

>>> import numpy as np
>>> arr = np.array([[7,0,5,0],[4,0,3,9],[0,1,8,0]])
>>> print(arr)
[[7 0 5 0]
 [4 0 3 9]
 [0 1 8 0]]
>>> stats(arr)
  Band  Cols  Rows   Min   Max  Mean   Std NoData
     1     4     3  0.00  9.00  3.08  3.30   None
>>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment