I have this piece of code that I can make lines between points and calculate the length of the line, but now I need to calculate the accumulated distance only between the X axis, I managed to use expressions like p2.x() + p1.x()
to calculate this but when I try to put it in a field it says that it is not possible because the type of the variable is float. How could I do this?
iter1 = layer.getFeatures()
for feature1 in iter1:
p1 = feature1.geometry().asPoint()
# loop all points again:
iter2 = layer.getFeatures()
for feature2 in iter2:
# following line creates lines from one feature to previous feature:
if feature2.id() == feature1.id() + 1:
# create new line feature:
p2 = feature2.geometry().asPoint()
l = QgsGeometry.fromPolylineXY([p1,p2])
linha = QgsFeature()
linha.setGeometry(l)
linha.setAttributes([l.length()])
pr.addFeature(linha)
vl.updateFields()
int
function (int(p2.x() + p1.x())
) if you don't care about loosing the decimal precision.