0

I have the below python script which executes my raster calculation perfectly using GDAL. However, I was wondering if there is a way to use gdal_calc without the path to gdal? gdal_path = r'"C:/Program Files/GDAL"'

import os

## Raster Calculator##
gdal_path = r'"C:/Program Files/GDAL"'
gdal_calc_path = os.path.join(gdal_path, 'gdal_calc.py')


# Arguements.
input_file_path = 'C:/Users/Public/try/singlebeam.tif'
input_file_path2 = 'C:/Users/Public/try/gdaltry.tif'
output_file_path = 'C:/Users/Public/try/pytry3.tif'
calc_expr = '"(A == 0) * B + (A != 0) * A"'
typeof = '"Float32"'

# Generate string of process.
gdal_calc_str = 'python {0} -A {1} -B {2} --outfile={3} --calc={4} --type={5} --hideNoData'
gdal_calc_process = gdal_calc_str.format(gdal_calc_path, input_file_path, input_file_path2,
    output_file_path, calc_expr, typeof)

# Call process.
os.system(gdal_calc_process)
1

1 Answer 1

0

If the path to the GDAL executables is on your system path, then you should be able to just call gdal_calc.py from anywhere, including in the os.system() call you're making.

Here's a website that says how to do it on Windows 10, but please search for how to do it on whatever OS you're on. Search for "Add to the system path" or similar.

2
  • once I have added it to my system path, how should I call it in my code correctly?
    – user196963
    Dec 3, 2021 at 0:30
  • 1
    If it's accessible through the system path, then you don't need to prepend an explicit path and could use gdal_calc_path = 'gdal_calc.py' instead.
    – Dave X
    Dec 3, 2021 at 0:43

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.