I am using Python 3.7 with Shapely and GeoPandas.
I have a big line of 181,000 points, and would like to find all the points where the line intersects itself. It does so a lot.
I don't need a new point at the precise intersection, just one of the existing points which is closest.
I have been writing code to loop through the points and find other points close by using.
for i,point in gdf.iterrows():
gdf[gdf.geometry.intersects(point.buffer(10) == True].index.tolist()
Where gdf is a geopandas GeoDataFrame where each row is a point from the line.
(eg it looks like this:)
geometry
0 POINT (-47.91000 -15.78000)
1 POINT (-47.92000 -15.78000)
But surely there is a way to do this using existing functions?
My way is very slow and records many duplicates at each intersection, so will require more code to reduce each intersection to one point.
Best Answer
update 2021:
a more elegant way using
unary_union
andlinemerge
. you can download the notebook here.There you go! now we have the points.
DEPRECATED answer from 2020:
Here's how I did it
unary_union
of the rest of the featureintersections
usingshapely
here's the example.
gdf
) of 6 lines like this GeoJSONgdf
. This is returning the geometry of the intersectionsGeoDataFrame
out of thepoints
the
intersections
data frame hasPoint
andMultiPoint
geometries. But there's a problem here... the points are intersecting. here's how to delete the overlapping pointsthe result is the same, but the intersection points won't overlap each other. here's the plot, and there are 6 row of dataframe, I checked.
edit: note, using `unary_union` means that if we have a large dataset, this may be RAM consuming.