I would like to give each of these polylines an "ID" e.g. 1,2,3,4 etc. from left to right (West to East) or more accurately off a bearing of 125°.
How can I achieve this in QGIS?
I would like to give each of these polylines an "ID" e.g. 1,2,3,4 etc. from left to right (West to East) or more accurately off a bearing of 125°.
How can I achieve this in QGIS?
You can try the 'Add autoincremental field' with x(centroid($geometry)) expression for a 'Sort expression', see image below
EDIT : delete centroid layer creation and add it in the QGIS expression
The best solution I could find is :
Create a line that has a perpendicular orientation with the "lines" and with a length greater than the "line" layer extend;
Get this line coordinates and / or get the WKT : 'LineString(181748.27971816 823480.02355376, 182605.02880682 822909.74994162)';
In the "line" layer, create a new field with this expression by replacing the line WKT :
with_variable(
'line',
geom_from_wkt(
'LineString(181748.27971816 823480.02355376, 182605.02880682 822909.74994162)' -- replace your WKT lineString here
),
with_variable(
'pts_on_line',
aggregate(
@layer,
'array_agg',
round(line_locate_point(@line, centroid($geometry)), 3)
), -- array of distance of features centroids projected on the line
array_find(
array_sort(@pts_on_line), -- sort array by distance
round(line_locate_point(@line, centroid($geometry)), 3) -- find the current feature centroid projection distance in the array
) -- to start number at 1, you can add here : + 1
))
The above expression will number in order the "line" features. Unfortunately, for same "distance" lines, it will number it with different numbers (centroids coordinates imprecision due to not perfect "lines" in the base layer).
+ 1 to start at 1.
– J. Monticolo
Mar 23 '21 at 10:53
Outline algorithm:
Get the X and Y coordinates of the centroids of each feature.
Use a rotation matrix to rotate the centroids by 125 degrees.
Order by X coordinate (assuming I've got the rotation the right way round) and label 1:N
Depending on how you do this (python function, manual clicking of menu options) you then have to relate those points back to the line features, so you may need to keep a separate ID column to match them.
Seeing someone use the Autoincremental field has inspired me to try that. By putting just the Y coordinate of the rotation matrix in the sort expression you can do this. Use:
x(centroid($geometry)) *sin(pi()*125/180) + y(centroid($geometry)) * cos(pi()*125/180)
which is the Y coordinate of the centroid rotated 125 degrees.
and then you get this:
the labels here aren't consecutive because the segments above are interweaved between them. Here they are: