HOW TO
In ArcGIS Pro, point features containing date fields are important to track patterns over time. Sometimes, these points contain a specified date duration and are no longer valid to be displayed on the map. This is important to avoid confusion and to improve data clarity, as invalid points can create confusion, leading to misinterpretation of the data.
This article describes the workflow to remove invalid points using ArcPy in ArcGIS Pro.
Note: This workflow requires a full script to run in the ArcGIS Pro Python window. All the indents are to be kept as portrayed in the code block.
import arcpy
import datetime
arcpy.env.workspace = r"<pathToPointFc>"
feature_class = "<featureClassName>"
date_field = "<dateFieldName>"
#If using the Date field type
threshold_date = datetime.datetime(YYYY, MM, DD)
#If using the Date Only field type
threshold_date = datetime.date(YYYY, MM, DD)
try:
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False, True)
edit.startOperation()
with arcpy.da.UpdateCursor(feature_class, ["OID@", date_field]) as cursor:
for row in cursor:
date_value = row[1]
# Check if date is not None and if it's expired
if date_value:
if date_value < threshold_date:
print(f"Deleting feature with OID {row[0]} and date {date_value}")
cursor.deleteRow()
else:
print(f"Skipping feature with OID {row[0]} because date field is None")
edit.stopOperation()
edit.stopEditing(True)
print("Points removed successfully.")
except Exception as e:
print(e)
edit.stopEditing(False)
The code block below demonstrates the full script.
import arcpy
import datetime
arcpy.env.workspace = r"C:\Users\Documents\ArcGIS\Projects\MyProject40\MyProject40.gdb"
feature_class = "Pointsss"
date_field = "Date"
threshold_date = datetime.datetime(2023, 1, 1)
try:
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False, True)
edit.startOperation()
with arcpy.da.UpdateCursor(feature_class, ["OID@", date_field]) as cursor:
for row in cursor:
date_value = row[1]
if date_value:
if date_value < threshold_date:
print(f"Deleting feature with OID {row[0]} and date {date_value}")
cursor.deleteRow()
else:
print(f"Skipping feature with OID {row[0]} because date field is None")
edit.stopOperation()
edit.stopEditing(True)
print("Points removed successfully.")
except Exception as e:
print(e)
edit.stopEditing(False)
The image below shows the expired points removed using ArcPy. Refresh the map to apply the changes.
Get help from ArcGIS experts
Download the Esri Support App