Gdal: how to get the max and min altitudes of my topographic raster?

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.


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