I'm using QGIS 2.14.4-Essen. I have two layers:
- points.shp that contains points with a YEAR column
- lines.shp that contains lines that exactly connect the points from points.shp
I would like to get the YEAR attribute from points.shp back to lines.shp. Each line is a single segment with one point at each end (see picture below). What I would like to get is YEAR from first point and YEAR from second point back into each line attributes.
For instance: line 1 is touching a first point with YEAR=2010 and a second point with YEAR=2011. I would like to get back something like '2010-2011' into line 1 attributes. The result should look like this:
id | points
----+-----------------
1 | 2010-2011
2 | 2011-2012
3 | 2012-2016
Is there a way to achieve this using QGIS?
Answer
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.
No comments:
Post a Comment