Given a .tiff topographic raster image.
How to get the altitudes of the highest and lowest point/pixel ?
With Python, you can access raster statistics using the Python GDAL/OGR API.
from osgeo import gdal
# open raster and choose band to find min, max
raster = r'C:\path\to\your\geotiff.tif'
gtif = gdal.Open(raster)
srcband = gtif.GetRasterBand(1)
# Get raster statistics
stats = srcband.GetStatistics(True, True)
# Print the min, max, mean, stdev based on stats index
print "[ STATS ] = Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % (
stats[0], stats[1], stats[2], stats[3])
>>> print "[ STATS ] = Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % (
... stats[0], stats[1], stats[2], stats[3])
[ STATS ] = Minimum=1.000, Maximum=4.000, Mean=1.886, StdDev=0.797
>>>
With bash alone, you can use :
gdalinfo -mm input.tif
It returns a range of infos within which is the string Computed Min/Max=-425.000,8771.000
, for my Eurasian raster.
Some cleanup and you get your vertical min/max variables:
$zMin=`gdalinfo -mm ./input.tif | sed -ne 's/.*Computed Min\/Max=//p'| tr -d ' ' | cut -d "," -f 1 | cut -d . -f 1`
$zMax=`gdalinfo -mm ./input.tif | sed -ne 's/.*Computed Min\/Max=//p'| tr -d ' ' | cut -d "," -f 2 | cut -d . -f 1`
$echo $zMin $zMax
>-425 8771
I trimed both the digits after decimal point and the spaces in case via cut -d <separator> -f <selected_field>
and tr -d ' '
, respectively. Adapt as needed.
Min=-32.000 Max=202.000 Computed Min/Max=-58.000,205.000
. The computed values reflect the real content of the cells.
If the stats have already been calculated:
gdalinfo input.tif
If they haven't been calculated yet, do:
gdal_translate -stats input.tif output.tif
gdalinfo output.tif
gdal_translate -stats SRTM_NE_250m.tif
& gdalinfo -stats SRTM_NE_250m.tif
fails here. No min/max. Found with gdalinfo -mm SRTM_NE_250m.tif
and appears as Computed Min/Max=-425.000,8771.000
for my data.
gdalinfo SPOT4_HRVIR1_042-209_0_080831_111638.tif
gave metadata but no min-max. the same command with -mm produced the min-max. gdal_translate -stats SPOT4_HRVIR1_042-209_0_080831_111638.tif output.tif
produces a new file and gdalinfo output.tif
on that gives metadata with min-max values plus more
Commented
Mar 26, 2014 at 15:56
Using gdalinfo
and jq
you can get what you need in a succinct fashion. This assumes that band that has your min/max is the first one (change the index if not).
gdalinfo -json -mm input.tif | jq .bands[0].computedMin
gdalinfo -json -mm input.tif | jq .bands[0].computedMax
gdalinfo -json -mm input.tif | jq '.bands[0].computedMax'
Commented
Apr 6, 2023 at 12:18
For Maximum and Minimum Height I will Prefer this
from osgeo import gdal
raster = r'Path of your image'
gtif = gdal.Open(raster)
srcband = gtif.GetRasterBand(1)
stats = srcband.GetStatistics(True, True)
print ('[ STATS ] = Minimum=%.3f, Maximum=%.3f, Mean=%.3f' % (
stats[0], stats[1], stats[2]))