Help with - Parameters not valid for delete_features & Deleted 2 Features

156
0
03-24-2024 06:46 PM
CedricCorbett
New Contributor II

Hello, I am trying to run a delete features function for any expired feature/point layer. After running the code in Esri Notebook I am receiving "parameters not valid for delete_features" and "Deleted 2 features". There are only two features in the map I am testing the code on (neither should be deleted, expecting a return of "No Features to Delete"). When I revisit the map the feature points are still there. Code below:

# Function to determine if the feature's desired_time_frame has expired
def is_time_frame_expired(creation_date, desired_time_frame):
current_date = datetime.datetime.now()
difference = current_date - creation_date

# Define the maximum age for each time frame
max_ages = {
"0-3 months": 90,
"3-6 months": 180,
"6-9 months": 270,
"9-12+ months": 365 # Adjust as necessary for "12+ months"
}

# Calculate the age of the feature in days
age_in_days = difference.days

# Get the maximum age for the feature's desired_time_frame
max_age = max_ages.get(desired_time_frame, 0)

# Check if the feature's age exceeds the maximum age for its time frame
return age_in_days > max_age

# Query all features (adjust as needed for performance)
features = survey.query(where="1=1").features

# List to hold Object IDs of features to delete
object_ids_to_delete = []

# Process each feature
for feature in features:
creation_date = feature.attributes['CreationDate']
creation_date = datetime.datetime.fromtimestamp(creation_date / 1000) # Assuming the date is in UNIX timestamp format

# Correct the attribute name to match the one in your feature layer
desired_time_frame = feature.attributes['desired_time_frame'] # Adjust the attribute name as necessary

# Check if the feature's desired_time_frame has expired
if is_time_frame_expired(creation_date, desired_time_frame):
# Find the correct unique identifier field name and use it
object_id_field_name = survey.properties.objectIdField
object_ids_to_delete.append(feature.attributes[object_id_field_name])

# Delete features if any are to be deleted
if object_ids_to_delete:
delete_result = survey.delete_features(deletes=object_ids_to_delete)
print(f"Deleted {len(object_ids_to_delete)} features.")
else:
print("No features to delete.")

0 Kudos
0 Replies