简体   繁体   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?

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!谢谢!

  • have used a few countries for source of LINESTRINGS使用了一些国家作为LINESTRINGS的来源
  • with this data set, there are only two points where there are three selected countries that intersect (as per visualisation)有了这个数据集,只有两个点有三个选定的国家相交(根据可视化)
  • key concept is inner join points to points, then find points that have more than required number of repetitions关键概念是内部连接点到点,然后找到重复次数超过要求的点
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.

相关问题 如何使用 Python 和 Geopandas 从 Geojson 文件中查找多边形中的点 - How To Find a Point In Polygon from a Geojson file Using Python And Geopandas geopandas / 匀称的兼容性 - geopandas / Shapely Compatibility geopandas 中的 groupby 匀称类型 - groupby shapely type in geopandas 有没有办法同时用两只乌龟在python乌龟上做一个绘图游戏,它们不能碰线? - is there a way to make a drawing game on python turtle with two turtles at the same time, where they cant touch the lines? Python 3:将带有 CRS 坐标的 Geopandas 数据框转换为 Graph 以查找连接组件和其他图形属性? - Python 3: Geopandas dataframe with CRS coordinates into Graph to find connected components and other graph properties? geopandas 没有在多边形中找到点,即使它应该? - geopandas doesn't find point in polygon even though it should? 如何在Python3中以更快的方式查找列表的子数组? - How to find sub-arrays of a list in a more faster way in Python3? 将坐标转换为 python 中图像上的一个点 - convert coordinates into a point on an image in python 鉴于任何纬度,长。 坐标,在列表中找到最近坐标的最快方法是什么? - Given any lat., long. coordinates, what is the fastest way to find the closest coordinates on a list? 使用 geopandas 和 folium 在 python 中绘制多边形 - plotting polygons in python using geopandas and folium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM