Select to view content in your preferred language

Loop to clip rasters with polygons and give name to clipped raster

2501
1
01-10-2018 08:29 AM
SebastianSalazar
Emerging Contributor

Hello. 
I have 190 rasters and 190 polygons.
I need to clip each raster with it respective polygon
Rasters are identified as sm001.jpg to sm192.jpg
Polygons are identified as sm001_plg.shp to sm192.shp

In each iteration the raster clipped must be named as sm001.img ...sm192.jpg

With the below code I am doing the proccess manually

arcpy.Clip_management(in_raster="D:/ask/smimgs/sm001.jpg",
out_raster="D:/ask/smimgs_clips/sm001clp.img",
in_template_dataset="D:/ask/sm_plg/sm001_plg.shp",
nodata_value="256", clipping_geometry="NONE", maintain_clipping_extent="NO_MAINTAIN_EXTENT")

Could anyone help me to write the code in a loop?
Any idea is welcome
Thank you very much indeed

Tags (1)
0 Kudos
1 Reply
deleted-user-qzyyiYme1rz9
Deactivated User

import arcpy

#folder locations
in_raster_folder = "D:/ask/smimgs"
in_polygon_folder = "D:/ask/sm_plg"
out_clip_folder = "D:/ask/smimgs_clips"


arcpy.env.workspace = in_raster_folder

in_rasters_list = arcpy.ListRasters("*", "ALL")

for raster in in_rasters_list:
    raster_name = raster[:-4]
    clip_poly = in_polygon_folder+"\\"+raster_name+"_plg.shp"
    out_raster = out_clip_folder+"\\"+raster_name+"clp.img"
    arcpy.Clip_management(raster,"" , out_raster, clip_poly, "256", "NONE", "NO_MAINTAIN_EXTENT")

0 Kudos