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

Rahkonen Jukka jukka.rahkonen at maanmittauslaitos.fi
Wed Oct 30 05:24:39 PDT 2024


Hi,

Thank you everybody for the good and working suggestions.

-Jukka Rahkonen-

Lähettäjä: gdal-dev <gdal-dev-bounces at lists.osgeo.org> Puolesta Ujaval Gandhi via gdal-dev
Lähetetty: keskiviikko 30. lokakuuta 2024 8.44
Vastaanottaja: Scott <public at postholer.com>
Kopio: gdal-dev at lists.osgeo.org
Aihe: Re: [gdal-dev] How to locate where the raster min and max values are?

Another approach using rasterio

import rasterio
import numpy as np

dataset = rasterio.open('P3412A.tif')
band1 = dataset.read(1)
rows, cols = np.where(band1 == np.max(band1))
lon, lat = rasterio.transform.xy(dataset.transform, row[0], col[0])
print(lat, lon)
---
Ujaval Gandhi
Spatial Thoughts
www.spatialthoughts.com<https://mailtrack.io/l/79bb80f67e4a9e655cddd40fc6a12779870dac63?w=Z2RhbC1kZXZAbGlzdHMub3NnZW8ub3Jn&url=http%3A%2F%2Fwww.spatialthoughts.com&u=8747767&signature=198a984b1b993e73>




On Wed, Oct 30, 2024 at 8:06 AM Scott via gdal-dev <gdal-dev at lists.osgeo.org<mailto:gdal-dev at lists.osgeo.org>> wrote:
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<mailto:gdal-dev at lists.osgeo.org>
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org<mailto:gdal-dev at lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/gdal-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20241030/bd738455/attachment-0001.htm>


More information about the gdal-dev mailing list