簡體   English   中英

有沒有辦法在 python 中使用 shapely 和 geopandas 找到 3 條或更多線相互接觸的點坐標列表?

[英]Is there way to find a list of point coordinates where 3 or more lines touch eachother using shapely and geopandas in python?

我有一個 Geopandas DataFrame ,每一行都包含一個勻稱的 LineString。 我需要找到三個或更多 LineStrings 相互接觸的點列表。 例如,在圖中Input image ,我需要三條彩色線相交點的坐標。

在我找到三條線相交的所有點后,我需要將三條線合並成兩條線:1)粉色+紫色2)綠色+紫色。 重疊應該沒問題。

任何幫助將不勝感激。 謝謝!

  • 使用了一些國家作為LINESTRINGS的來源
  • 有了這個數據集,只有兩個點有三個選定的國家相交(根據可視化)
  • 關鍵概念是內部連接點到點,然后找到重復次數超過要求的點
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM