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.

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()
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()
3

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

subprocess to call gdal from within python

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()
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
    – user28542
    Commented Feb 2, 2021 at 1:22

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.