6

I'm using gdal_translate to downsample some data using averaging. The input data uses an Int data type, and I want to store the float values of the pixel averaging.

However, even when I force gdal_translate to use Float32, the values in the output file have been cast to int. The command I'm using is:

gdal_translate -tr 0.0075 0.0075 -r average -ot Float32 -co COMPRESS=LZW -q in.tif out.tif

When I use the same options for gdalwarp, I get the expected result (float values).

gdalwarp -tr 0.0075 0.0075 -r average -ot Float32 -co COMPRESS=LZW -q in.tif out.tif

Am I missing something, or is this a bug in gdal_translate?

2
  • I believe the same thing applies with gdal_calc.py, where you need to convert image to float explicitly (in expression, e.g. --calc="A.astype(float)"). Since it calculates expression first and only after that converts resulting image into -ot type
    – dr_times
    Jul 7, 2017 at 12:43
  • The order of the options does not make a difference (nor should it!) @dr_times Yes, I've encountered that with gdal_calc.py; I wondered if it might be a similar issue. However there's no similar ability to force casting for gdal_translate. Jul 7, 2017 at 13:27

1 Answer 1

3

I understand this is a bit hard to understand, but this is an expected result given the design of both utilities. In gdal_translate, the resampling is done with the input datatype (so Int), the conversion to the output data type is done late in the process. In gdalwarp, -ot Float32 implies a -wt Float32 (working datatype), hence more expected results.

1
  • Yes, I noticed the -wt option for gdalwarp. AFAICT there's no equivalent option for gdal_translate - correct? Jul 10, 2017 at 9:36

Your Answer

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

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