当前位置:网站首页>ArcGIS Pro脚本工具(5)——排序后删除重复项
ArcGIS Pro脚本工具(5)——排序后删除重复项
2022-06-30 09:37:00 【学点GIS】
需求描述
每个市有多个高速出入口,每个高速出入口与所在市的市政府的距离已经包含在属性表中,如何留下每个市里距离市政府最近,也就是NEAR_DIST值最小的那个高速出入口呢?

解决思路
ArcGIS自带了 删除相同项 工具,但是只能留下 OID 最靠前的那一项,而这一项往往不是我们希望保留的。为了满足需求,先通过 SearchCursor 构建存在重复的要素的sql表达式,再用Updatecursor 对每一组重复项按需求排序,保留最靠前的一项,删除之后的所有项。使用脚本构建地理处理工具,参数设置和界面如下。

Python脚本
# coding=utf-8
import arcpy
from collections import Counter
fc = arcpy.GetParameterAsText(0)
fieldVaule = arcpy.GetParameterAsText(1)
field_names = fieldVaule.split(";")
field_count = len(field_names)
field_types = []
i = 0
while i < field_count:
field_type = arcpy.ListFields(fc, field_names[i])[0].type
field_types.append(field_type)
i = i+1
def quotes(type):
if type == "String":
return "'"
else:
return ""
value = []
with arcpy.da.SearchCursor(fc, field_names=field_names) as cursor:
for row in cursor:
i = 0
clause = ""
while i < field_count:
quote = quotes(field_types[i])
clause = clause+field_names[i]+" = " + \
quote+str(row[i])+quote+" And "
i = i+1
clause = clause[:-5]
value.append(clause)
del row
del cursor
counts = Counter(value)
dupValue = [id for id in value if counts[id] > 1]
uniValue = list(set(dupValue))
order = arcpy.GetParameter(2)
orderColumn = order.columnCount
orderRow = order.rowCount
switch = {"保留最小值": "ASC",
"保留最大值": "DESC", }
i = 0
orderclause = ""
while i < orderRow:
orderfid = order.getValue(i, 0)
ordertype = order.getValue(i, 1)
orderclause = orderclause+orderfid+" "+switch.get(ordertype)+";"
i = i+1
for uni in uniValue:
with arcpy.da.UpdateCursor(fc, field_names=field_names, where_clause=uni, sql_clause=(None, 'ORDER BY ' + orderclause)) as cursor:
next(cursor)
while True:
try:
next(cursor)
cursor.deleteRow()
except StopIteration:
break
arcpy.AddMessage("已删除 "+uni+" 的相同项")
del cursor
工具演示

工具说明
- 适用于ArcGIS Pro和Python 3,如果想在ArcMap和Python 2中使用,需要处理一下中文字符编码
- 与ArcGIS自带的 删除相同项 工具一样,本工具不生成新的数据集,而是直接在原有的数据集上删除相同项,使用前请注意备份数据
- 工具中的 字段 和 排序字段 可根据需要设置多个值和多种类型
- 排序方法 中的保留最小值 对应的是升序,保留最大值 对应的是降序,工具保留的是每组相同项排序后的第一项
边栏推荐
- What is the real performance of CK5, the king machine of CKB?
- Guolin was crowned the third place of global popularity of perfect master in the third quarter of 2022
- Didn't receive robot state (joint angles) with recent timestamp within 1 seconds
- "Kunming City coffee map" activity was launched again
- Magnetic levitation 3D lamp
- 乡村振兴公益基金启动暨古茶树非遗保护公益行发布
- Regular expression Basics
- GD32 RT-Thread DAC驱动函数
- About Jul
- Enter the world of helium (hNT) hotspot servers to bring you different benefits
猜你喜欢

2022第六季完美童模 合肥赛区 初赛圆满落幕

MIT-6874-Deep Learning in the Life Sciences Week5

Magnetic levitation 3D lamp

MIT-6874-Deep Learning in the Life Sciences Week5

Splendid China: public welfare tourism for the middle-aged and the elderly - entering Foshan nursing home

South China Industrial Group launched digital economy and successfully held the city chain technology conference

Js獲取指定字符串指定字符比特置&指定字符比特置區間的子串【簡單詳細】

Oracle creates a stored procedure successfully, but the compilation fails

移植完整版RT-Thread到GD32F4XX(详细)

MIT-6874-Deep Learning in the Life Sciences Week6
随机推荐
【C语言快速上手】带你了解C语言,零基础入门③
keras ‘InputLayer‘ object is not iterable
Didn't receive robot state (joint angles) with recent timestamp within 1 seconds
The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art
半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
Oracle creates a stored procedure successfully, but the compilation fails
Automated stock trading ensemble strategy based on Reinforcement Learning
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
Article content cannot be copied
G-Code 详解
Input limit input
Magnetic levitation 3D lamp
100个句子记完7000个雅思词汇,实际只有1043个词汇(包括 I and you 等简单词汇)
SolidWorks质量特性详解(惯性张量、转动惯量、惯性主轴)
Installation and use
GNN hands on practice (II): reproduction graph attention network gat
KOREANO ESSENTIAL打造气质职场范
Configure Yii: display MySQL extension module verification failed
Deploy lvs-dr cluster
GD32 RT-Thread RTC驱动函数