Storing result from gdallocationinfo as variable in Python

You can capture standard out this way:

from subprocess import Popen, PIPE

cmd = ['gdallocationinfo', '-geoloc', '-valonly', src, lng, lat]
p = Popen(cmd, stdout=PIPE)
p.wait()
val = p.stdout.read()

I did a little more digging and found the answer to my question:

instead of using os.system, the correct syntax for storing the result in a variable is:

result = os.popen('gdallocationinfo -valonly -wgs84 %s %s' % (lyr, loc)).read()

I think you will have to use the subprocess syntax, it is explained in this post:

subprocess to call gdal from within python

Tags:

Python

Linux

Gdal