Reputation: 1671
I have a GeoDataFrame with LineStrings and want to get all coordinates where the Geometries touch each other. I can find the ones who touch each other so far but how do I get the coordinates of the touching point? It should be as fast as possible because I have a lot of data.
data = gpd.read_file("data.geojson")
heads, tails = data.sindex.query_bulk(data.geometry, predicate="touches")
touching_dataframe = data.iloc[heads]
Upvotes: 0
Views: 144
Reputation: 11522
Use the intersection method provided by shapely library which is a dependency of geopandas.
# create a list to store the coordinates
coordinates = []
# iterate over the indexes of the touching geometries
for i in range(len(heads)):
# get the geometry of the current touching pair
head_geom = data.iloc[heads[i]]['geometry']
tail_geom = data.iloc[tails[i]]['geometry']
# get the intersection of the two geometries
intersection = head_geom.intersection(tail_geom)
# add the coordinates of the intersection to the list
coordinates.append(intersection.coords[:])
# create a new dataframe with the coordinates
touching_coordinates = pd.DataFrame(coordinates, columns=["longitude", "latitude"])
You will have a new DataFrame with all the coordinates where the geometries touch each other. Note that this process can be slow if you have a lot of data, so you should consider to use multiprocessing techniques or Dask to perform this operation in parallel.
Upvotes: 1