Getting points-touching-a-line attributes back to lines layer using QGIS?
Although @radouxju answer is valid, I will explain it a little more detailed.
- You need to make sure that the polyline feature is split exactly above the point locations.
- Use
Join attribute by location
. Choose the split line feature at point locations as target layer - in my case I name it "exploded". - In the summary section, select "Take summary of intersecting feature". Here, instead of running the tool two times; one for Min and another time for Max, you can run it one time and choose both Min and Max.
- The out file will have the following attribute:
- Add new field of type string with a name "Year" to the new shapefile from step 4.
Use Field calculator, and go to update existing field. Select "Year" and write the following expression:
to_string( "MINYEAR2" ) + '-' + to_string( "MAXYEAR2" )
The final output attribute will look like this:
- The final output will be like this:
Here is the output after testing your data:
The table on the left is the point data after creating a new field of type integer, and the table on the right is after joining the line with point data using step 2 mentioned above. Then I used steps 5-6 to create the final data.
Update
I tried to convert the field YEAR from string to integer using expression to_int() and it worked. So you don't need to do it manually. However, The field type should be of type Integer
not Integer64
. Make sure that the field length is up to 9. If you chose a field length of 10, it will be converted to Interger64
, and it will not work with Interger64
. Then you can follow the process from step 2-6
Here is the final output after using the expression to_int():
In the above attribute table on the left, the YEAR is of type string and the YEAR2 is of type integer converted using expression to_int(). You can see in the attribute table on the right after following steps 2-6, I got MINYEAR2 and MAXYEAR2, then converted to string back again to concatenate everything together in the field YEAR.
When join join points to line you will have multiple relationship. With "join attribute by location", you will be able to ask for a given summary method. In your case, do this twice : once with minimum and once with maximum. After that you can concatenate the two fields in a new field and you end up with what you need.
In field calculator :
tostring(minYEAR) + '-' + tostring(maxYEAR)
Assuming the topology is perfect, creating a field 'WKT' with the the expression
geom_to_wkt( $geometry)
in your point layer, you could use the expression:
min( attribute( get_feature('points','WKT', geom_to_wkt(start_point($geometry) )),'year'),attribute( get_feature('points','WKT', geom_to_wkt(end_point($geometry) )),'year'))||'-'|| max( attribute( get_feature('points','WKT', geom_to_wkt(start_point($geometry) )),'year'),attribute( get_feature('points','WKT', geom_to_wkt(end_point($geometry) )),'year'))
in the field calculator of the pipe layer, creating a text string.
- attribute(feature, attribute_name) Returns the value of a specified
attribute from a feature, here, the year of the point feature
obtained. - get_feature(layer, attribute, value) returns the first feature of a layer matching a given attribute value. Here we check if we
can find a point with the same coordinates (in WKT format) as the
ones of your line's start and end vertices. - start_point(geometry) returns the first node from a geometry. Here the first vertex of your line.
- end_point(geometry) returns the last node from a geometry. Here the last vertex of your line.
- geom_to_wkt(geometry) returns the Well-Known Text (WKT) representation of the geometry.
You could even update it to:
CASE
WHEN attribute( get_feature('points','WKT', geom_to_wkt(start_point($geometry) )),'year') = attribute( get_feature('points','WKT', geom_to_wkt(end_point($geometry) )),'year')
THEN attribute( get_feature('points','WKT', geom_to_wkt(end_point($geometry) )),'year')
ELSE min( attribute( get_feature('points','WKT', geom_to_wkt(start_point($geometry) )),'year'),attribute( get_feature('points','WKT', geom_to_wkt(end_point($geometry) )),'year'))||'-'|| max( attribute( get_feature('points','WKT', geom_to_wkt(start_point($geometry) )),'year'),attribute( get_feature('points','WKT', geom_to_wkt(end_point($geometry) )),'year'))
END
in order to show just one year if two points with the same year are connected (getting 200X instead of 200X-200X).
The main advantage of this method is that if your data changes in your points, you can update it very fast with just one field calculator.
You could even add this rule as an Autofield for when you create new lines.
Cheers,