7

enter image description here

I want to add points "ID" attribute as a default value for a line created between two points, for line layer fields "start_point" and "stop_point".

0
6

If you want to retrieve the point layer ID, you can use these expressions as default values :

  • For start point :
array_first(
    aggregate(
        layer:='point', -- here put the name of the point layer
        aggregate:='array_agg',
        expression:="ID", -- here put the name of the point layer field you want to get
        filter:=intersects($geometry, start_point(geometry(@parent)))
    )
)

  • For end point :
array_first(
    aggregate(
        layer:='point', -- here put the name of the point layer
        aggregate:='array_agg',
        expression:="ID", -- here put the name of the point layer field you want to get
        filter:=intersects($geometry, end_point(geometry(@parent)))
    )
)

  • Explanations :

    • aggregate : create an array with the values of the point layer that intersects your start or end point of your current line

    • array_first : get the first value in the array (normally there is only one)

7
  • Seeing your answer I guess I misunderstood the question. Let me add that you can also use $id instead of "ID" as a fixed field. – MrXsquared Jan 2 at 23:14
  • Maybe, I reformuled the author question with the few elements he let and hope that the author wants. You can use any expression and point layer field, so, I just put a comment and let the user change it as he wants. – J. Monticolo Jan 2 at 23:16
  • 1
    If you remove a feature, the deleted $id will not be assigned again when creating new features. So it should be safe I think. – MrXsquared Jan 3 at 17:35
  • thank you I've tried – kemalferyater Jan 3 at 19:29
  • Could not commit changes to layer hat_ilet_ken Errors: ERROR: 1 feature(s) not added. Provider errors: SQLite error: NOT NULL constraint failed: hat_ilet_ken.pıd SQL: INSERT INTO "hat_ilet_ken"("geom","uzunluk","start_point","end_point","enable") VALUES (ST_Multi(GeomFromWKB(?, 0)),?,?,?,?) gave an error. not saving – kemalferyater Jan 5 at 18:06
5

If you need the IDs of the intersecting points, see J. Monticolo's answer. If you need the lines ID, keep on reading:

Unfortunately, you can not use $id or @row_number directly. Not sure why, but I assume its because you can not determine the id of a not yet existing feature. You will also see this in preview, which returns a negative number, which is not the actual id, if applying abs(). So because the id's do not exist until you save the layer, I'll call them "dummy-ids", thats also the reason why array_last(array_sort(array_agg($id)))+1 or array_max() wont work here.

But here is a workaround you can use:

array_length(array_agg($id))+1

It creates an array of all "not yet existing" as well as all existing features, containing their dummy-id's or actual id's. Its counting the length of this array and adds 1 to it, which results in the same number as $id would be after you created the feature.

enter image description here

4

I keep a copy of QGIS 2.18 to solve problems just like yours. Use the tool Points to Paths (note the plural Paths), which, sadly, was never ported to 3.x. This tool creates an output line from each point pair, where the attributes from the beginning and ending point in each pair are transferred to their respective line in the output layer.

2.18 is easily installed, and it plays nicely with other QGIS versions. You can download 2.18 here.

Points to Paths is installed from Plugins > Manage and Install Plugins... and select PointsToPaths.

When using the tool, make sure that you check on the "Line per vertex" option:

enter image description here

Consider the following hypothetical example: airline flight data that exists as points, with a string of points representing each flight. The point attribute table contains a flight id field as well as a datetime field. Thus, each point is defined by a flight id and timestamp.

With this example, use flight id as the Point group field, and datetime as the Point order field. If datetime is a text field, define it in the Date Format input box.

The output shapefile contains one line per flight id/datetime point pair. Each line segment contains attribute fields that define the flight id, beginning datetime, and ending datetime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.