gpt4 book ai didi

python - 堆叠多个形状良好的多边形以形成热图

转载 作者:行者123 更新时间:2023-12-01 03:43:59 26 4
gpt4 key购买 nike

我有一堆重叠的 Shapely 多边形。每个形状代表了对野外物体的一次特定观察。我想通过将多边形组合在一起来建立某种累积观察(热图?)。不仅仅是联合:我想以这样的方式将它们组合起来,以便我可以将其阈值组合在一起,并更好地估计对象的实际位置。什么是“光栅化”形状多边形的最佳方法?

最佳答案

也许可以通过一次添加一个多边形来继续,保留不相交形状的列表,并记住每个形状贡献了多少个多边形:

import copy
from itertools import groupby
from random import randint, seed
from shapely.geometry import Polygon, box
from shapely.ops import cascaded_union

polygons = [
Polygon([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]),
Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]),
Polygon([(1, 1), (2, 1), (2, 2), (1, 2), (1, 1)])
]

def check_shape(s):
return (s.geom_type in ['Polygon', 'MultiPolygon'] and not s.is_empty)

shapes = []
for p in polygons:

polygon = copy.deepcopy(p)
new_shapes = []
for shape_cnt, shape in shapes:

new_shapes.extend([
(shape_cnt, shape.difference(polygon)),
(shape_cnt+1, shape.intersection(polygon))
])

polygon = polygon.difference(shape)
new_shapes.append((1, polygon))

shapes = list(filter(lambda s: check_shape(s[1]), new_shapes))

for p in polygons:
print(p)

for cnt, g in groupby(sorted(shapes, key = lambda s: s[0]), key = lambda s: s[0]):
print(cnt, cascaded_union(list(map(lambda s: s[1], g))))

这会产生:

POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))
POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
POLYGON ((1 1, 2 1, 2 2, 1 2, 1 1))
1 MULTIPOLYGON (((2 1, 2 0, 1 0, 1 1, 2 1)), ((0 1, 0 2, 1 2, 1 1, 0 1)))
2 MULTIPOLYGON (((1 0, 0 0, 0 1, 1 1, 1 0)), ((1 2, 2 2, 2 1, 1 1, 1 2)))

关于python - 堆叠多个形状良好的多边形以形成热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39216767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com