2

i have a series of ndvi images that i am trying to normalize with gdal to run some calculations, but i think i'm missing something.

per: https://github.com/OSGeo/gdal/blob/master/gdal/swig/include/python/gdal_python.i

scaleParams --- list of scale parameters, each of the form [src_min,src_max] or [src_min,src_max,dst_min,dst_max]

my original stats are

[ STATS ] =  Minimum=-0.194, Maximum=0.459, Mean=0.103, StdDev=0.073

i set my newmin and newmax to 0 and 1

then i

my_options = gdal.TranslateOptions(scaleParams = [[-0.194, 0.459,newmin, newmax]], format = 'GTiff',
                                      creationOptions = ['TFW=YES', 'COMPRESS=LZW']

gdal.Translate(gdaloutput, my_raster, options=my_options)

yet my output is

[ STATS ] =  Minimum=-30.148, Maximum=105.483, Mean=26.477, StdDev=18.837

I am getting the statistics with

srcband = gtif.GetRasterBand(1)

# Get raster statistics
stats = srcband.GetStatistics(True, True)

I had thought this was the correct way to get the statistics? https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html#loop-through-all-raster-bands

i tried with a different raster and changed my min and max to floats, from 0 and 1 to 0.0 and 1.0. which gave me

[ STATS ] =  Minimum=0.226, Maximum=1.083

from the original of

[ STATS ] =  Minimum=-0.211, Maximum=0.439

I also read somewhere about avoid setting the new max higher or the new min lower than the original, so following that, setting new min to 0 and new max to 0.439, for the new raster

i got

[ STATS ] =  Minimum=0.099, Maximum=0.475

which places my new max higher than the existing.

What am i doing wrong?

Edit:

without the double brackets

Changing

[[src_min,src_max,dst_min,dst_max]] to [src_min,src_max,dst_min,dst_max]

you get

TypeError: 'float' object is not iterable

both

ComputeRasterMinMax()  and ComputeStatistics()

returns

(-0.19442784786224365, 0.4585714042186737)
2
  • I would first check that the reported min and max are correct and not a fast estimate that is computed from a sample of pixels.
    – user30184
    Feb 2, 2020 at 16:16
  • It might be worth trying two other methods gdal.org/python/osgeo.gdal.Band-class.html: ComputeRasterMinMax or ComputeBandStats.
    – user30184
    Feb 2, 2020 at 20:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.