Python – Using gdal_calc without Specifying GDAL Path

gdalpython

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)

Best Answer

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.

Related Question