我有一个最小的可重现的例子.

输入数据:

coordinates = [(50.0, 50.0, 100.0, 50.0), (70.0, 50.0, 75.0, 40.0, 80.0, 50.0)]

This is what these lines form in the drawing: drawing

任务是找到一个循环,即一个封闭的区域,并在终端中显示它们的边界.

在这种情况下,结果应该如下(它表示为[(line1),(line2)]):

[(70.0, 50.0, 75.0, 40.0), (75.0, 40.0, 80.0, 50.0), (80.0, 50.0, 70.0, 50.0)]

我try 了networkx库中的Cycle_Basic方法.

但在这种方法中,循环顶点必须接触其他循环的顶点.但在这里顶点可以接触到另一个循环的任何地方

推荐答案

IIUC, you want the induced / chordless_cycles. If so, here is a primal approach with :

points = MultiPoint(list(batched(chain.from_iterable(coordinates), 2)))

lines = [
    line
    for coo in coordinates
    for pop in pairwise(batched(coo, 2))
    for gc in [split(LineString(pop), points)]
    for line in gc.geoms
]

G = gdf_to_nx(
    gpd.GeoDataFrame(geometry=lines), multigraph=False, approach="primal"
)

cycles = [
    [
        tuple(chain.from_iterable(pair))
        for pair in pairwise(cyc)
    ] for cyc in nx.chordless_cycles(G)
    for cyc in [cyc + [cyc[0]]]
]

enter image description here

输出(cycles,clockwise):

[
    [ # top/green cycle
        (70.0, 50.0, 80.0, 50.0),
        (80.0, 50.0, 77.5, 45.0),
        (77.5, 45.0, 72.5, 45.0),
        (72.5, 45.0, 70.0, 50.0),
    ],
    [ # bottom/red cycle
        (72.5, 45.0, 77.5, 45.0),
        (77.5, 45.0, 75.0, 40.0),
        (75.0, 40.0, 72.5, 45.0),
    ],
]

使用的输入(coordinates):

from itertools import chain, batched, pairwise
import geopandas as gpd
from momepy import gdf_to_nx
import networkx as nx
from shapely import LineString, MultiPoint
from shapely.ops import split

coordinates = [
    (50.0, 50.0, 100.0, 50.0),
    (70.0, 50.0, 75.0, 40.0, 80.0, 50.0),
    (72.5, 45.0, 77.5, 45.0)
]

Full code
Old answer

Python相关问答推荐

使用mySQL的SQlalchemy过滤重叠时间段

在Python中处理大量CSV文件中的数据

为什么符号没有按顺序添加?

处理带有间隙(空)的duckDB上的重复副本并有效填充它们

无法在Docker内部运行Python的Matlab SDK模块,但本地没有问题

为什么\b在这个正则表达式中不解释为反斜杠

如何更改groupby作用域以找到满足掩码条件的第一个值?

如何在Pyplot表中舍入值

在代码执行后关闭ChromeDriver窗口

使用Openpyxl从Excel中的折线图更改图表样式

30个非DATETIME天内的累计金额

我对这个简单的异步者的例子有什么错误的理解吗?

简单 torch 模型测试:ModuleNotFoundError:没有名为';Ultralytics.yolo';

利用SCIPY沿第一轴对数组进行内插

上传文件并使用Panda打开时的Flask 问题

了解如何让库认识到我具有所需的依赖项

将参数从另一个python脚本中传递给main(argv

如何将ManyToManyfield用于Self类

根据两个lambda条件筛选组并根据条件创建新列的最佳方式是什么?

是否从Python调用SHGetKnownFolderPath?