0

I am new to arcpy. I am attempting to batch clip over 350 rasters to one the extent of one mask raster. I tried it in model builder and it wouldn't run so now I am trying it in arcpy but I continue to get error messages.

Here is my code:

import arcpy
import os
import glob
from arcpy import env
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *

#Sets environment
env.workspace = "E:/AlexandraOrrego/GIS_2017/RasterData/Reclass2.gdb"

mask = "E:/AlexandraOrrego/GIS_2017/Shapefiles/westus_forest"
outputDir = "E:/AlexandraOrrego/GIS_2017/RasterData/clipped.gdb"

rasList = arcpy.ListRasters()
for raster in rasList:  
    outRasterName = raster.replace("r2", "c")
    rasterObject = arcpy.sa.ExtractByMask(raster, mask, outRasterName)
#Saves Raster
    rasterObject.save(os.path.join(outputDir, outRasterName))
#prints once each raster layer has been clipped
    print outRasterName + " Clipped Successfully!"

 Runtime error 
Traceback (most recent call last):
  File "<string>", line 17, in <module>
TypeError: ExtractByMask() takes exactly 2 arguments (3 given)
>>> 
5
  • 3
    Welcome to GIS SE! A key part of your question is that you get error messages, but you haven't told us what they are. Please edit your question to include all error messages in full (including line numbers), as text (not as a picture).
    – Midavalo
    Commented Jun 27, 2017 at 1:50
  • 3
    What error messages? Do you actually have a spatial analyst extension available? If your mask is a shapefile it should have the extension .shp. Try use arcpy.sa.ExtractByMask instead of arcpy.gp.ExtractByMask_sa. Do your images already exist? Try setting arcpy.env.overwriteOutput = True. Does your output gdb exist? Have you inputted the path and name correctly? Commented Jun 27, 2017 at 1:51
  • @MichaelStimson I edited the code to include your suggestion of using arcpy.sa but still receive an error.
    – Allie
    Commented Jun 27, 2017 at 17:29
  • Your error says the function takes 2 inputs but you've supplied 3 parameters. You could be looking at old code (prior to ArcGIS 10.0) where the function did take 3 arguments. Change the extract by mask to rasterObject = arcpy.sa.ExtractByMask(raster, mask) as your output is rasterObject a return and not a parameter. If @Midavalo would consider reopening this question I could post that as an answer with appropriate links. Commented Jun 27, 2017 at 20:53
  • @MichaelStimson I have reopened the question.
    – Midavalo
    Commented Jun 27, 2017 at 21:06

1 Answer 1

2

Your error says the function takes 2 inputs but you've supplied 3 parameters. You could be looking at old code (prior to ArcGIS 10.0) where the function did take 3 arguments.

Between v9.3 and v10.0 there is a fundamental change in the handling of rasters by spatial analyst: from version 10 the raster object is a return value, not necessarily persisted to disc, where previously the output was a parameter, implicitly stated and it was the script writers' problem to clean up temporary rasters. From version 10 rasters not saved with raster_object.save(Full_Path) just disappear when the script finishes (so long as the script hasn't crashed!).

Change the extract by mask to rasterObject = arcpy.sa.ExtractByMask(raster, mask) as your output is rasterObject, a return value and not a parameter.

9.3 and prior syntax:

gp.ExtractByMask_sa(InRaster, InMask, OutRaster)

10.0 and newer syntax:

out_raster_object = arcpy.sa.ExtractByMask (in_raster, in_mask_data)
out_raster_object.save(out_raster)

This is a little gotcha when trying to learn or implement old code.

1
  • Thank you for the explanation, since changing it to two argument code works great!
    – Allie
    Commented Jun 27, 2017 at 21:30

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.