当前位置:网站首页>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自带的 删除相同项 工具一样,本工具不生成新的数据集,而是直接在原有的数据集上删除相同项,使用前请注意备份数据
- 工具中的 字段 和 排序字段 可根据需要设置多个值和多种类型
- 排序方法 中的保留最小值 对应的是升序,保留最大值 对应的是降序,工具保留的是每组相同项排序后的第一项
边栏推荐
- MySQL index, transaction and storage engine of database (2)
- 新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
- Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
- Article content cannot be copied
- MIT-6874-Deep Learning in the Life Sciences Week4
- Curl --- the request fails when the post request parameter is too long (more than 1024b)
- Go -- standard library sort package
- MySQL advanced SQL statement of database (2)
- 《锦绣中华》中老年公益文旅游-走进佛山敬老院
- Rider does not prompt after opening unity script
猜你喜欢
![[AGC] build service 3- authentication service example](/img/32/44547c00476a055557dd1790e18849.png)
[AGC] build service 3- authentication service example

开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

Rider does not prompt after opening unity script

Configure Yii: display MySQL extension module verification failed

Oracle creates a stored procedure successfully, but the compilation fails

调试方法和技巧详解

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

Theme Studio

“昆明城市咖啡地图”活动再度开启

新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
随机推荐
The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded
Koreano essential creates a professional style
About the split and join operations of strings
Jump table introduction
100个句子记完7000个雅思词汇,实际只有1043个词汇(包括 I and you 等简单词汇)
Article content cannot be copied
GNN动手实践(二):复现图注意力网络GAT
Rider does not prompt after opening unity script
Deploy lvs-dr cluster
Quick completion guide for manipulator (4): reducer of key components of manipulator
MySQL index, transaction and storage engine of database (1)
GD32 RT-Thread RTC驱动函数
Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
How to seize the opportunity of NFT's "chaos"?
train_ de.py: error: argument --save_ steps: invalid int value: ‘$[$[889580/128/4]*10/2]‘
"Kunming City coffee map" activity was launched again
Right click to open CMD (command line)
Xinguan has no lover, and all the people benefit from loving deeds to warm the world -- donation to the public welfare action of Shangqiu children's welfare home
逸仙电商发布一季报:坚持研发及品牌投入,实现可持续高质量发展