[GIS] Batch Extract by Mask, but customize individual output names

arcgis-10.3arcpybatcherror-000865spatial-analyst

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)

Best Answer

There is no need to loop through the list of rasters generated from glob.glob() and append them to a new list. I would take the following approach:

  1. Define the input and output workspaces
  2. List all rasters using glob
  3. Loop through the list
  4. Define the output name and path
  5. Perform extract by mask
  6. Save the extract by mask

import arcpy, glob, os

arcpy.CheckOutExtension('Spatial')

# folder containing only input rasters and nothing else
inws = r'D:\Sheyenne\Atmospherically Corrected Landsat\Practice_bands'
outws = r'D:\Sheyenne\Atmospherically Corrected Landsat\Practice_bands\out' # Note the new output workspace folder

# absolute path to mask layer
mask = r'D:\Sheyenne\sheyenne_area_poly.shp'

# Generate a list of all .tif files (Note this lists full paths)
rasters = glob.glob(os.path.join(inws, "*.tif"))

# iterate over all input .tif from raster list
for ras in rasters:

    # Define the output path and name
    outname = os.path.join(outws, os.path.basename(ras).split(".")[0] + "_clp.tif")

    # Perform the EBM
    out_extract = arcpy.sa.ExtractByMask(ras, mask)

    # Save the output
    out_extract.save(outname)
Related Question