I am working on a code to clip a raster with another raster. I have a forestcover.tiff image and I want to clip it by using other image clip.tiff which extend lies within it. I don't want to use arcpy module because it requires ArcGIS installed. My code should work for all users.
-
Use Python-GDAL.– SS_RebeliousCommented Jun 8, 2015 at 9:15
-
Check if this post helps [gis.stackexchange.com/questions/86960/… [1]: gis.stackexchange.com/questions/86960/…– addcolorCommented Jun 8, 2015 at 10:19
-
I am using Python-GDAL . I am new to it so i dont know how can i use it to solve my problem. Currently what i am trying to do is convert the clip.tiff to a polygon.shp and use that shapefile to clip forestcover.tiff. But When I run gdal.Polygonize() it creates .shp file without the .prj file.– Davis NhemaphukiCommented Jun 8, 2015 at 10:34
Add a comment
|
1 Answer
You can use the subprocess
module with the GDAL command line utilities to solve this.
import subprocess
#input files
forestcover = "/data/forestcover.tif"
clip = "/data/clip.tif"
#output files
cutline = "/data/cutline.shp"
result = "/data/result.tif"
#create the cutline polygon
cutline_cmd = ["gdaltindex", cutline, clip]
subprocess.check_call(cutline_cmd)
#crop forestcover to cutline
#Note: leave out the -crop_to_cutline option to clip by a regular bounding box
warp_cmd = ["gdalwarp", "-of", "GTiff", "-cutline", cutline, "-crop_to_cutline", forestcover, result]
subprocess.check_call(warp_cmd)