# Intersections (line and polygon) taking long time in QGIS

I'm intersecting some lines with polygons in QGIS v3.20 and it is taking a very long time to run the tool (hours and hours).

The linework is complicated and had a large number of attributes. Likewise, there are a large number of polygons to intersect it with.

I have tried both using shapefiles and importing them into the same geopackage, but both methods are very slow.

I have tried building a spatial index, but even on my tiny test sample data this took a few minutes to process for less than 100 features.

My computer is very powerful (128GB ram) and I use it for manipulating climate data so I'm puzzled as to why QGIS is so slow.

Are there alternative tools I could use in QGIS?

I'm certain ArcGIS was much faster when I had a licence for that, but currently that is not in the budget!

• Are they big multilines/polygons? How many lines and polygons is it?
– Bera
Commented Sep 24, 2021 at 16:53
• Do the geometries contain geometry/topology errors? Commented Sep 24, 2021 at 16:53
• @BERA the polygons are multipolygons (197 different classes, but lots of individual polys) Commented Sep 24, 2021 at 17:18
• @BERA the lines have 1.4 million features Commented Sep 24, 2021 at 17:20
• @Babel I did fix the geometry but did not look for topological errors. I presume there is a fix topology tool? Commented Sep 24, 2021 at 17:21

Here is how you can achieve this using the shapely library in Python:

``````# Import the necessary modules
from shapely.geometry import LineString, Polygon

# Define the coordinates of the line and the polygon
line_coordinates = [(0, 0), (1, 1)]
polygon_coordinates = [(0, 0), (0, 1), (1, 1), (1, 0)]

# Create a LineString and a Polygon from the coordinates
line = LineString(line_coordinates)
polygon = Polygon(polygon_coordinates)

# Use the intersection() method to intersect the line and polygon
intersection = line.intersection(polygon)

# Print the intersection geometry
print(intersection)
``````

In this example, the intersection variable will contain a Point geometry representing the intersection of the line and the polygon. You can use the `geom_type` attribute to determine the type of geometry returned by the `intersection()` method.

I would stick to shape file format since its less verbose to load. Try using the clip tool rather than the intersection tool but ensure spatial index exists on both lines & polygon layers for speedup. If your lines or geometry features are too detailed you could try simplifying them to fewer points & if you have lots of features spanning large geographies I would pre-partition shape files to allow processing in parallel.