I want to add points
"ID" attribute as a default value for a line created between two points, for line layer fields
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))) ) )
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)
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
@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_max() wont work here.
But here is a workaround you can use:
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.
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:
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.