I'd like to learn using GDAL, and I want to use it in Python. Following the tutorial (http://www.gdal.org/gdal_tutorial.html), I succeded in opening vector and raster files using ogr.Open and gdal.Open () calls respectively. I then tried to understand how to create new files, but I didn't figure it out so far. For instance: if I want to use "gdalwarp" function to (let's say) clip a raster with a polygon, it does its job perfectly and clearly within QGIS, but using GDAL in Python how do I:

1) specify the output folder?

2) create the new file (I think it's done using the Create or CreateCopy () methods, is it?)?

I hope someone could help me, thanks in advance to the community.


I came up with creating a file in a specific folder. Anyway, it is just a file to be filled, namely, now I want to know how can I do my calculation and output to this "empty" file. Here is my code to let you understand me:

from subprocess import call
call(["ls", "-l"])
import gdal, ogr
from gdalconst import *

input= 'C:/Users/unimi/Desktop/sept2000_B5_refl.tif'
clip_area= 'C:/Users/unimi/Desktop/study_area_foscagno.shp'
output= 'C:/Users/unimi/Desktop/clip_sept2000_B5_refl.tif'

shp= ogr.Open (clip_area)
if shp is None:
    raise SystemExit("The shapefile could not opened")
raster= gdal.Open (input, GA_ReadOnly)
if raster is None:
    raise SystemExit("The raster could not opened")
print raster.GetProjection ()
format= 'GTiff'
driver= gdal.GetDriverByName (format)
metadata= driver.GetMetadata ()
dst_ds= driver.Create (output, 2133, 1273, 1, GDT_Float32)

ClipByPolygon= 'gdalwarp -dstnodata 0 -q -cutline %s -crop_to_cutline -of GTiff %s %s' % (shp, raster, dst_ds)
call (ClipByPolygon)

dst_ds= None

The code doesn't raise any error. However, the "empty" file from the "Create" call is produced, but gdalwarp has no effects afterwards.

  • Have you searched this site for previous questions and examples? For instance in this question here code-examples are provided (gis.stackexchange.com/questions/16657/…)
    – Curlew
    Commented Oct 30, 2013 at 11:41
  • 1
    Yes, but still not answering my questions... For instance, now I came up with creating an "empty file" (with "Create" method), but don't know how to "fill it" with a warp (for instance). I'll add my code to the question to be clear.
    – umbe1987
    Commented Oct 30, 2013 at 12:15

1 Answer 1


I finally came up with a quite nice result calling GDAL expressions from Python using "Call ()" method and specifying the path with "os.chdir ()" method. Here is a script example:

import os
from subprocess import call
call(["ls", "-l"])

path= 'C:/users/unimi/Desktop/'


cmd= 'gdalwarp -dstnodata 0 -q -cutline \
    study_area_foscagno.shp -crop_to_cutline -of GTiff sept2000_B5_refl.tif clip_sept2000_B5_refl.tif'
call (cmd)

The "Call ()" method is here used to do all the GDAL stuff without even importing GDAL in Python ("Call ()" method is even better than "os.system ()" method, see "https://stackoverflow.com/questions/89228/calling-an-external-command-in-python"). "os.chdir ()" method than have let me set the path where the program should look for my data. Hope this could help.

  • Is there not a way to do to clip a raster using gdal.Warp(), or gdal. Warpoptions()?
    – icypy
    Commented Jan 8, 2018 at 2:41

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.