Created
October 7, 2021 14:34
-
-
Save ghelobytes/b156e204164aa09c3b8efa066bb3f62b to your computer and use it in GitHub Desktop.
geotiff sanity check
This file contains hidden or 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
from osgeo import gdal | |
raster_file = "some_geotiff.tif" | |
print("gdal.__version__:", gdal.__version__) | |
print("############ Get STATS using GetStatistics() ############") | |
ds = gdal.Open(raster_file) | |
band_count = ds.RasterCount | |
print("band_count:", band_count) | |
for band in range(1, band_count + 1): | |
stats = ds.GetRasterBand(band).GetStatistics(0, 1) | |
print("#############") | |
print("band:", band) | |
print("min:", stats[0]) | |
print("max:", stats[1]) | |
print("mean:", stats[2]) | |
print("stddev:", stats[3]) | |
print("############ Get STATS using gdal.Info() ############") | |
result = gdal.Info(raster_file, options=gdal.InfoOptions(stats=True)) | |
print("result:", result) | |
print("############ Get STATS some other way... ############") | |
a = ds.ReadAsArray() | |
for band in range(a.shape[0]): | |
band_data = a[band, ...] | |
for stat in ['min', 'max', 'mean']: | |
print(f"Band {band + 1} {stat}: {getattr(band_data, stat)()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment