问题描述
形状多边形的形状可以很容易地通过使用
转换为点数组x,y = polygon.exterior.xy
但是,这仅返回实际点。
如何将形状多边形转换为数组,形状外部的每个像素都为0,形状内部的每个像素都为1?
因此,例如,一个宽度为100,高度为100的多边形应生成(100,100)数组。
我可以通过获取外部点,然后遍历每个像素并查看它是在形状的内部/上方还是外部来实现。但是我认为应该有一个更简单的方法?
解决方法
我不知道您的确切要求,但是最快能得到一般结果的应该是:
width = int(shape.bounds[2] - shape.bounds[0])
height = int(shape.bounds[3] - shape.bounds[1])
points = MultiPoint( [(x,y) for x in range(width) for y in range(height)] )
zeroes = points.difference( shape )
ones = points.intersection( shape )
,
这可以回答我的问题,但是我想知道是否有更快的方法。
data = []
width = int(shape.bounds[2] - shape.bounds[0])
height = int(shape.bounds[3] - shape.bounds[1])
for y in range(0,height):
row = []
for x in range(0,width):
val = 1 if shape.convex_hull.contains(Point(x,y)) else 0
row.append(val)
data.append(row)