[gdal-dev] How to locate where the raster min and max values are?

Scott public at postholer.com
Tue Oct 29 19:36:41 PDT 2024


It ain't pretty or efficient, but it's cheap. Here's min value. Remove 
'r' from sort for max value:

gdal2xyz.py -csv -skipnodata source.tif /dev/stdout | grep -v done | 
sort -rnk 3,3 -t "," | tail -1

result:
-116.9916667,36.54166667,46

On 10/29/24 17:31, Rahkonen Jukka via gdal-dev wrote:
> Hi,
> 
> I would like to know the georeferenced coordinates of the min and max 
> values of a DEM file. Even better if I could forward them into a vector 
> file. If the minimum or maximum happens to be on a flat area like seabed 
> I would be happy with the first pixel at the moment.
> 
> By copy-pasting from How do I open geotiff images with GDAL in Python? - 
> Stack Overflow 
> <https://stackoverflow.com/questions/41996079/how-do-i-open-geotiff-images-with-gdal-in-python> and How to find the indexes of the minimum or maximum value(s) in a matrix using python ? <https://en.moonbooks.org/Articles/How-to-find-the-indexes-of-the-minimum-or-maximum-values-in-a-matrix-using-python-/> I think I managed to get the correct points as numpy indexes
> 
>>>> import numpy as np
> 
>>>> from osgeo import gdal
> 
>>>> ds = gdal.Open('P3412A.tif', gdal.GA_ReadOnly)
> 
>>>> rb = ds.GetRasterBand(1)
> 
>>>> img_array = rb.ReadAsArray()
> 
>>>> vmin = img_array.min()
> 
>>>> vmax = img_array.max()
> 
>>>> vmin
> 
> -0.929
> 
>>>> vmax
> 
> 17.246
> 
>>>>
> 
>>>> np.where(img_array==vmin)
> 
> (array([1504], dtype=intg64), array([1189], dtype=int64))
> 
>>>> np.where(img_array==vmax)
> 
> (array([1545], dtype=int64), array([2423], dtype=int64))
> 
>>>>
> 
> But now I have no idea about how to get the georeferenced coordinates.
> 
> The task feels rather simple and I was sure that someone has already 
> made an utility or a QGIS plugin, but all I have found yet is for R. I 
> was thinking that perhaps some of the gdaldem modes could be misused for 
> this purpose, but I believe they cannot. For QGIS I found advice to use 
> an obvious but  clumsy method of polygonising the raster and finding the 
> extremes from the vector data. And one OpenJUMP developer took the 
> challenge and wrote a prototype with Java but it is not complete yet.
> 
> -Jukka Rahkonen-
> 
> 
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list