问题描述
所以该字段来自矢量图层属性表。我想要的是当名为“距离”的字段中的每一行值从前一个值中减去时能够得到结果,我得到一个结果,然后我可以将其用于其他计算。所以基本上我想能够说:第 4 列中的第 3 行减去第 4 列中的第 2 行(相同的列但不同的行相互减去)。我的代码如下所示:
fn = ‘C:/PLUGINS1/sample/checking.shp’
layer = iface.addVectorLayer(fn,”,‘ogr’)
layer=iface.activeLayer()
idx=layer.fields().indexFromName(‘distance’)
with edit(layer):
for f in layer.getFeatures():
dist1 = float(row[2],column [4]) # since row 1 contains the field name
dist2 = float(row[3],column [4])
final = abs(dist2 – dist1)
解决方法
这确实有效:
fn = 'C:/PLUGINS1/sample/checking.shp'
layer = iface.addVectorLayer(fn,'','ogr') # '' means empty layer name
for i in range(0,1):
feat1 = layer.getFeature(i)
ab = feat1[0] # this is the first column and first row value
for i in range(0,2):
feat2 = layer.getFeature(i)
dk = feat2[0] # this is the first column and second row value
lenggok = (dk - ab) # Note this is the difference between both rows
print(lenggok)