I'm writing a script that takes longitude and latitude and runs them through an executable called gdallocationinfo. The executable takes the latitude and longitude as its arguments and returns its STDOUT as the value for that coordinate. I've been reading about sub-processes and I was wondering if this is the most efficient way to implement this if I want to run alot of points. It seems to be taking a really long time.
def gdalgetpointdata(longitude,latitude):
proc = subprocess.Popen(["gdallocationinfo","C:\Users\data\pop","-wgs84","-valonly","{0}".format(longitude),"{0}".format(latitude)], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
return int(out)
Is there a better way to call this executable without having to make a new sub-process every time I run my function? Is there something I could do to speed it up?
As a side note I know that if you run the executable from the command line it will keep accepting STDIN and giving ouputs in STDOUT until you tell it to quit()
The utility can do multiple coordinates with one call. For example, prepare a simple
coords.txt
text file with coordinate pairs:Then pipe it in and out of
gdallocationinfo
from an OSGeo4W shell:which will make a
values.txt
file with the value for each coordinate. You can do the same with Popen with PIPE stdin and stdout arguments.values
will be a list of values, the same length ascoords
.