Extract by Mask Problem

3985
2
Jump to solution
07-11-2013 01:14 PM
DonalCasey
New Contributor
Hi all,

I'm having a bit of trouble with using the Extract by Mask functionality. I have a Python code that creates a file GDB and then I import shapefiles to the GDB. I then create a new raster dataset within the file GDB and then mosaic 4 .tiff into it. The problem occurs when I try to 'Extract by Mask' using the new raster dataset as the input and a polygon as the mask. I get an error stating that:

'AttributeError: 'module' object has no attribute 'ExtractByMask''

I have imported the spatial analyst extension and below is the code that I'm trying to use:

# Set local variables in_raster = "C:/Users/D/Prog_Assign.gdb/R_Dataset" in_mask_data = "C:/Users/D/Prog_Assign.gdb/Study_area_extent_shp" #Extract by Mask arcpy.ExtractByMask (in_raster, in_mask_data) # Save the output outExtractByMask.save("C:/Users/D/Prog_Assign.gdb/T_Mask")


Any help or advice would be greatly appreciated!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StacyRendall1
Occasional Contributor III
The error is saying that you are trying to call a module (ExtractByMask) that does not exist in arcpy. That is because ExtractByMask lives in Spatial Analyst, which is arcpy.sa

I noticed that you have missed a step in your code, that is setting outExtractByMask to ExtractByMask(). This will cause errors too, see code below for the fix.

You have two options to fix your initial problem:

  1. adding from arcpy.sa import * to the top of your code (just below import arcpy) - this is what ESRI does but it is generally not recommended. Then you can just use ExtractByMask(), not arcpy.ExtractByMask()

  2. call ExtractByMask directly by using arcpy.sa.ExtractByMask(), so your posted code would become (changes in red):

# Set local variables
in_raster = "C:/Users/D/Prog_Assign.gdb/R_Dataset"
in_mask_data = "C:/Users/D/Prog_Assign.gdb/Study_area_extent_shp"
#Extract by Mask
outExtractByMask = arcpy.sa.ExtractByMask (in_raster, in_mask_data)
# Save the output
outExtractByMask.save("C:/Users/D/Prog_Assign.gdb/T_Mask")

View solution in original post

0 Kudos
2 Replies
StacyRendall1
Occasional Contributor III
The error is saying that you are trying to call a module (ExtractByMask) that does not exist in arcpy. That is because ExtractByMask lives in Spatial Analyst, which is arcpy.sa

I noticed that you have missed a step in your code, that is setting outExtractByMask to ExtractByMask(). This will cause errors too, see code below for the fix.

You have two options to fix your initial problem:

  1. adding from arcpy.sa import * to the top of your code (just below import arcpy) - this is what ESRI does but it is generally not recommended. Then you can just use ExtractByMask(), not arcpy.ExtractByMask()

  2. call ExtractByMask directly by using arcpy.sa.ExtractByMask(), so your posted code would become (changes in red):

# Set local variables
in_raster = "C:/Users/D/Prog_Assign.gdb/R_Dataset"
in_mask_data = "C:/Users/D/Prog_Assign.gdb/Study_area_extent_shp"
#Extract by Mask
outExtractByMask = arcpy.sa.ExtractByMask (in_raster, in_mask_data)
# Save the output
outExtractByMask.save("C:/Users/D/Prog_Assign.gdb/T_Mask")
0 Kudos
DonalCasey
New Contributor
Thanks very much, that worked a treat, I couldn't figure out where I was going wrong!
0 Kudos