使用ArcPy根据其他字段选择最大值

1548
13
跳到解决方案
06-27-2021 08:28 am
badrinathkar
通过
新贡献者III

我想填充一个字段(MaxElv),根据Id字段(Id)从另一个字段(Elevation)中选择最高值。数据是这样的:

Id海拔高度MaxElv 1 50
1 72
1 83
1 66
2 105年
3 27
3个24
2 98年
1 46
3 22
2 96年
3 26
2 99年
4 25
4 21
4日22
3 28
1 52

我开发了以下代码:
>>>导入arcpy
…fc = '国家'
…CountryList = []
…arcpy.da。搜索Cursor(fc, ["Id"]) as cur1:
…对于cur1中的row1:
…如果row1[0]不在CountryList中:
…CountryList.append(第一行[0])

…对于CountryList中的i:
…ElevList = []
…其中= """{}={}"""。格式(“Id”,我)
…arcpy.da。搜索Cursor(fc, ["Elevation"], where) as cur2:
…对于cur2中的row2:
…如果row2[0]不在列表中:
…ElevList.append (row2 [0])
…ElevList.sort ()
…max_Elev = ElevList [1]
…where2 = """{} = '{}' AND{} = '{}' ""。format("Id", i, "Elevation", max_Elev)
…arcpy.da。UpdateCursor(fc, ["MaxElv"], where2) as cur3:
…对于cur3中的行3:
…Row3 [0] = 1
…cur3.updateRow (row3)


但它给出了以下错误
运行时错误
>>>导入arcpy…fc = 'Country'…CountryList =[]…arcpy.da。搜索Cursor(fc, ["Id"]) as cur1: ... for row1 in cur1: ... if row1[0] not in CountryList: ... CountryList.append(row1[0]) ... ... for i in CountryList: ... ElevList = [] ... where = """{} = {}""".format("Id", i) ... with arcpy.da.SearchCursor(fc, ["Elevation"], where) as cur2: ... for row2 in cur2: ... if row2[0] not in ElevList: ... ElevList.append(row2[0]) ... ElevList.sort() ... max_Elev= ElevList[-1] ... where2 = """{} = '{}' AND {} = {}""".format("Id", i, "Elevation", max_Elev) ... with arcpy.da.UpdateCursor(fc, ["MaxElv"], where2) as cur3: ... for row3 in cur3: ... row3[0] = 1 ... cur3.updateRow(row3) But it gives the following error Runtime error Traceback (most recent call last): File "", line 20, in  RuntimeError: An invalid SQL statement was used.

回溯(最近一次调用):
文件"",第20行,在
RuntimeError:使用了无效的SQL语句。

0荣誉
1解决方案

接受的解决方案
DanPatterson
通过 MVP尊敬贡献者
MVP尊敬贡献者

汇总统计(分析)-ArcGIS Pro |文档

Id是你的案件领域吗?

数据字段是标高与最大值作为选项(或其他)

跳过maxelev的内容,如果需要,只需连接结果表并持久化连接


…有点退休了……

在原帖子中查看解决方案

13日回复
JayantaPoddar
通过 MVP尊敬贡献者
MVP尊敬贡献者

如果您可以使用“插入/编辑代码示例”选项添加Python脚本,将会有所帮助。

jayantapoddar_0 - 1624807999388. - png



认为位置
0荣誉
badrinathkar
通过
新贡献者III

先生,我已经编辑了这篇文章。请参阅。

0荣誉
JoshuaBixby
通过 MVP尊敬贡献者
MVP尊敬贡献者

这些问题在Esri社区中经常被问到,大概每4-6个月一次。188金宝博复式188金宝搏网址导航解决这个问题没有唯一的方法,但有些方法比其他方法更有效。在您的情况下,该方法是效率较低的方法之一,因此我不会讨论您的无效SQL语句,而是建议使用SQL排序的另一种方法。

flds = ["Id", "Elevation", "MaxElv"] sql = "ORDER BY Id, Elevation DESC" with arcpy.da。UpdateCursor(lyr, flds, sql_clause=(None, sql)) as cur: eid, elev, max_elev = next(cur) max_elev = elev cur. updaterow ([eid, elev, max_elev]) prev_eid = eid for eid, elev, _ in cur: if eid != prev_eid: max_elev = elev cur. updaterow ([eid, elev, max_elev]) prev_eid = eid . updaterow ([eid, elev, max_elev]

badrinathkar
通过
新贡献者III

很抱歉这么说!但是,您的脚本仅使用Elevation的值填充MaxElv字段,它首先与相同的id进行冲突。

0荣誉
JayantaPoddar
通过 MVP尊敬贡献者
MVP尊敬贡献者
sql = "ORDER BY Id, Elevation DESC"

我觉得这句话很管用。每个ID的第一个Elevation Value是Max_Elev值。

约书亚的剧本非常适合我。

你得到了什么结果/异常(截图)?



认为位置
badrinathkar
通过
新贡献者III

先生,请看附件的图。

Error1.png

0荣誉
JoshuaBixby
通过 MVP尊敬贡献者
MVP尊敬贡献者

我看到你有一个“FID”字段,我假设你正在使用形状文件。如果您查阅文档,UpdateCursor-ArcMap | Documentation (arcgis.com)

DISTINCT、ORDER BY和ALL仅在处理数据库时支持。其他数据源(如dBASE或INFO表)不支持它们。

简而言之,我的方法不适合您,因为您正在处理形状文件。

0荣誉
badrinathkar
通过
新贡献者III

我想填充一个字段(MaxElv),根据Id字段(Id)从另一个字段(Elevation)中选择最高值。数据是这样的:

Id海拔高度MaxElv 1 50
1 72
1 83
1 66
2 105年
3 27
3个24
2 98年
1 46
3 22
2 96年
3 26
2 99年
4 25
4 21
4日22
3 28
1 52

我开发了以下代码:
>>>导入arcpy
…fc = '国家'
…CountryList = []
…arcpy.da。搜索Cursor(fc, ["Id"]) as cur1:
…对于cur1中的row1:
…如果row1[0]不在CountryList中:
…CountryList.append(第一行[0])

…对于CountryList中的i:
…ElevList = []
…其中= """{}={}"""。格式(“Id”,我)
…arcpy.da。搜索Cursor(fc, ["Elevation"], where) as cur2:
…对于cur2中的row2:
…如果row2[0]不在列表中:
…ElevList.append (row2 [0])
…ElevList.sort ()
…max_Elev = ElevList [1]
…where2 = """{} = '{}' AND{} = '{}' ""。format("Id", i, "Elevation", max_Elev)
…arcpy.da。UpdateCursor(fc, ["MaxElv"], where2) as cur3:
…对于cur3中的行3:
…Row3 [0] = 1
…cur3.updateRow (row3)


但它给出了以下错误
运行时错误
回溯(最近一次调用):
文件"",第20行,在
RuntimeError:使用了无效的SQL语句。

标签(3)
0荣誉
JoshuaBixby
通过 MVP尊敬贡献者
MVP尊敬贡献者

问题交叉贴://www.gobook3.com/t5/arcmap-questions/selecting-maximum-value-based-on-other-field-using-ar..。@badrinathkar我建议删除这条,因为人们已经开始回复另一条了。在将来,可以询问与python相关的问题,这些问题不是特定于ArcMap或Pro的//www.gobook3.com/t5/python/ct-p/python

0荣誉