1

I am now want to use subprocess.call with gdalwarp to clip raster from shapefile (in qgis 1.8.0)

I use raster→projections→wrap(reproject)to generate command_line below

gdalwarp -t_srs "+proj=tmerc +lat_0=0 +lon_0=121 +k=0.9999 +x_0=250000 +y_0=0
+ellps=GRS80 +units=m +no_defs" -dstnodata 0 -q -cutline "C:/Program Files
(x86)/Quantum GIS Wroclaw/test/area.shp"
-dstalpha -of GTiff C:/Program Files
(x86)/Quantum GIS Wroclaw/test/wrapM_0.tif "C:/Program Files
(x86)/Quantum GIS Wroclaw/test/wrap"

and following is my code

import gdal
from subprocess import call


crs='"+proj=tmerc +lat_0=0 +lon_0=121 +k=0.9999 +x_0=250000 +y_0=0
+ellps=GRS80 +units=m +no_defs"'

mask='"C:/Program Files(x86)/Quantum GIS Wroclaw/test/area.shp"'   
input='C:/Program Files(x86)/Quantum GIS Wroclaw/test/wrapM_0.tif'
output='"C:/Program Files(x86)/Quantum GIS Wroclaw/test/wrap"'

call(gdalwarp, -t_srs crs -dstnodata 0 -q -cutline msak -dstalpha -of GTiff
input output)

And Python will give the error below:

  File "<input>", line 1
    call(gdalwarp, -t_srs crs -dstnodata 0 -q -cutline msak -dstalpha -of
GTiff input output)
                            ^
SyntaxError: invalid syntax

How to fix the error here??

6
  • 1
    Your call function should be like that: call(['gdalwarp', '-t_srs ' + crs, '-dstnodata 0', '-q', '-cutline ' + mask, '-dstalpha', '-of GTIFF', input, output]). I didn't test your code, but is it possible that you missed the double quotes for the Input variable. You use a directory that contains blanks.
    – Saleika
    Commented May 6, 2013 at 6:18
  • 1
    An alternatve to the code above should be: call('gdalwarp -t_srs ' + crs + ' -dstnodata 0 -q -cutline ' + mask + ' -dstalpha -of GTIFF ' + input + ' ' + output, shell=True).
    – Saleika
    Commented May 6, 2013 at 6:22
  • thanks for your comment!it's worked!but I still have question that I can't find my output shapefile? when I use the plugin it's will give me a file (no extend file name) but after I use call(),it's just flash and nothing left! Commented May 6, 2013 at 6:50
  • oh it's return 1 in the end of code means it's can't complete the command here but i can run it perfectly via plugin... Commented May 6, 2013 at 7:19
  • What do you mean with "output shapefile"? Your output file Format is GeoTiff (-of GTiff). You didn't add an extension to your GeoTiff output file in your commandline.
    – Saleika
    Commented May 6, 2013 at 7:54

2 Answers 2

4

You have to specifiy the path to gdalwarp (in the example below I took: "C:\python32\python.exe", but you have to change it to your intallation path) and change your call function like that:

call(['C:\Program Files\GDAL\gdalwarp.exe', '-t_srs ' + crs, '-dstnodata 0', '-q', '-cutline ' + mask, '-dstalpha', '-of GTIFF', input, output]).

or:

call('C:\Program Files\GDAL\gdalwarp.exe -t_srs ' + crs + ' -dstnodata 0 -q -cutline ' + mask + ' -dstalpha -of GTIFF ' + input + ' ' + output, shell=True)

Another possibility is to use the popen function of the subprocess modul. And don't forget to add double quotes for the input variable, since you use a directory that contains blanks.

Have a look at the 2nd answer: How to call gdal_translate from Python code?

1
  • thanks for help!but I am now have other problem with subprocess.call() even it's can be run by now ~thank you! Commented May 8, 2013 at 2:54
0

I use this method:

import os

command = 'gdalwarp -t_srs crs -dstnodata 0 -q -cutline msak -dstalpha -of GTiff %s %s' % (input, output)

os.system(command)

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.