我在表格中有一些数据:
ID A B VALUE EXPECTED RESULT
1 1 2 5 GROUP1
2 2 3 5 GROUP1
3 3 4 6 GROUP2
4 3 5 5 GROUP1
5 6 4 5 GROUP3
我要做的是遍历数据(数千行)并创建一个公共字段,以便能够轻松地连接数据(*a->;开始节点,B->;结束节点值->;顺序…数据形成类似于链的形式,其中只有邻居共享公共a或B)
加入规则:
组中所有元素的值相等
元素一的A等于元素二的B(或元素二的B,但不是A=A'或B=B')
最困难的是:将构成一系列相交节点的所有顺序数据分配给同一组
也就是说,第一个元素[1 2 5]必须与[2 2 3 5]连接,然后与[4 3 5 5]连接
当遍历大量数据时,您知道如何稳健地实现这一点吗?我对第三条规则有问题,其他规则很容易应用。对于有限的数据,我取得了一些成功,但这取决于我开始检查数据的顺序。这对大型数据集不起作用。 我可以使用arcpy(最好)甚至Python、R或Matlab来解决这个问题已经尝试过arcpy,但没有成功,因此我正在寻找替代方案。
在ArcPy中,该代码可以正常工作,但扩展有限(即,在具有多个段的大型功能中,我得到3-4个组,而不是1个组):
TheShapefile="c:/Temp/temp.shp"
desc = arcpy.Describe(TheShapefile)
flds = desc.fields
fldin = 'no'
for fld in flds: #Check if new field exists
if fld.name == 'new':
fldin = 'yes'
if fldin!='yes': #If not create
arcpy.AddField_management(TheShapefile, "new", "SHORT")
arcpy.CalculateField_management(TheShapefile,"new",'!FID!', "PYTHON_9.3") # Copy FID to new
with arcpy.da.SearchCursor(TheShapefile, ["FID","NODE_A","NODE_B","ORDER_","new"]) as TheSearch:
for SearchRow in TheSearch:
if SearchRow[1]==SearchRow[4]:
Outer_FID=SearchRow[0]
else:
Outer_FID=SearchRow[4]
Outer_NODEA=SearchRow[1]
Outer_NODEB=SearchRow[2]
Outer_ORDER=SearchRow[3]
Outer_NEW=SearchRow[4]
with arcpy.da.UpdateCursor(TheShapefile, ["FID","NODE_A","NODE_B","ORDER_","new"]) as TheUpdate:
for UpdateRow in TheUpdate:
Inner_FID=UpdateRow[0]
Inner_NODEA=UpdateRow[1]
Inner_NODEB=UpdateRow[2]
Inner_ORDER=UpdateRow[3]
if Inner_ORDER==Outer_ORDER and (Inner_NODEA==Outer_NODEB or Inner_NODEB==Outer_NODEA):
UpdateRow[4]=Outer_FID
TheUpdate.updateRow(UpdateRow)
和shapefile form and dbf form中的一些数据
使用matlab:
以下是输出:
果然
相关问题 更多 >
编程相关推荐