# Find line segment that is not between other lines with QGIS

As shown in below image, I have a project with rivers(blue) which are bordered by fences(green). I have to identify the places that are not guarded by fences. The fences can be further away from the water, anything over 20 meters away does not count as guarding the water anymore.

Im using QGIS, also open for Python solutions (Fiona/Shapely/Geopandas/..)

My best try:

1. Create 4 buffers on the fences: North, East, South, West, using MMQGIS
2. Find overlap of North & South, West & East.
3. Find difference between rivers and fences

However this fails because MMQGIS can appearently no longer create buffers without errors.

• I wonder if you can use this answer to develop a flat end buffer and then do an intersection with the rivers layer in QGIS? Jul 19, 2016 at 13:21
• That would only help me determine whether there is a fence on any side, but not whether there is a fence on both sides right? Checking for an intersection with 2 buffers does not work as there could be two fences on a single side Jul 20, 2016 at 12:53
• The second answer from your link; openJUMP does let me make sidebuffers, working on that right now! Jul 20, 2016 at 13:19

What I would try:

• Densify river lines.
• Draw perpendicular lines on vertices.
• Split perpendicular lines by riverlines.
• Classify perpendicular lines left / right with sidebuffer.
• Analyse linecrossings perpendicular lines with fencelines.
• Join results to vertices.
• Check where left and / or right did not cross fencelines.

I would use FME Desktop (build model - test with sample data for tweaking parameters - run complete dataset) for this kind of stuff. Makes iterating / finding correct parameters a lot less work. I believe that can be done with the QGIS Processing modeler as well. The hard part is: How dense should the vertices be to prevent missing openings without killing performance. And how long can the perpendicular lines be without hitting not near fences.

The following is a 4-step approach with standard QGIS functions which might get what you want. Here's the TLDR:

1. Buffer the fences
2. Split the buffers along all boundaries
3. Extract the overlap areas
4. Remove the parts of the river that fall within the overlaps

Here's a more detailed explanation:

1. Buffering the fences

"Vector --> Geoprocessing Tools --> Buffer(s)..." using a 20 m buffer distance.

2. Splitting the buffers along boundaries

In the Processing Toolbox ("Processing --> Toolbox" or Ctrl+Alt+T), find the GRASS Command "v.clean". Select `bpol` as the Cleaning tool.

This should result in a layer in which the areas where the buffer zones overlap are duplicated, while the buffer areas are not. In effect, the buffer overlaps show you where fences from both sides are within 20 m.

3. Extracting the overlapping areas

In the DB Manager tool ("Database --> DB Manager --> DB Manager") we select our "cleaned" layer as a QGIS Layer and open the SQL window. ("Virtual Layers --> QGIS layers --> [our cleaned buffer layer]" and then the "SQL window" button in the upper left.

Using the following SQL query we select only the polygons that are identical to another polygon in the same layer, i.e. the buffer overlaps. Note: this requires a unique id field on the buffer polygons (which could be inherited from the fence lines).

``````SELECT a.id, a.geometry
FROM Cleaned a, Cleaned b
WHERE ST_EQUALS(a.geometry, b.geometry) AND a.id != b.id
``````

Hit "Execute (F5)" and the check the "Load as new layer" box followed by the "Load now!" button in the lower right.

4. Removing the guarded river sections

"Vector --> Geoprocessing Tools --> Difference..." using the rivers as the Input vector layer and the QueryLayer we loaded in step 3 as the Difference layer.

This image shows how the buffer overlaps can identify the guarded parts of the rivers:

NOTE! This method checks for multiple guarding fences rather than fences on both sides

The caveat of this method is that it finds river sections that are guarded by at most one fence line rather than sections that are guarded on both sides. If a single fence line were to guard both sides of a river, it would be treated as guarded by one fence and thus be included. Conversely, if two fence lines on the same side of a river section are both within 20 m, the river section would be excluded for being guarded by two or more fences.

I guess depending on your data this approach may or may not be acceptable.

• Thanks for your clear answer and elaboration. The problem is that there could be multiple fences on either side of the river. Jul 26, 2016 at 7:11