1

I have written code that selects rows of a buffer shapefile based on that file's intersection with a polygon that represents the extent of a raster. In the photo below, the code selects all of the red dots for this given polygon.

representation of arcgis output for buffers that are selected in a polygon

Using a cursor, I am going through those selected buffers and clipping an associated raster by each buffer. The code below was working when I wrote it three months ago; however, since updating, I am having a problem grabbing the SHAPE@ token as a polygon to clip the raster. I would have been using ArcGIS Pro 2.8.3 when I wrote and ran the code successfully, and I have Version 2.9.1 now.

I believe I've isolated the problem to the row[1] command. Previously, I'm under the impression that I was grabbing a polygon object. Now, however, my output for the lines,

 print('      row: ', row)
 print('      row[1]: ', row[1])

is showing row[1] as two different outputs, a Polygon object that I want and the geoprocessing geometry object that it now gives me:

  row:  ['LCRas_0', <Polygon object at 0x1f56ab62108[0x1f56db53300]>]
  row[1]:  <geoprocessing describe geometry object object at 0x000001F56DB53300>

The clip code that once worked is now giving me a blank output. I've also tried Extract by Mask, but receive an ERROR 999999 when I try to specify that the output extent should be that of the buffer.

Could this be a bug in the versions, or am I missing something simple?

arcpy.env.addOutputsToMap = False
fields = ['RasterName', 'SHAPE@']
j=0 #code was made pieacemeal to work on 3 comps; number changes depending where in the shapefile you're at
buff_file = r"I:\test\test_buffs.shp"

for poly in listp:
    #record lc file that corresponds
    lc_file = poly[:-9].replace(" ", "")
    path = r"I:\test\Classified"
    cor_rast = os.path.join(path, str(lc_file+"_classified.tif"))
    raster_xmin = arcpy.GetRasterProperties_management(cor_rast, "LEFT")
    raster_xmax = arcpy.GetRasterProperties_management(cor_rast, "RIGHT")
    raster_ymin = arcpy.GetRasterProperties_management(cor_rast, "BOTTOM")
    raster_ymax = arcpy.GetRasterProperties_management(cor_rast, "TOP")
    extent = raster_xmin[0] + " " + raster_ymin[0] + " " + raster_xmax[0] + " " + raster_ymax[0]
    print('Starting arcpy.da.UpdateCursor')   
    
    with arcpy.da.UpdateCursor(arcpy.management.SelectLayerByLocation(buff_file, "INTERSECT", poly),fields) as cursor: #select by polygon
        for row in cursor:
            rname = str("LCRas_" + str(j)) #make raster name
            path = r"I:\test\Rasters" #set path where raster output will happen
            rpath = os.path.join(path, str(rname+".tif")) #create name of raster file
            print('   Starting arcpy.Clip_management for ', rpath)
            print('      cor_rast: ', cor_rast)
            print('      raster_xmin: ', raster_xmin)
            print('      raster_xmax: ', raster_xmax)
            print('      raster_ymin: ', raster_ymin)
            print('      raster_ymax: ', raster_ymax)
            print('      extent: ', extent)
            print('      row: ', row)
            print('      row[1]: ', row[1])
            arcpy.Clip_management(cor_rast, extent, rpath, row[1], "0", "ClippingGeometry", 'MAINTAIN_EXTENT') #extract raster from land cover
            print('   Ended arcpy.Clip_management for ', rpath)
            row[0] = rname #set row to equal raster name
            cursor.updateRow(row) #updates row to have raster column 
            j=j+1  
    print('Ended arcpy.da.UpdateCursor')```

1
  • Obvious question to ask, you say you have not used this code for 3 months and during that period you have upgraded ArcPro.. Has in anyway your data changed/updated/ moved or been edited? If so consider that it's the dataset causing the issue?
    – Hornbydd
    Jan 21, 2022 at 16:46

1 Answer 1

1

The polygon ended up being a red herring. By chance I realized that the output from the code above was not a blank raster, but instead one at such a large scale that I couldn't see the small 0.6m radius raster. This issue of extent was fixed by changing the extent used in the Clip_management function to the extent of the buffer being used to clip the raster.

env.workspace = r'I:\test\Polygons'
arcpy.env.addOutputsToMap = False
fields = ['RasterName', 'SHAPE@']
j=0 #code was made pieacemeal to work on 3 comps; number changes depending where in the shapefile you're at
buff_file = r"I:\test\test_buffs.shp"

for poly in listp:
    #record lc file that corresponds
    lc_file = poly[:-9].replace(" ", "")
    path = r"I:\test\Classified"
    cor_rast = os.path.join(path, str(lc_file+"_classified.tif"))
    print('Starting arcpy.da.UpdateCursor')   
    
    with arcpy.da.UpdateCursor(arcpy.management.SelectLayerByLocation(buff_file, "INTERSECT", poly),fields) as cursor: #select by polygon
        for row in cursor:
            rname = str("LCRas_" + str(j)) #make raster name
            path = r"I:\test\Rasters" #set path where raster output will happen
            rpath = os.path.join(path, str(rname+".tif")) #create name of raster file
            extent = row[1].extent
            extentBuff = str(extent.XMin) + " " + str(extent.YMin) + " " + str(extent.XMax) + " " + str(extent.YMax)            
            arcpy.management.Clip(cor_rast, extentBuff, rpath, row[1], "15", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
            print('   Ended arcpy.Clip_management for ', rpath)
            row[0] = rname #set row to equal raster name
            cursor.updateRow(row) #updates row to have raster column 
            j=j+1  
    print('Ended arcpy.da.UpdateCursor')

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.