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".
- 32,823
- 4
- 66
- 137
- 97
- 1
- 1
- 7
3 Answers
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)
- 15,695
- 1
- 29
- 64
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.
- 34,292
- 21
- 67
- 117
[EDIT: Points to Paths has been updated, and is now available at 3.x]. 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.
- 8,252
- 8
- 35
- 84



$idinstead of"ID"as a fixed field. – MrXsquared Jan 02 '21 at 23:14$idwill not be assigned again when creating new features. So it should be safe I think. – MrXsquared Jan 03 '21 at 17:35Errors: 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)),?,?,?,?)
– kemalferyater Jan 05 '21 at 18:06gave an error. not saving
AFTER INSERT OR UPDATE(see https://www.sqlitetutorial.net/sqlite-trigger/). – J. Monticolo Jan 05 '21 at 19:15