2

I have a file geodatabase with a polygon feature class named "AOI". The "AOI" feature class currently holds two records. I want to loop through each record and run a geoprocessing task, for example the ExtractByMask tool. When I run my script it only runs against the first records and stops successfully. Below is my code....

with arcpy.da.SearchCursor(AOI, ("SHAPE@")) as cursor:
    for row in cursor:
        Extract = arcpy.sa.ExtractByMask(costraster, row)
2

Are you planning on doing anything with the output? You need to save your raster object if you plan on doing anything with it. See the examples on the documentation.

import os
import arcpy

with arcpy.da.SearchCursor(AOI, ("SHAPE@")) as cursor:
    for index, row in enumerate(cursor):
        extract = arcpy.sa.ExtractByMask(costraster, row[0])
        out_raster_name = "extract{}".format(index + 1)
        out_raster = os.path.join(r'c:\output', out_raster_name)
        extract.save(out_raster)
        arcpy.management.MakeRasterLayer(out_raster, out_raster_name, None, AOI)
        extract = None
  • Yes I plan to run other processes. This is the first step in a three part process I will then need to run a Cost Distance and a Cost Path. These clipped rasters are first. – user22811 Oct 10 '13 at 14:42
  • The key here was that you were trying to use the row for your mask, when you needed to use the shape (row[0]). More importantly, that means that your call to ExtractByMask was breaking, but you were not catching the error. I think this might be because of the ExtractByMask loop being inside of a with statement. – blord-castillo Oct 10 '13 at 15:04
  • Is this the best method for looping over individual records and executing a geoprocessing service against a record? – user22811 Oct 10 '13 at 16:14
  • Really depends on what you're trying to do. For the problem you've described, you could also create a selection on a feature layer each time, use that feature layer as your mask environment, and then each raster tool would respect that without having to make all these intermediate datasets. – Jason Scheirer Oct 10 '13 at 17:22
  • I have three steps total that I need to do, Extract By Mask, Cost Distance, and Cost Path. So far I have ran the Extract By Mask and Cost Distance tools successfully but I ran into an issue with the Cost Path tool. It was saying it cannot read the backlink raster. Could there be something wrong with the script when I performed the Cost Distance? – user22811 Oct 10 '13 at 19:42

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.