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.
UPDATE
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.