如何在旧对象下创建新对象?

问题描述 投票:0回答:1

输入数据格式[(x1,y1,z1,x2,y2,z2),(...),...]:

https://pastebin.com/EiZZEeQR

任务是找到一种方法,根据创建函数的调用顺序在旧对象下创建新对象。在本例中,我们首先创建一个 obj_1,然后创建一个 obj_2,它将隐藏在 obj_1 之下。然后创建了 obj_3,因此该对象隐藏在第一个和第二个对象之下。

所需的输出数据 - 用于绘制绘图的所有线的坐标

@Timeless 编写的部分程序正在运行。但我的数据集更复杂。就我而言,需要改进的解决方案。

obj_1 = [[0.09, 0.98, 0.13, 0.98, 0.17, 0.97, 0.2, 0.94, 0.2, 0.91, 0.17, 0.88, 0.13, 0.87, 0.09, 0.87, 0.05, 0.88, 0.02, 0.91, 0.02, 0.94, 0.05, 0.97, 0.09, 0.98]]
obj_2 = [[0.1, 0.77, 0.2, 1.07]]

def create_lines(coords):
    return unary_union(
        [
            (
                Polygon(ls) if (
                    ls := LineString(batched(coo, 2))
                ).is_ring else ls
            ) for coo in coords
        ]
    )

lines1, lines2 = map(create_lines, [obj_1, obj_2])

fig, ax = plt.subplots(figsize=(4, 7))

plot_line(lines2.difference(lines1), color="w", add_points=False, ax=ax)
plot_line(lines1.exterior, color="w", lw=2, add_points=False, ax=ax)

ax.set(facecolor="#242c34", xticks=[], yticks=[])

plt.show()
python matplotlib shapely
1个回答
0
投票

下面是一个相对可扩展的解决方案,它创建实际切割的形状对象,而不是仅仅将它们显示在另一个对象下面。

from typing import Iterable

import shapely
from matplotlib import pyplot as plt
from shapely import Polygon, LineString
from shapely.plotting import plot_line, plot_polygon
from shapely.strtree import STRtree

dresva = [0.09, 0.97, 0.15, 0.88, 0.03, 0.88, 0.09, 0.97]

graviy = [0.05, 0.98, 0.07, 0.98, 0.09, 0.97, 0.1, 0.95, 0.1, 0.93, 0.09, 0.91, 0.07, 0.9, 0.05, 0.9, 0.03, 0.91, 0.02,
          0.93, 0.02, 0.95, 0.03, 0.97, 0.05, 0.98]

glina = [0.02, 0.9, 0.2, 0.98]


def grouped(iterable, n=2):
    """s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ..."""
    return zip(*[iter(iterable)] * n)


circle = Polygon(grouped(graviy))
triangle = Polygon(grouped(dresva))
line = LineString(grouped(glina))


def overlay(geometries: Iterable[BaseGeometry]):
    """First features will be underneath, last feature on top"""
    result = []
    s = set()
    tree = STRtree(geometries)
    for idx, current_feature in enumerate(geometries):
        s.add(idx)
        overlapping_features = tree.geometries.take(list(set(tree.query(current_feature)) - s))
        cut = current_feature.difference(unary_union(overlapping_features))
        result.append(cut)
    return result



l = [line, circle, triangle]

l_overlayed = overlay(l)

fig, ax = plt.subplots(figsize=(4, 7))
line2, circle2, triangle2 = l_overlayed

plot_polygon(circle2, color="w", add_points=False, ax=ax)
plot_polygon(triangle2, color="w", add_points=False, ax=ax)
plot_line(line2, color="w", lw=2, add_points=False, ax=ax)

ax.set(facecolor="#242c34", xticks=[], yticks=[])
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.