I can obtain the maximum/minimum value of a raster via the ComputeRasterMinMax code:

  using (var ds = Gdal.Open(tiffFile, Access.GA_ReadOnly))
    var nCol = ds.RasterXSize;
    var nRow = ds.RasterYSize;
    var allValues = new int[nCol * nRow];
    Band band = ds.GetRasterBand(1);
    band.ReadRaster(0, 0, nCol, nRow, allValues, nCol, nRow, 0, 0);   
    double[] minMax = new double[2];
    band.ComputeRasterMinMax(minMax, 0);


As per documentation, minMax[0] is the minimum value, and minMax[1] is the maximum value of the band.

But how to get the cell that these maximum and minimum occurs, besides actually looping through the array and compare the item one by one?


you could get the value of the "max" cell, reclassify everything that isn't that value to NA, convert the raster to polygon, then find the centroid of the polygon. it's the gorilla approach.

  • Can you show some code snippet how this can be done ? – Graviton Jul 12 '19 at 22:25

You can return the max pixel of a raster simply using gdal_calc as follows:

gdal_calc.py -A  input_raster.tif --calc="(A==max(A.flatten()))*1" --outfile=max_pixel.tif --overwrite

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.