kp42
kp42

Reputation: 25

How do I loop through two geodataframes to find the number of intersections between a given line in one gdf and all lines in the other?

I have two geodataframes of line features. For each line in the first dataframe, I want to figure out how many lines from the second dataframe intersect it and append that number to a new column in the dataframe.

For instance, if I have two geodataframes like this:

from shapely.geometry import LineString
import geopandas as gpd

df = gpd.GeoDataFrame([['a',LineString([(1, 0.25), (2,1.25)])], ['b', LineString([(1.2, 1.0), (1.4, 1.5)])]], columns = ['name','geometry'])
df = gpd.GeoDataFrame(df, geometry = 'geometry')

df2 = gpd.GeoDataFrame([['c', LineString([(2.0, 0.5), (1.0, .75)])], ['d', LineString([(2, 0.75), (1.25, 0.8)])]], columns = ['name', 'geometry'])
df2 = gpd.GeoDataFrame(df2, geometry = 'geometry')

Two dataframes plotted on a chart (df = blue, df2 = red)

I would want to have an output that looks like:

desired output

after running the script.

I found this question from several years ago that was asking something similar. I like the idea of trying to cut down on processing time using the buffer. However, when I tried to implement it using my line data, the final results list was blank. Does anyone have any ideas for ways to iterate over these dataframes?

Upvotes: 0

Views: 601

Answers (2)

Brener Ramos
Brener Ramos

Reputation: 356

Another way of doing it with less code.

df['intersections'] = df['geometry'].apply(lambda line1: sum(line1.intersects(line2) for line2 in df2['geometry']))

Upvotes: 1

kp42
kp42

Reputation: 25

I think I figured out a solution with a nested for loop:

#empty list to add counts
results_list = []
#iterate through geometries of both dataframes
for otherlines in df["geometry"]:
    count = 0
    for lines in df2["geometry"]:
        if lines.intersects(otherlines):
            count += 1
    results_list.append(count)

df['intersections'] = results_list

Initially, I was only able to get this to work on the geodataframe I posted above and it wasn't working for my more complicated geodataframe. That was because it had null geometries which I was able to repair in ArcMap (although I'm sure there's a function to check and repair empty geometries in Python).

Upvotes: 0

Related Questions