[GIS] Lines and polygons intersection with tolerance in QGIS

intersectionlinepolygonqgistolerance

I have a layer with polygons and a layer with polylines.

I am looking for the list of polygons crossed by the polylines.

Just here it's relatively simple: intersection function.

That said, the layout of the line is not perfect, so I would like to exclude polygon for which the line only touches the polygon contour or does not fit more than 1m into the polygon.

Best Answer

I'd go for SQL (QGIS natively supports the SpatiaLite 4.3.0 dialect for any layer query) and check for the spatial relation with the negative buffered polygons:

  • QGIS | DB Manager

    • connect to Virtual Layers (double-click) in the left panel (make sure your layers are loaded in the project)
    • open the SQL Window (second icon to the left)

      • in the top panel, run (Execute)

        SELECT a.<id>,
               group_concat(b.<id>) AS its_lines,
               a.geometry 
        FROM <polygon_layer> AS a
        JOIN <line_layer> AS b
          ON ST_Relate(ST_Buffer(a.geometry, -1), b.geometry, 'T********')
        GROUP BY a.id;
        
      • load the produced table as a layer to the project by ticking the Load as new layer checkbox and fill in the fields

The above query will return a table with all polygon <id>s (paste the actual id field name here), their geometries and a concatenated string of line <id>s (same here) that intersect, but not touch, the negative buffered polygons of your layer.

Important:
Since you asked for meters, I put -1 in ST_Buffer. However, the function will treat the passed number in CRS units; here, this implies that you are working with a CRS whose units are in meter! If you are working in LatLon, e.g. EPSG:4326, the parameter in ST_Buffer will be treated as degrees...and thus makes no sense. In that case, a transformation into an appropriate projection is necessary; you can do that with
... ST_Buffer(ST_Transform(a.geometry, <srid_of_metric_CRS>), -1) ...

Related Question