0

i try to use python GDAL raster calculation to take some raster result where i need.

i windows user and i work in OSGeo4W Shell.

first i try to work using batch file with success.

code work with batch file :

import os
os.system("C:/xxx/xxx/path/test.bat ")

batch file :

gdal_calc.py -A C:/xxx/xxx/path/a_layer.tif -B C:/xxx/xxx/path/a_layer.tif --outfile=C:/xxx/xxx/path/a_layer.tif --calc="(2*A)+(2*B)"

all work without error.

but if i try to wotk using python then any time before complete my script then python.exe crashed.any time

try using python script :

call('python C:/OSGeo4W64/bin/gdal_calc.py -A {0} -B {1} -C {2} --outfile={3} --calc="(2*A)+(2*B)"'.format(a_layer,b_layer,out_layer))

i want to use python script because i have many options for working usnig python and gdal.

but can someone to tell me why batch file work and not python script ?

edit

if print 'PATHEXT' environment variable in OSGeo4W Shell i dont have '.PY' extesion.but if is this hte problem can i define 'PATHEXT' inside some python script ?

3
  • Do you get an error message, if so what is it? IMO writing the batch file and executing is better, but I use subprocess.Popen to run the scripts as call can be flaky, something to do with paths not being set right I think. You could try to execute with subprocess.Popen but your string needs to be a list. Nov 22, 2017 at 21:24
  • @Michael Stimson just take error message stop working python.exe .can you show me some example using subprocess.Popen ?
    – hellas
    Nov 22, 2017 at 21:38
  • 1
    You should do these calculations directly with osgeo.gdal and NumPy; see the example for CreateCopy() to get an idea how to do this in a more Python way
    – Mike T
    Nov 23, 2017 at 0:59

1 Answer 1

1

This is part of a much bigger process

import os, sys, subprocess

InFolder = sys.argv[1]
OutFDS   = sys.argv[2]
DatumAdj = sys.argv[3]

DatAdjust     = r'd:\path\to\AdjustToDatum.py'
PythonEXE     = r'C:\path\to\python.exe'

TempName = 'xx' + fNam + '.shp'
TempFC   = os.path.join(InFolder,TempName)
thisID   = 1

proc = subprocess.Popen([PythonEXE, DatAdjust,TempFC,DatumAdj,str(thisID)])
proc.communicate() # wait until process ends

This shows calling a python script with subprocess.Popen successfully. I have very much simplified this process; in my script I create a list of process objects and poll them in a loop adding a new process as the finish so that a maximum number of processes execute at the same time.

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.