Tuesday, 1 October 2019

Merging lines (with and with no overlaps) in QGIS


I have two layers ("Layer_1" and "Layer_2") of lines where they sometimes overlap and follow each other for a bit and then takes another route (see the image below).



  • "Layer_1": Red line

  • "Layer_2": Orange line

  • An overlap between Layer 1 and Layer 2: Green line


Example_of_layers


What I'd like to do is to merge these two layers so that there will only be




  1. one line where there is lines overlap including some attribute information saying this is a line which used to be an overlapping line.

  2. other lines having other attribute information, so that I can distinguish these three lines from each other.


It's OK if the output will be three layers, as long as I can distinguish these three categories of lines.



Answer



@oskarlin, I tried to recreate a similar example of your layers. However, I do not know whether your Layers consist of line segments (polylines) or continuous lines (monolines), therefore there are two cases that define the workflow.




CASE 1. When Layers (polylines) consist of segments, i.e. the polyline is split into sections at each vertex or intersection node, see the image below.


Case1_Example



'Layer_2', as well as 'Layer_1', includes two fields, namely "id" and "Value".




  1. For merging two shapefiles, you need to proceed with Vector -> Data Management Tools -> Merge vector layers. Moreover, there are other solutions available, e.g. the MMQGIS plugin does the job (MMQGIS -> Combine -> Merge layers) or SAGA GIS Merge Layers Module from the Processing Toolbox.


    It will provide you with a new polyline layer that includes geometries and attributes of polylines, 'Layer_1' and 'Layer_2' accordingly, see the graphics below. Case1_Output



  2. Then in a new shapefile create a column that defines the overlap value of your layers, e.g. string data type with a length of 1.

  3. In Expression dialogue execute the following expression


    CASE

WHEN sum(num_geometries($geometry), group_by:= geom_to_wkt($geometry)) > 1 THEN 'Y'
ELSE 'N'
END

'Y' will give you all overlapping lines, and 'N' lines that do not overlap.


P.S. If there is a difference in any part of the geometry, this expression fails to match them. @Kazuhito comment


As you can notice from the image below, each Layer has only two line segments where exists an overlap. Moreover, you can easily distinguish between line segments based on values from the Attribute Table. Case1_Result




  1. At this last step, you will need to delete the duplicates based on geometry, however, I am not sure whether you have to remain the "Value" field in case it plays a primary role in your project.








CASE 2. When your layers contain continuous lines, see the image below.


Case2_Example


It's just a matter of running a few more tools.



  1. First, add a field to your original layers with an identifier that will mark features as being from that original layer when they are all merged.

  2. Second, merge the layers. (Check possibilities above)


  3. Then use the Union geoprocessing tool (Vector -> Geoprocessing Tools -> Union) to split features into overlapping and non-overlapping parts. The non-overlapping parts of lines will still be one multipart feature so if you want each segment to be separated, you'll need to run the Multipart to Singleparts... tool (Vector -> Geometry Tools -> Multipart to singleparts).

  4. Finally, create the overlapping field and populate it using the above instructions.


P.S. Answer was completed with the help of @Gabriel C..




References:



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...