从 Shapely 中的平面坐标列表创建矩形列表 [英] Create a list of rectangles from a flat list of coordinates in Shapely

查看:56
本文介绍了从 Shapely 中的平面坐标列表创建矩形列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This doesn't answer my question.

I have a list of coordinates where every 5 consecutive coordinates define the coordinates of a rectangle, e.g.,

mylist=[(0, 7),(4, 7),(4, 12),(0, 12),(0, 7),(7, 1),(10, 1),(10, 8),(7, 8),(7, 1),(4, 8),(10, 8),(10, 12), (4, 12),(4, 8),(0, 0),(7, 0),(7, 7),(0, 7),(0, 0)]

I want to create four rectangles in Shapely with these coordinates shown in the example. The four rectangles should also be uniquely identifiable. Also, the list size can be variable as there can be more or less than the current number of coordinates.

EDIT:

At this point, I have 4 lists:

[[(0, 7), (4, 7), (4, 12), (0, 12), (0, 7)],
 [(7, 1), (10, 1), (10, 8), (7, 8), (7, 1)],
 [(4, 8), (10, 8), (10, 12), (4, 12), (4, 8)],
 [(0, 0), (7, 0), (7, 7), (0, 7), (0, 0)]] 

Now my question is how to pass these 4 sets of coordinates to shapely so I can later draw them in a figure and identify them individually? I am new to shapely.

解决方案

You could store your Shapely objects in a dictionary and use them whenever your want afterwards, as e.g. below:

from shapely.geometry import Polygon

your_list = [[(0, 7), (4, 7), (4, 12), (0, 12), (0, 7)],
            [(7, 1), (10, 1), (10, 8), (7, 8), (7, 1)],
            [(4, 8), (10, 8), (10, 12), (4, 12), (4, 8)],
            [(0, 0), (7, 0), (7, 7), (0, 7), (0, 0)]] 

your_dict = {}

for i,sublist in enumerate(your_list):
    
    your_dict[i] = Polygon(sublist)

Then plot them you could use your_dict in this way:

plt.figure()

for key,your_polygon in your_dict.items():
    
    plt.plot(*your_polygon.exterior.xy)

which would give:

这篇关于从 Shapely 中的平面坐标列表创建矩形列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆