Select to view content in your preferred language

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

742
1
03-24-2024 06:46 PM
CedricCorbett
Emerging Contributor

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
1 Reply
Clubdebambos
MVP Regular Contributor

Hi @CedricCorbett 

The delete_features() deletes parameter is expecting a string of comma-separated values representing the OIDs to delete not a list of integers

 

object_ids_to_delete = ",".join(str(oid) for oid in object_ids_to_delete)
# eg. "12,44,67,98"

 

 

The delete_features() will not fail if supplied the wrong format it will run successfully and allow you to access information about any failed deletions, check out the documentation. This is why you are getting your print statement executed that "Deleted 2 features", even though they are not deleted.

 

~ learn.finaldraftmapping.com
0 Kudos