Given a .tiff topographic raster image.
How to get the altitudes of the highest and lowest point/pixel ?
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.
– AndreJ
Mar 27 '14 at 08: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
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.
– Hugolpz
Mar 24 '14 at 20:16
gdal_translate nor gdalinfo -stats work here. Jump in, edit your answer with gdalinfo -mm filename.tif and so I could valide it again.
– Hugolpz
Mar 24 '14 at 21:00
gdal_translate -stats nor gdal_translate -stats SRTM_NE_250m.tif don't work here, and don't create any file.
– Hugolpz
Mar 26 '14 at 15:44
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
– mercergeoinfo
Mar 26 '14 at 15:56
gdal_translate -stats input.tif output.tif, gdalinfo output.tif, and shorter wording. That avoid unclear answer, series of comments to clarify, while link to your blog will be better accepted.
– Hugolpz
Mar 27 '14 at 07:22
gdalinfo and the 'short' version is a couple of comments above here. There isn't any more to it really, gdal_translate -stats fileIn.tif fileOut.tif then gdalinfo fileOut.tif
– mercergeoinfo
Mar 27 '14 at 14:12
gdalinfo -stats someraster.tif (this may not have been the case when this answer was first posted). Also see How to get GDAL to create statistics for GTiff in Python
– matt wilkie
Aug 02 '16 at 17:38
gdalinfo -stats ... and not during conversion. gdal_translate -stats ... computes statistics at the beginning before anything else, wasting a LOT of time.
– matt wilkie
Aug 02 '16 at 22:05
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'
– Joe Beuckman
Apr 06 '23 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]))