Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am now want to use subprocess.call with gdalwrap to clip raster from shapfile (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??

share|improve this question
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 May 6 '13 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 May 6 '13 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! –  blueman010112 May 6 '13 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... –  blueman010112 May 6 '13 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 May 6 '13 at 7:54
show 1 more comment

1 Answer

up vote 2 down vote accepted

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?

share|improve this answer
    
thanks for help!but I am now have other problem with subprocess.call() even it's can be run by now ~thank you! –  blueman010112 May 8 '13 at 2:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.