简体   繁体   中英

Determine coordinates at highest point of raster

I have a raster from which I want to derive the coordinates of the highest point (elevation) in the raster.

Getting the highest elevation is easy, but I don't know how to get its coordinates.

What I have so far:

# required modules
from osgeo import gdal
from osgeo import osr
import numpy as np
import rasterio

# allow GDAL to use python exceptions
gdal.UseExceptions()

# save paths to the files needed
input_raster = 'data/dem.tif' 

# open input raster file with errorcatching
try:
    ds = gdal.Open(input_raster)
except RuntimeError as err:
    print (err)
    exit(keep_kernel=True)

if ds is None:
    print ('Unable to open %s' % input_raster)
    exit(keep_kernel=True)

#access size of file
cols = ds.RasterXSize
rows = ds.RasterYSize

#access band and data as numpy arrays
band = ds.GetRasterBand(1)
data1 = band.ReadAsArray(0, 0, cols, rows).astype(float)

#set nodata values to Nan
nodata_val = band.GetNoDataValue()
print(nodata_val)

data_masked = np.ma.masked_equal(data1,nodata_val)


#determine highest elevation value und its coordinates
highest_val = data_masked.max()

geotransform = ds.GetGeoTransform()
originX     = geotransform[0]
originY     = geotransform[3]
pixelWidth  = geotransform[1]
pixelHeight = geotransform[5]

I'm stuck and thankful for any advice.

Get the indices of the max value(s):

indices = np.where(data_masked == data_masked.max())

Also decide what to do when there are multiple cells with the maximum value.

Compute the coordinates with the transforms:

x = indices[0][0] * pixelWidth + originX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM