Trying to get raster extents into a GDB using open cursor

439
2
09-20-2022 02:45 PM
LeonConsus
New Contributor II

Hello, I am fairly new to py programming and have 1/2 copy pasted this and 1/2 developed this to meet my requirements.  Most of the script is successful, but the cursor seems to stall after the first record is inserted.  I have added all of the code, just in case some of the set up is the problem...

 

try:
    import sys, traceback, os
    import arcpy
    arcpy.env.overwriteOutput = True
    
    #inDir = arcpy.GetParameterAsText(0)
    inDir = "Q:\\CCR\\1.Alexis Creek Flights\\"
    #outFile = arcpy.GetParameterAsText(1)
    outFile = "Q:\\EntireQDrive.gdb"
    print "In Dir: " + (inDir)
    print "Out File: " + (outFile)
    print "dirname: " + os.path.dirname(outFile)
    print "basename: " + os.path.basename(outFile)
    FileName = os.path.dirname(outFile) + os.path.basename(outFile)
    print "filename: " + (FileName)
    arcpy.env.workspace = FileName
    
    feature_classes = []
    walk = arcpy.da.Walk(inDir, datatype="RasterDataset", type="All")
    
    for dirpath, dirnames, filenames in walk:
        for filename in filenames:
            #print (dirpath)
            #print (filenames)
            feature_classes.append(os.path.join(dirpath, filename))

    arcpy.CreateFeatureclass_management(FileName,"CCR", "POLYGON")
    print ("Created FC")
    
    fieldName1 = "RasterName"
    fieldLength1 = 100
    fieldName2 = "RasterPath"
    fieldLength2 = 250
    arcpy.AddField_management("CCR", fieldName1, "TEXT", fieldLength1)
    arcpy.AddField_management("CCR", fieldName2, "TEXT", fieldLength2)
    print ("Added Field")
    
    cursor = arcpy.InsertCursor("CCR")
    point = arcpy.Point()
    array = arcpy.Array()
    corners = ["lowerLeft", "lowerRight", "upperRight", "upperLeft"]

    #print (feature_classes)
    for Ras in feature_classes:
        print(Ras)
        feat = cursor.newRow()  
        r = arcpy.Raster(Ras)
        for corner in corners:    
            point.X = getattr(r.extent, "%s" % corner).X
            point.Y = getattr(r.extent, "%s" % corner).Y
            array.add(point)
        array.add(array.getObject(0))
        polygon = arcpy.Polygon(array)
        feat.shape = polygon
        feat.setValue("RasterName", Ras.split('\\')[-1])
        feat.setValue("RasterPath", Ras)
        cursor.insertRow(feat)
        array.removeAll()
    del feat
    del cursor  
    
    print ("")
    print ("All Done :)")

    
except arcpy.ExecuteError: 
    msgs = arcpy.GetMessages(2) 
    arcpy.AddError(msgs) 
    print msgs
except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
    print pymsg + "\n"
    print msgs

 

Thanks in advance!

Tags (2)
0 Kudos
2 Replies
RhettZufelt
MVP Frequent Contributor

That is a lot of code for someone to try to figure out all the indents, etc.

Might get more responses if you edit the post and paste the code in using the code formatter:

 

RhettZufelt_0-1663715689201.png

RhettZufelt_1-1663715699942.png

 

R_

 

LeonConsus
New Contributor II

Thanks Rhett, missed that in my original post.  It has now been updated 😊

0 Kudos