[英]Is there way to find a list of point coordinates where 3 or more lines touch eachother using shapely and geopandas in python?
I have a Geopandas DataFrame with each row containing a shapely LineString.我有一个 Geopandas DataFrame ,每一行都包含一个匀称的 LineString。 I need to find a list of points where three or more LineStrings touch each other.我需要找到三个或更多 LineStrings 相互接触的点列表。 For example, in figure Input image , I need the coordinates of the point where the three colored lines meet.例如,在图中Input image ,我需要三条彩色线相交点的坐标。
After I find all the points where three lines intersect, I need to merge the three lines to form two lines: 1) Pink + Purple 2) Green + Purple.在我找到三条线相交的所有点后,我需要将三条线合并成两条线:1)粉色+紫色2)绿色+紫色。 The overlap should be fine.重叠应该没问题。
Any help would be highly appreciated.任何帮助将不胜感激。 Thanks!谢谢!
import geopandas as gpd
import shapely.geometry
import pandas as pd
import plotly.express as px
# some polygons
# fmt: off
gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).loc[lambda d: d["iso_a3"].isin(["BEL", "LUX", "NLD", "DEU", "AUT","POL"]), ["geometry", "iso_a3"]].reset_index(drop=True)
# fmt: on
# change to linestrings as per question
gdf["geometry"] = gdf["geometry"].apply(
lambda p: shapely.geometry.LineString(p.exterior.coords)
)
# generate series of points from linestrings
points = (
gdf["geometry"].apply(lambda l: l.coords).rename("point").explode().reset_index()
)
# 1. join all points to all points
# 2. exclude point to self
points = (
points.merge(points, on=["point"])
.drop_duplicates()
.loc[lambda d: d["index_x"] != d["index_y"]]
)
# now find points that appera N times
n_times = (
points.groupby(["point"])
.size()
.loc[lambda s: s >= 3]
.reset_index()["point"]
.apply(pd.Series)
)
# visualize to test...
px.scatter_mapbox(n_times, lon=0, lat=1, mapbox_style="carto-positron",).update_traces(
marker_size=20
).update_layout(mapbox={"layers": [{"source": gdf.__geo_interface__, "type": "line"}]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.