【问题标题】:Is there way to find a list of point coordinates where 3 or more lines touch eachother using shapely and geopandas in python?有没有办法在python中使用shapely和geopandas找到3条或更多线相互接触的点坐标列表?
【发布时间】:2022-01-04 08:47:47
【问题描述】:

我有一个 Geopandas DataFrame,每一行都包含一个匀称的 LineString。我需要找到三个或更多 LineStrings 相互接触的点列表。比如图Input image中,我需要三色线相交点的坐标。

找到三条线相交的所有点后,我需要将三条线合并形成两条线:1)粉色+紫色2)绿色+紫色。重叠应该没问题。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: python-3.x geometry gis geopandas shapely


    【解决方案1】:
    • 已使用几个国家/地区作为 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"}]})
    

    【讨论】:

      猜你喜欢
      • 2020-10-30
      • 2022-01-22
      • 2022-01-22
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      相关资源
      最近更新 更多