操作方法
在 ArcGIS Pro 中,包含日期字段的点要素对于追踪一段时间内的模式非常重要。 有时,这些点包含指定的日期持续时间,并且不再有效,无法显示在地图上。 这对于避免混淆和提升数据清晰度非常重要,因为无效点可能会造成混淆,由此导致对数据的误解。
本文介绍了在 ArcGIS Pro 中使用 ArcPy 移除无效点的工作流。
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)
以下代码块演示了完整脚本。
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)
下图显示了使用 ArcPy 移除的过期点。 刷新地图以应用更改。

文章 ID: 000032387
获取来自 ArcGIS 专家的帮助
立即开始聊天