0

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")
        
5
  • UpdateCursor operates on all rows in the selection environment unless a where_clause is applied. You didn't use Select By {property} (and a Layer) or apply a WHERE, so it updates all rows. Note that you don't really need to del either row or cursor.
    – Vince
    Commented Dec 14, 2021 at 0:09
  • Hi, that makes sense. Unfortunately I believe that I don't have any unique values to go by to create a Select By or to apply a WHERE. Not even an OBJECTID, should I add a counter to enumerate through the rows? and if so do you have any recommendations? Commented Dec 14, 2021 at 4:44
  • Append and update is the wrong paradigm for your use case. You should be using a single Insert.
    – Vince
    Commented Dec 14, 2021 at 4:50
  • Thanks Vince I will give it a try. Commented Dec 14, 2021 at 22:30
  • Hi Vince, Insert Cursor is definitely the correct option, the only issue is if I place it in the location of the append cursor from above I get rows with correct attribution but no geometry/polygons and I also get Polygons with no attribution. I believe that I need to add the fields and their values when creating the polygon array. Do you have any recommendations? Commented Dec 16, 2021 at 22:48

1 Answer 1

-1

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
2
  • This code shouldn't work to produce valid polygons. You can't reuse the Point object -- You need at least four separate Point objects to assemble a quadrilateral (the first/last can technically be the same). It's also questionable that this would work with different spatial references in the rasters without explicitly reprojecting the geometry (only one spatial reference is permitted per polygon layer). And the Polygon constructor needs a SpatialReference parameter or it's likely to be truncated to three decimal places.
    – Vince
    Commented Dec 17, 2021 at 3:46
  • Hi Vince, it does work to an accuracy that I require. I have already included an if else to to catch undefined or different projections. Thanks again for your help with the insert tip. Commented Dec 19, 2021 at 23:15

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.