I am writing a function to update the value of some features inside a feature class after selecting them with arcpy.SelectLayerByLocation_management().
everything works fine in Pro but when I move to standalone script (that does not reference a specific aprx project) I am having issues.
in Pro I used
desc = arcpy.Describe(inFc)
if desc.FIDSet:
to check if some features was selected by the function SelectLayerByLocation and if so I update the values with a cursor.
In a standalone script this method is not available (DescribeData: Method FIDSet does not exist).
someone say to use arcpy.GetCount_management()
, but this method returns the total number of features in the FC is no feature is selected; this way my cursor will update everything ignoring the SelectLayerByLocation step.
ChatGPT says to create a new layer with the selection made by SelectLayerByLocation but still if 0 features have been selected the newly created temp layer will contain all the features in the main Feature class.
is there a way to solve my problem without referencing the layers in a aprx project but by working on a gdb as I am doing now?
this is my function that works in PRO but not in standalone
def checkFilling(inFc, mode, intersectFc, timeFile):
try:
# Select only gaps contained in the coverage map
arcpy.SelectLayerByLocation_management(inFc, "WITHIN", intersectFc)
# Get the selected feature count
desc = arcpy.Describe("inFc_layer")
if desc.FIDSet:
# Update the end date for selected features
with arcpy.da.UpdateCursor(inFc, ["end_date"]) as cursor:
for row in cursor:
# Update the field value as needed
row[0] = timeFile
cursor.updateRow(row)
print("Update completed successfully.")
else:
print("No features selected.")
except Exception as e:
print("An error occurred: {}".format(str(e)))
timeFile = "25/12/2023"
checkFilling("layerToUpdate", "WITHIN", "layerThatMightContainFeatures", dateVariable)