HOW TO

Remove invalid points using ArcPy in ArcGIS Pro

Last Published: April 26, 2024

Summary

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.

Procedure

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.
  1. In ArcGIS Pro, open the map containing the point feature layer.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  3. Run the following script:
    1. Import the necessary modules.
import arcpy
import datetime
  1. Set up the workspace and the point feature class. Replace <pathToPointFc> with the path of the point feature class. Replace <featureClassName> with the name of the point feature class.
arcpy.env.workspace = r"<pathToPointFc>"
feature_class = "<featureClassName>"
  1. Set the date field to be checked against. Replace <dateFieldName> with the name of the date field.
date_field = "<dateFieldName>"
  1. Set the date threshold to remove the invalid points.
#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)
  1. Start the editing session and the edit operation.
try:
    edit = arcpy.da.Editor(arcpy.env.workspace)
    edit.startEditing(False, True)

   edit.startOperation()
  1. Create a cursor to iterate through the features.
    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")
  1. Stop the editing session and the edit operation.
    edit.stopOperation()

    edit.stopEditing(True)
  1. Print a message when the points are successfully hidden.
    print("Points removed successfully.")
  1. Print out an error message and roll back any edits made during the editing session to prevent data corruption.
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.

The expired points are removed in ArcGIS Pro

Article ID: 000032387

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options