0

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()
4
  • Change the field type by refactoring (use this tool to refactor in QGIS), or convert the python float to integer with the int function (int(p2.x() + p1.x())) if you don't care about loosing the decimal precision.
    – Mayo
    Commented Sep 8, 2023 at 13:52
  • I tried this and got the error: argument 1 has unexpected type 'int'
    – Mestrexama
    Commented Sep 9, 2023 at 1:46
  • So what type of field do you have?
    – Mayo
    Commented Sep 9, 2023 at 1:59
  • Double type field
    – Mestrexama
    Commented Sep 9, 2023 at 2:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.