I have a script that is using the OID@ as the cursor to iterate through a feature class. It seems to be skipping 3 OIDs at random intervals during the script when using UpdateCursor, but seems to have a consistent 3 row skip when using SearchCursor.
Is there a reason as to why I am getting a random skip here?
with arcpy.da.SearchCursor(IterateLayer, ["OID@"]) as cursor:
for row in cursor:
rowID = str(row[0])
arcpy.AddMessage(fr"Processing for Pole: "+ rowID)
PoleSelected = mgt.SelectLayerByAttribute(IterateLayer, "NEW_SELECTION", "OBJECTID = {}".format(row[0]))
arcpy.AddMessage("Closest River/Stream Processing")
def Closest_RiverStream(SaR, PoleS):
def RiverStream_ID(PoleS):
with arcpy.da.SearchCursor((PoleS), "Stream_ID") as cursor:
for row in cursor:
StreamID = '{}'.format(row[0])
StreamID = str(StreamID)
return StreamID
RID = RiverStream_ID(PoleS)
RID = str(RID)
clause = "OBJECTID = " + RID
StreamSelected = mgt.SelectLayerByAttribute(SaR, "NEW_SELECTION", where_clause=clause)
def StreamPermanence(S_S):
with arcpy.da.SearchCursor(S_S, "StreamPermanence"):
for row in cursor:
StreamPerm = '{}'.format(row[0])
StreamPerm = str(StreamPerm)
return StreamPerm
if (RID != "-1"):
ParclOwner = mgt.CalculateField(PoleS, "StreamPermanence", "'"+str(StreamPermanence(StreamSelected))+"'")
else:
NotInSearchDist = "Not Within 100 Feet of Stream or River"
ParclOwner = mgt.CalculateField(PoleS, "StreamPermanence", "'"+str(NotInSearchDist)+"'")
def StreamSize(S_S):
with arcpy.da.SearchCursor(S_S, "FPAStreamSize"):
for row in cursor:
Stream_Size = '{}'.format(row[0])
Stream_Size = str(Stream_Size)
return Stream_Size
if (RID != "-1"):
ParclOwner = mgt.CalculateField(PoleS, "StreamSize", "'"+str(StreamSize(StreamSelected))+"'")
else:
NotInSearchDist = "Not Within 100 Feet of Stream or River"
ParclOwner = mgt.CalculateField(PoleS, "StreamSize", "'"+str(NotInSearchDist)+"'")
#mgt.Delete(TempPole)
Closest_RiverStream(StreamAndRivers, PoleSelected)
mgt.SelectLayerByAttribute(StreamAndRivers, "CLEAR_SELECTION")
I think issue was mainly due to the features being in different projections (comparing a local dataset to a national dataset). Instead of using distanceTo, I switched over to running the analysis Near geoprocessing tool, and comparing the if any of the NEAR_FID values I get match the Keys from the Line Dataset. After it is just a simple translation of data between the tables and utilize update Cursor to update the feature class.
Thanks for the help, you pointed me in the right direction and helped me better understand how to utilize geometry and the best practices for search/update cursor.
def GetRecordsDict(IterateLayer, Fields):
rows = {}
with arcpy.da.SearchCursor(IterateLayer, Fields) as cursor:
for row in cursor:
key = row[0]
values = list(row[1:])
rows[key] = values
return rows
def Proximity(PoleFeature, DistanceFeature, Distance):
for keyA, value in PoleFeature.items():
near_fid = value[0]
if near_fid in DistanceFeature:
value = list(value)
value[1] = DistanceFeature[near_fid][0]
value[2] = DistanceFeature[near_fid][1]
PoleFeature[keyA] = value
else:
value = list(value)
value[1] = "Not Within 100ft of Stream"
value[2] = "Not Within 100ft of Stream"
PoleFeature[keyA] = value
arcpy.AddMessage("Updated Dictionary for Poles")
return PoleFeature
def UpdateFeatureClass(UpdateDict, FC, Fields):
with arcpy.da.UpdateCursor(FC, Fields) as cursor:
for row in cursor:
key = row[0]
if key in UpdateDict:
row[1:] = UpdateDict[key]
cursor.updateRow(row)
arcpy.AddMessage("FC updated Properly")
Just to give you another tip about coding, especially in GIS, it is best to reduce using gp tools in a script or mid script since those create other features that then need to be utilized, which further complicates scripts.
The one cool thing about the geometry methods in arcpy is you can also project features to different projections using projectAs (spatial_reference, {transformation_name}) geometry method.