13

Given a .tiff topographic raster image.

How to get the altitudes of the highest and lowest point/pixel ?

Hugolpz
  • 2,653
  • 3
  • 26
  • 51

5 Answers5

19

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
>>> 
Hugolpz
  • 2,653
  • 3
  • 26
  • 51
Aaron
  • 51,658
  • 28
  • 154
  • 317
  • 1
    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! – Ahmed Fasih Apr 27 '16 at 20:23
17

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.

Hugolpz
  • 2,653
  • 3
  • 26
  • 51
  • 3
    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. – AndreJ Mar 27 '14 at 08:43
5

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
Hugolpz
  • 2,653
  • 3
  • 26
  • 51
mercergeoinfo
  • 784
  • 4
  • 15
  • The altitude info is not there. Did you test it successfully ? – Hugolpz Mar 24 '14 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). – mercergeoinfo Mar 24 '14 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. – Hugolpz Mar 24 '14 at 20:16
  • The max value of 8771 looks correct with a 250m pixel size. – HeikkiVesanto Mar 24 '14 at 20:21
  • @Vesanto: I unvalidated your answer for the moment, since neither 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
  • OK, gdal_translate -stats requires you create a new file, you can also set the nodata value at the same time. But given that gdalinfo -mm *.tif worked it would seem that the nodata value was already set. -mm is fine for a quick check but it solves one problem, once; calculating the stats properly will give you greater flexibility later. – mercergeoinfo Mar 24 '14 at 21:39
  • Can you include the precises serie of commands you do ? Nor 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
  • 1
    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 – mercergeoinfo Mar 26 '14 at 15:56
  • 1
    I've posted a more complete example on my blog (http://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. – mercergeoinfo Mar 26 '14 at 20:19
  • Would be good to do more solid answers with exact and full commands such 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
  • I've put colour highlighting on the commands. Most of the text is the output from 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
  • This takes the long way around. Gdalinfo can calculate and record the stats in an .aux file without needing to create a new raster: 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
  • #pro-tip: if you're shrinking raster storage, e.g. by converting that huge 30+GB 32bit float to nice manageable 100MB 8bit, compute stats after using 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
3

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

Marc Miles
  • 69
  • 2
-1

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]))
nmtoken
  • 13,355
  • 5
  • 38
  • 87