1

I am attempting to run Extract by Mask through a loop using Search Cursor, where the Cursor is defined by the row "FID" in a table from a grid shapefile. The code is as follows:

import arcpy, os
from arcpy import env

outws = r"C:/path2/"
raster = r"C:/path1/raster.tif"
fc = r"C:/path1/shapegrid.shp"

arcpy.CheckOutExtension("Spatial")
count = 0
with arcpy.da.SearchCursor(fc, ["FID"]) as cursor:
    for row in cursor:
        out = os.path.join(outws, "demgrid" + str(count) + ".tif")
        ebm = arcpy.sa.ExtractByMask (raster, row)
        ebm.save(out)
        count = count + 1

arcpy.CheckInExtension("Spatial")       


sys.exit() 

The error that first pops up is on the 17th line: ebm = arcpy.sa.ExtractByMask (raster, row)

Traceback (most recent call last):
  File "grid_extract.py", line 17, in <module>
    ebm = arcpy.sa.ExtractByMask (raster, row)

My speculation is that either I've called the wrong module, or that I've not defined a specific one I need for this task.

1

As far as I remember, cursor row's shape attribute can be used for any sorts of overlay operation in a general sense. In your case, if you change your cursor declaration to this, with arcpy.da.SearchCursor(fc, ["FID", "SHAPE@"]) as cursor and change your function call as ebm = arcpy.sa.ExtractByMask (raster, row[1]) (be aware that row[1] refers to the shape row of your polygon feature class), you should be fine.

  • @faith_dur Thanks for getting back on the code, I changed the code to include your changes...and there was a successful liftoff! Key notes to include that this works in its present form ONLY for rasters, do not use ASCII grids – E. Weglarz Jul 25 '17 at 19:59
0

The second parameter of that method is supposed to be a raster layer or feature layer not a row object. I'm guessing your wanting to extract a mask for each feature in your fc? To do that you may:

  1. Within search cursor for row loop add Make Feature Layer method
  2. Within Make Feature Layer statement define a where clause to query/show only the current row within the search cursor
  3. Finally, within the Extract By Mask method, include the feature layer name as the second parameter

Note, you may also need to use Make Raster Layer on the raster layer and use this object within the extract method. This will query the fc by each feature and extract by each feature

  • Thanks for looking at this, the code that I found to base this process off of had the loop as such so raster is the .tif file that I will be running the Extract by Mask process on (defined "raster" variable). Does that change the need to include making a Feature Layer within the loop? – E. Weglarz Jul 24 '17 at 13:09
  • You should define the raster layer before the cursor and define the feature layer within the cursor since you need it to be dynamically changing based on the current row feature (defined by using the where clause). – artwork21 Jul 24 '17 at 13:13
0

The following code will allow the Spatial Analyst tool "Extract by Mask" to be used across an expansive dataset by looping through a shapefile with numerous polygons. For reference, the grid shapefile in the example code below has three fields, the field used to loop through all polygons is "FID":

import arcpy, os
from arcpy import env

outws = r"C:/path2/"
raster = r"C:/path1/raster.tif"
fc = r"C:/path1/shapegrid.shp"

arcpy.CheckOutExtension("Spatial")
count = 0
with arcpy.da.SearchCursor(fc, ["FID", "SHAPE@"]) as cursor:
    for row in cursor:
        out = os.path.join(outws, "demgrid" + str(count) + ".tif")
        ebm = arcpy.sa.ExtractByMask (raster, row[1])
        ebm.save(out)
        count = count + 1

arcpy.CheckInExtension("Spatial") 

"FID" is a column from 0...n, which is looped through sequentially by row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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