I have created several rasters using arcpy by subtracting several rasters using the arcpy.Minus_3d
function.
Now I want to clip these rasters using polygons. My rasters are called test01_out
, test02_out
etc. My polygons are called test poly_name_test01.shp
, test poly_name_test02.shp
, etc.
Is there a way to clip raster test01_out
with polygon test poly_name_test01.shp
using arcpy? I used QGIS to split my shapefile into separate shapefiles based on attributes. Alternatively, how can I call the polygon in a shapefile containing all my shapefiles based on attribute (name=test01
)?
I've been trying with this code but I'm stuck
import arcpy
import os
from arcpy import env
env.workspace = r"C:\rasters"
list1 = arcpy.ListRasters("test*out")
list2 = arcpy.ListFeatureClasses("*.shp")
for raster in list1:
rasterName = os.path.basename(raster) #I know this is wrong - I somehow need to remove the _out
rasterMask = ""
for mask in list2:
if mask.endswith(rasterName):
rasterMask = mask
print "mask: " + mask
break
if rasterMask !="":
clipFile = rasterName + "_clip"
print "clip file: " + clipFile
arcpy.Clip_analysis(raster, env.workspace + "\\" + mask, env.workspace + "\\" + clipFile)