Monday, 2 October 2017

Clip raster using shapefile into multiple rasters using ArcGIS Desktop?


I have a raster file representing a large area and a shapefile of 30 polygons representing watersheds. I would like to clip the raster with the shapefile to create 30 rasters representing each watershed? I would like to automate the process as I will be repeating it for another 10 raster files representing large areas.


I'm using ArcGIS 10.1 with an advanced arcinfo license. Can this be done without the use of python?



Answer



you can loop on each polygon and perform the clip raster or extract by mask. Here is a small sample code :


import arcpy

rasterlist = arcpy.ListRasters("your_workspace")
for raster in rasterlist:
for i in range(30):

arcpy.MakeFeatureLayer_management("your_shapefile", "layer" + str(i), ' "FID" = ' + str(i)) #create a layer with only polygon i
arcpy.Clip_management(raster, "#", raster[:-4] + "clip" + str(i) +" .tif","layer" + str(i), "0", "ClippingGeometry") #clip based on layer, clipping geometry will use the polygon extent only

note that you may want to use the snap raster environment to make sure that you are ligned with your original image.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...