I want to extract by mask (the same mask everytime) on about 500 raster's, but I want to just append the name of every input raster. So if a raster is titled Landsat_010182 I just want to add _clip to it so that I still know what every image contains. Is this something I would need to do in python or is there another way to do this?
Edit:
This is the exact code I am trying:
import arcpy
import glob
import os
arcpy.CheckOutExtension('Spatial')
"""folder containing only input rasters and nothing else"""
indir = r'D:\Sheyenne\Atmospherically Corrected Landsat\Practice_bands'
"""absolute path to mask layer"""
mask = r'D:\Sheyenne\sheyenne_area_poly.shp'
"""create emply list to hold paths to input rasters"""
inrasters = []
"""populate the list inrasters with all paths to .tif in directory"""
os.chdir(indir)
for r in glob.glob('*.tif'):
inrasters.append(r)
#
#"""iterate over all input .tif from list inrasters"""
for inraster in inrasters:
#
# """create a unique name for each output raster"""
outraster = inraster.replace('.tif','_clip.tif')
# """Clip each raster with it's unique name as output""
arcpy.sa.ExtractByMask(inraster,mask)
but it returns the error:
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster: LT50290282011268PAC01_sr_band1.tif does not exist.
Failed to execute (ExtractByMask).
LT50290282011268PAC01_sr_band1.tif is the first file in the inrasters
list
Edit:
Everything works as I expect up until the loop starts. When I print inrasters
all of them are listed. When I print outraster
only the second raster in my list is printed. So somewhere in the loop is when things are breaking down. Also, I never actually use the variable outraster
which may be part of the problem.
Edit:
I got it to work using this code:
import arcpy
arcpy.env.workspace='D:\Sheyenne\Practice_bands'
arcpy.CheckOutExtension('Spatial')
"""absolute path to your mask layer"""
mask = r'D:\Sheyenne\sheyenne_area_poly.shp'
"""Loop through rasters, append names and savefiles"""
rasters = arcpy.ListRasters()
for raster in rasters:
outraster = raster.replace('.tif','_clip.tif')
arcpy.gp.ExtractByMask_sa(raster,mask,outraster)
'inraster'
(a string) instead ofinraster
(a variable name that you are creating when iterating yourinrasters
list). The error looks like it may be out of sync with your posted code. Something you may want to think about is that the code you are using is very un-ArcPythonic. I would be usingarcpy.da.Walk
(and/or perhapsarcpy.ListRasters
) rather thanglob
to do something like this.arcpy.ListRasters
. Thank you.