操作方法

操作方法:在 ArcGIS Pro 中使用 ArcPy 移除无效点

Last Published: April 26, 2024

描述

在 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.
  1. 在 ArcGIS Pro 中,打开包含点要素图层的地图。
  2. 打开 Python 窗口。 有关详细信息,请参阅 ArcGIS Pro:Python 窗口
  3. 运行以下脚本:
    1. 导入所需的模块。
import arcpy
import datetime
  1. 设置工作空间和点要素类。 将 <pathToPointFc> 替换为点要素类的路径。 将 <featureClassName> 替换为点要素类的名称。
arcpy.env.workspace = r"<pathToPointFc>"
feature_class = "<featureClassName>"
  1. 设置要进行检查的日期字段。 将 <dateFieldName> 替换为日期字段的名称。
date_field = "<dateFieldName>"
  1. 设置日期阈值以移除无效点。
#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. 启动编辑会话和编辑操作。
try:
    edit = arcpy.da.Editor(arcpy.env.workspace)
    edit.startEditing(False, True)

   edit.startOperation()
  1. 创建一个游标,用于遍历要素。
    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. 停止编辑会话和编辑操作。
    edit.stopOperation()

    edit.stopEditing(True)
  1. 成功隐藏点后,将输出消息。
    print("Points removed successfully.")
  1. 输出错误消息并回滚在编辑会话期间进行的任何编辑内容,以防止数据损坏。
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 移除的过期点。 刷新地图以应用更改。

将在 ArcGIS Pro 中移除过期点

文章 ID: 000032387

获得人工智能支持

使用 Esri Support AI Chatbot 快速解决您的问题。

立即开始聊天

相关信息

发现关于本主题的更多内容

获取来自 ArcGIS 专家的帮助

联系技术支持部门

立即开始聊天

转至下载选项