6

I'm trying to find the cell value at various locations in a set of rasters so I can count the number of real values (aka not 'NoData') that occur at all the locations within each raster. The various locations are represented as points in a point shapefile. I exported the shapefile's attribute table (including lat/long) to a csv and am looping over each point to get the value. I have a script ready to go, but am having trouble storing the output of the 'gdallocationinfo' as a result.

How can I do this in python?

Here is my pseudo code:

for raster in rasters:
 count = 0
 for i in range(0, Npts):
 lat = points[i][10] # lat is 10th row of csv
 lon = points[i][9] # longitude is 9th row of csv
 result = os.system('gdallocationinfo -valonly -wgs84 %s %s %s' % (raster, lon, lat))
 print result
 if not result == 'nan': count+=1 # if result is no 'nan', add to the count
 row = [raster, str(count)]
 # write row to csv

When I run the actual code, the system call for 'gdallocationinfo' prints out the correct values (or 'nan'). But when it gets to 'print result', it just prints 0. So the command call is giving me the correct result, but this obviously is not the way to store the result as a variable so I can use it later. I'm calling the python code from the linux command line.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 15, 2014 at 14:28

4 Answers 4

6

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()
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Nov 17, 2017 at 16:33
5

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()
answered Oct 15, 2014 at 17:19
3

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

subprocess to call gdal from within python

answered Oct 15, 2014 at 15:07
0
0

To speed up the process you could use gdallocationinfo with input/output redirection. Export all your lat/lon values to input.csv and read the corresponding cell values from output.csv:

os.system(r'gdallocationinfo -valonly -wgs84 "%s" <%s >%s' % ('dhm.tif','input.csv','output.csv'))
with open('output.csv','r') as f:
 result = f.read()
answered Dec 29, 2019 at 8:39
1
  • Can anyone suggest how to write this value in a text file in which already 8 values of each :9f .I tried to write in a combined string this gdallocation info value with file.write( str(long)+str(lat)+str(val1)+str(val2)++str(val2)+str(val3)+str(val4)+str(val5)+str(val6)+str(val7)+str(val8)+str(val9)+str(val10)) where this val10 is from gdalocationinfo.which of length 2 digits.its getting printed in separate line like below. Textfile: 65.5,23.5,0.00004,0.000000005,0.000000789,0.004,0.0006,0.0000006,0.00005,13, 20 Commented Feb 2, 2021 at 1:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.