Last active
July 3, 2019 18:27
-
-
Save srgorelik/85940e240a11298f209d762455faf7bc to your computer and use it in GitHub Desktop.
Print descriptive statistics of a raster or numpy array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples
To use a GeoTIFF on disk as the input:
To use a numpy array as the input: