1

I perform some calculations on a set of approx. 700 rasters. The calculation works in my loop, but the new raster files are now 500 MB instead of 2 MB before the calculations. By default, there is no copression set in the GDAL raster calculator. I have tried to add the compression information to the 'OPTIONS' input, but it does not work:

    calc_expression = '(255>A>={0})*A)'.format(some_value)
    parameters = {'INPUT_A' : raster_layer,
        'BAND_A' : 1,
        'FORMULA' : calc_expression,
        'RTYPE': 2,
        'NO_DATA': 0,
        'OPTIONS': '--co="COMPRESS=LZW"',
        'OUTPUT' : output_raster}

    processing.runAndLoadResults('gdal:rastercalculator', parameters)

I have also tried by calling gdal_calc.py directly as follows:

    pythonpath = 'C:/Program Files/QGIS 3.10/apps/Python37/Scripts/gdal_calc.py'
    gdal_calc = 'python {0} ' \
        '-A {1} ' \
        '--outfile={2} ' \
        '--calc={3} ' \
        '--overwrite'.format(pythonpath, raster_file, output_raster, calc_expression)

    os.system(gdal_calc)

But this won't create any raster at all (giving "1" as output).

Can anybody help me to set the compression option in the first code snippet correctly (priority 1), or alternatively to get the code in the 2nd snippet running (priority 2)?

3
  • Have you checked if the output raster is LZW compressed? If it is not try with single dash -co compress=lzw.
    – user30184
    Commented Feb 10, 2021 at 12:22
  • 1
    Thanks for the hint! Output raster had compression = None. However, I solved the problem by changing the variable to 'OPTIONS': 'COMPRESS=LZW'.
    – GeoBaer
    Commented Feb 12, 2021 at 14:24
  • If you solved your question, you can add the solution as an answer and mark it as valid answer. That's how we properly close questions in this site, making it easier for future readers to access the answer. Commented Jun 4, 2021 at 13:38

2 Answers 2

2

Thanks for all the inputs from your side.

I finally solved the problem by changing the variable to 'OPTIONS': 'COMPRESS=LZW'.

0

If the output is used thru web servers the best format and compression options are to make a cloud optimized geotiff COG and use oprions COMPRESS=DEFLATE PREDICTOR=YES (or 2 for integers and 3 for floats). BIGTIFF=IF_SAFER if the 4 GB file size could be surpassed.

Your Answer

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

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