4

I'm writing a Python script that aims to re-project various LANDSAT tiles into a common projection and then import the tiles into Grass. The script at present is shown below:

#!/usr/bin/python
import os
import sys
import glob
import grass.script as grass
def import_tifs(dirpath):
 for dirpath, dirname, filenames in os.walk(dirpath):
 for tile in filenames:
 if tile.upper().endswith('.TIF'):
 full_path = os.path.join(dirpath, tile) 
 name = os.path.splitext(tile)[0] 
 rename = name[:-4] + '.' + name[-2]
 new_path = os.path.join(dirpath, rename)
 re_project = 'EPSG:32643'
 grass.message('Importing %s -> %s@%s...' % (tile, name, dirpath)) 
 grass.run_command('gdalwarp',
 '-t_srs',
 re_project,
 full_path,
 new_path)
 grass.run_command('r.in.gdal',
 flags = 'e',
 input = full_path,
 output = name,
 quiet = True,
 overwrite = True)
def main(): 
 if len(sys.argv) == 1: 
 for directory in filter(os.path.isdir, os.listdir(os.getcwd())):
 import_tifs(directory)
 else:
 import_tifs(sys.argv[1]) 
if __name__ == "__main__":
 main()

I'm having real trouble using the 'gdalwarp' command to reproject the LANDSAT tiles. I keep getting the following error:

Traceback (most recent call last):
 File "C:/Users/Simon/Documents/import_landsat3.py", line
76, in <module>
 main()
 File "C:/Users/Simon/Documents/import_landsat3.py", line
73, in main
 import_tifs(sys.argv[1])
 File "C:/Users/Simon/Documents/import_landsat3.py", line
31, in import_tifs
 new_path)
 File "C:\GRASS64\etc\python\grass\script\core.py", line
189, in run_command
 ps = start_command(*args, **kwargs)
 File "C:\GRASS64\etc\python\grass\script\core.py", line
167, in start_command
 args = make_command(prog, flags, overwrite, quiet,
verbose, **options)
 File "C:\GRASS64\etc\python\grass\script\core.py", line
124, in make_command
 raise ScriptError("'-' is not a valid flag")
grass.script.core.ScriptError: "'-' is not a valid flag"

I'm assuming this is related to '-t_srs' but I haven't been able to work out a way around the problem - can anyone suggest how to do this?

Many thanks.

asked Oct 29, 2012 at 19:27

2 Answers 2

3

Have you tried calling gdalwarp using os.sys? Check this post for more details on the topic.

answered Oct 29, 2012 at 19:35
3
  • Thanks - I've given this a try, but have got the error: TypeError: 'module' object is not callable What module do I need to import to use gdalwarp with os.sys in a Python script? Commented Oct 29, 2012 at 22:12
  • what about trying os.system('gdalwarp ... ')? Commented Oct 30, 2012 at 8:02
  • 1
    Thanks - this has worked! Could you tell me why os.system worked and os.sys didn't? Commented Oct 30, 2012 at 8:37
2

Try the subprocess module:

import subprocess
cmd = ['gdalwarp','-t_srs',re_project,full_path,new_path]
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout,stderr=proc.communicate()
exit_code=proc.wait()
if exit_code: #Oops, something went wrong!
 raise RuntimeError(stderr)
else:print stdout
answered Oct 30, 2012 at 4:41
2
  • Thanks for this - I've managed to get the code to work using os.system as suggested by @dmci but I'm going to try using the subprocess module as I haven't come across it before. Commented Oct 30, 2012 at 8:39
  • 1
    From the python docs for os.system: "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." Commented Oct 30, 2012 at 19:54

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.