I have two line-type shapfiles A and B that have different sizes, but some of them go through the same alignment as the other. These two shapefiles have an attribute table with a column called ident.

I would like to extract the lines from shapefile A that pass in exactly the same alignment as B and have the same value for ident.

shp A

enter image description here

shp B

enter image description here

Line 1A is aligned with line 1B, but line 2A crosses line 1B (I don't want to). So I would like to extract only line 1A if it has the same ident value for both, that is, ident=1A=1B

Update: I added the files from question: https://github.com/wesleysc352/wesleysc352.github.io/raw/master/files_1.zip

enter image description here

  • How many segments a line has? Just one ore it's variable?
    – Mayo
    Dec 18, 2022 at 15:46
  • each line is just one segment Dec 18, 2022 at 15:54

2 Answers 2


Use the Extract by expression tool. Set the Input parameter to layer A, and use the below expression for the Expression parameter.

    layer:='l2', -- set here the name of layer b
            (y(end_point($geometry)) - y(start_point($geometry)))
            (x(end_point($geometry)) - x(start_point($geometry)))
            (y(end_point(@geom_a)) - y(start_point(@geom_a)))
            (x(end_point(@geom_a)) - x(start_point(@geom_a)))
        ) < 0.1 -- set here the threshold, maximum slope difference
        attribute(@parent, 'ident') = "ident"

I'm texting from my phone so maybe there are some parenthesis missing.

  • Important update.
    – Mayo
    Dec 19, 2022 at 23:37
  • I tested your algorithm, however it didn't filter as I expected. Actually it returns shapefile A as the original Dec 24, 2022 at 16:40
  • @wesleysc352, Did you adjust the threshold to your data?
    – Mayo
    Dec 24, 2022 at 22:11
  • yes, i changed the threshold to 0.01 and also to 90 to test Dec 24, 2022 at 22:41
  • where does the term geom_a come from? and why use the expression aggregate:='count'? Dec 26, 2022 at 1:31

In QGIS you can use the Intersect tool to create a new shapefile that contains only the lines from shapefile A that intersect with shapefile B with shapefile A as the input layer and shapefile B as the intersect layer.

Use Select by Attribute tool to select only the lines from the new shapefile that have the same value for the ident attribute as shapefile B.

Finally use Export Selection tool to save the selected lines as a new shapefile.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.