ArcPy – Fixing Overwritten Rows Using da.UpdateCursor

arcpycursoriteration

I am trying to populate fields based on objects. When trying to use Update Cursor it overwrites all of the row values with information from the last feature read. I am sure that I am meant to get it to iterate and move to the next feature, but I am stuck, next(cursor) stops the script entirely.

#Import modules
import arcpy
import sys
import os
import datetime
arcpy.env.overwriteOutput = True
arcpy.gp.overwriteOutput = True

# Set the current workspace
folder = arcpy.env.workspace = r"F:\Workspace\Broome"
sr = arcpy.SpatialReference(4326)
Type = "JP2"
rasters = arcpy.ListRasters("*", Type)
fc = feature_wgs84 = arcpy.CreateFeatureclass_management ("c:/geometry","polygons_wgs84.shp", "POLYGON", "", "DISABLED", "DISABLED", sr)
arcpy.AddField_management(feature_wgs84, "RasterName", "TEXT")
arcpy.AddField_management(feature_wgs84, "Projection", "TEXT")
arcpy.AddField_management(feature_wgs84, "FILENAME", "TEXT")
for raster in rasters:
    # Get properties for the specified raster dataset
    propsInRaster = arcpy.Describe(raster)
    SR = propsInRaster.spatialReference
    FileLoc = os.path.join (folder, raster)
    FileName = (raster)

    # {{{ Create a polygon for the featureList array
    point = arcpy.Point()
    array = arcpy.Array()
    featureList = []

    point.X = propsInRaster.extent.XMin
    point.Y = propsInRaster.extent.YMin
    array.add(point)

    point.X = propsInRaster.extent.XMin
    point.Y = propsInRaster.extent.YMax
    array.add(point)

    point.X = propsInRaster.extent.XMax
    point.Y = propsInRaster.extent.YMax
    array.add(point)

    point.X = propsInRaster.extent.XMax
    point.Y = propsInRaster.extent.YMin
    array.add(point)
    array.add(array.getObject(0))
    polygon = arcpy.Polygon(array)
    featureList.append(polygon)
    # }}}
    if (SR.name) == "GCS_WGS_1984":
        arcpy.Append_management(featureList, feature_wgs84, "NO_TEST")
        fc = r'c:\geometry\polygons_wgs84.shp'
        
        fields = ['RasterName', 'Projection', 'FILENAME']
        with arcpy.da.UpdateCursor(fc, fields) as cursor:
            for row in cursor:
                row[0] = FileName
                row[1] = (SR.name)
                row[2] = FileLoc
                cursor.updateRow(row)
            del row
            del cursor

    else:
        arcpy.CreateFeatureclass_management ("c:/geometry","polygons_" + (SR.name) + ".shp", "POLYGON", "", "DISABLED", "DISABLED", SR.name)
        Target = os.path.join ("c:/geometry","polygons_" + (SR.name) + ".shp")
        arcpy.Append_management(featureList, Target, "NO_TEST")
        fc = Target
        fields = ['RasterName', 'Projection', 'FILENAME']
        with arcpy.da.UpdateCursor(fc, fields) as cursor:
            for row in cursor:
                row[0] = FileName
                row[1] = (SR.name)
                row[2] = FileLoc
                cursor.updateRow(row)
            del row
            del cursor

        ProjFile = os.path.join ("c:/geometry","polygons_" + (SR.name) + "_" + sr + ".shp")
        print (ProjFile)
        arcpy.Project_management(Target, ProjFile, sr)
        arcpy.Append_management(ProjFile, "c:/geometry/polygons_wgs84.shp", "NO_TEST")
        

Best Answer

Thanks to Vince, suggesting to do insert I was able to get the result I need to create the rows with geometry I had to insert the geometry with the rest of the fields, see below:

sr = arcpy.SpatialReference(4326)
##Type = "JP2"
rasters = arcpy.ListFiles("*.ecw")
shp = os.path.basename(folder) + ".shp"
base = os.path.basename(folder)

print shp

fc = feature_wgs84 = arcpy.CreateFeatureclass_management ("c:/test",shp, "POLYGON", "", "DISABLED", "DISABLED", sr)
arcpy.AddField_management(feature_wgs84, "RasterName", "TEXT")
arcpy.AddField_management(feature_wgs84, "Projection", "TEXT")
arcpy.AddField_management(feature_wgs84, "FILENAME", "TEXT")
for raster in rasters:
    
    # Get properties for the specified raster dataset
    propsInRaster = arcpy.Describe(raster)
    SR = propsInRaster.spatialReference
    FileLoc = os.path.join (folder, raster)
    FileName = (raster)
    print FileName

    # {{{ Create a polygon for the featureList array
    point = arcpy.Point()
    array = arcpy.Array()
    featureList = []

    point.X = propsInRaster.extent.XMin
    point.Y = propsInRaster.extent.YMin
    array.add(point)

    point.X = propsInRaster.extent.XMin
    point.Y = propsInRaster.extent.YMax
    array.add(point)

    point.X = propsInRaster.extent.XMax
    point.Y = propsInRaster.extent.YMax
    array.add(point)

    point.X = propsInRaster.extent.XMax
    point.Y = propsInRaster.extent.YMin
    array.add(point)
    array.add(array.getObject(0))
    polygon = arcpy.Polygon(array)
    fields = ("shape@",'RasterName', 'Projection', 'FILENAME')
    rowValues = [((polygon), FileName, (SR.name), FileLoc)]
    cur = arcpy.da.InsertCursor(fc, (fields))
    for row in rowValues:
        cur.insertRow(row)
    del row
    del cur