3

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)
7
  • for what its worth I ended up just using a python code to rename the new output files based on the file names of the old input names
    – spotter
    Commented Aug 8, 2015 at 20:00
  • inraster (variable) not 'inrasters' (string) on offending line
    – PolyGeo
    Commented Aug 9, 2015 at 4:54
  • actually that was a typo, I changed it above but the same error is still returned
    – spotter
    Commented Aug 9, 2015 at 5:03
  • You still have 'inraster' (a string) instead of inraster (a variable name that you are creating when iterating your inrasters 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 using arcpy.da.Walk (and/or perhaps arcpy.ListRasters) rather than glob to do something like this.
    – PolyGeo
    Commented Aug 9, 2015 at 5:10
  • I see what you mean. I modified my post and will look into using arcpy.ListRasters. Thank you.
    – spotter
    Commented Aug 9, 2015 at 5:16

2 Answers 2

2

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)
1

You can copy the field from the batch generator and paste it into excel to make custom output names if you don't use python. Can then be copied back into batch from excel.

I'd highly recommend using this as an opportunity to try Python, this a perfect beginners application. Something like this should do the trick:

import glob
import os

"""folder containing only your input rasters and nothing else"""
indir = 'c:/GIS/input_rasters/'

"""absolute path to your mask layer"""
mask = 'c:/GIS/clip_mask.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"""
    r = arcpy.sa.ExtractByMask(inraster,mask)
    r.save(outraster)
6
  • I will definitely try it in python, didn't realize you could copy and paste and make changes so simple! I need to learn python anyways though....
    – spotter
    Commented Aug 8, 2015 at 20:18
  • when I try to run this in python it says that for extract by mask only 2 arguments are taken and three are given. When i take out 'outraster' it says that the tool is not licensed even though I have successfully used it within ArcMap itself.
    – spotter
    Commented Aug 8, 2015 at 22:02
  • Thanks, I've fixed the script, try now. If you are running within the Python window of ArcGIS, and you have everything checked when you go to 'customize->extensions,' there's no reason this shouldn't run. In you are running in Python outside of ArcGIS, you'll need to import arcpy first in the first line of the script.
    – brokev03
    Commented Aug 8, 2015 at 23:19
  • yea im pretty sure the code is good, im just having problems with arcpy and licensing I'll have to work out. Thanks again!
    – spotter
    Commented Aug 8, 2015 at 23:20
  • I solved my acrpy problem, when I run this code it returns: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000865: Input raster: inrasters does not exist. ERROR 000865: Input raster or feature mask data: mask does not exist. Failed to execute (ExtractByMask) When I print inrasters though they are all there
    – spotter
    Commented Aug 9, 2015 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.