error with gdalbuildvrt, in Python
Question: I am new to python/GDAL and am running into perhaps a trivial issue. This may
stem from the fact that I don't really understand how to use GDAL properly in
python, or something careless, but even though I think I am following the help
doc, I keep getting a syntax error when trying to use "gdalbuildvrt".

What I want to do is take several (amount varies for each set, call it N)
geotagged 1-band binary rasters [all values are either 0 or 1] of different
sizes (each raster in the set overlaps for the most part though), and "stack"
them on top of each other so that they are aligned properly according to their
coordinate information. I want this "stack" simply so I can sum the values and
produce a 'total' tiff that has an extent to match the exclusive extent
(meaning not just the overlap region) of all the original rasters. The
resulting tiff would have values ranging from 0 to N, to represent the total
number of "hits" the pixel in that location received over the course of the N
rasters.

I was led to gdalbuildvrt [http://www.gdal.org/gdalbuildvrt.html] and after
reading about it, it seemed that by using the keyword -separate, I would be
able to achieve what I need. However, each time I try to run my program, I get
a syntax error. The following shows two of the several different ways I tried
calling gdalbuildvrt:

    
    
    gdalbuildvrt -separate -input_file_list stack.vrt inputlist.txt
    gdalbuildvrt -separate stack.vrt inclassfiles
    

Where inputlist.txt is a text file with a path to the tif on every line, just
like the help doc specifies. And inclassfiles is a python list of the
pathnames. Every single time, no matter which way I call it, I get a syntax
error on the first word after the keywords (i.e. 'inputlist' in inputlist.txt,
or 'stack' in stack.vrt).

Could someone please shed some light on what I might be doing wrong?
Alternatively, does anyone know how else I could use python to get what I
need?

Thanks so much.


Answer: `gdalbuildvrt` is a GDAL command line utility. From your example its a bit
unclear how you actually run it, but when running from within Python you
should execute it as a subprocess.

And in your first line you have the `.vrt` and the `.txt` in the wrong order.
The textfile containing the files should follow directly after the
`-input_file_list`.

From within Python you can call `gdalbuildvrt` like:

    
    
    import os
    os.system('gdalbuildvrt -separate -input_file_list inputlist.txt stack.vrt')
    

Note that the command is provided as a string. Using a Python list with the
files can be done with something like:

    
    
    os.system('gdalbuildvrt -separate stack.vrt %s') % ' '.join(data)
    

The `' '.join(data)` part converts the list to a string with a space between
the items.

Depending on how your GDAL is build, its sometimes possible to use wildcards
as well:

    
    
    os.system('gdalbuildvrt -separate stack.vrt *.tif')
    

