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.
Asked
Active
Viewed 1,223 times
1
-
Use Python-GDAL. – SS_Rebelious Jun 08 '15 at 09:15
-
Check if this post helps http://gis.stackexchange.com/questions/86960/clip-raster-layer-with-raster-mask-layer-in-qgis – addcolor Jun 08 '15 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 Nhemaphuki Jun 08 '15 at 10:34
1 Answers
1
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)
Kersten
- 9,899
- 3
- 37
- 59