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

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
>>>
• I used to sneer when reading answers using the Python GDAL/OGR API, thinking "Why would anyone suggest that when there's a perfectly good command-line way to do the same?" Now, that I have to programmatically do nearly everything, I'm super-grateful to answers like yours. Thanks! Apr 27, 2016 at 20:23

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.

• Strangely, this does not have to be the same as the metadata. For an original SRTM hgt file: Min=-32.000 Max=202.000 Computed Min/Max=-58.000,205.000. The computed values reflect the real content of the cells. Mar 27, 2014 at 8:43

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
• The altitude info is not there. Did you test it successfully ? Mar 24, 2014 at 20:04
• That should be the MINIMUM and MAXIMUM values returned by gdalinfo. You may have a problem if the NODATA value hasn't been set, in which case you may have a very low MINIMUM value (rarely a very high MAXIMUM instead). Mar 24, 2014 at 20:11
• 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. Mar 24, 2014 at 20:16
• Well, I don't have room to show all the details but using a SPOT4 scene: 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 Mar 26, 2014 at 15:56
• I've posted a more complete example on my blog (mercergeoinfo.blogspot.com/2014/03/gdalinfo-and-statistics.html). If that's against the rules here then I apologise now. Feel free to remove this if it is the case. Mar 26, 2014 at 20:19

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
• I needed to quote the jq selector gdalinfo -json -mm input.tif | jq '.bands[0].computedMax' 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]))