python - 如何绘制带孔的 Shapely 多边形?
问题描述
我想要一个函数plot(shapely_objects)
,其中shapely_objects
包含多边形、点和多多边形的列表。我有一些非常相似的东西,但它填补了多边形的洞。如何绘制带孔的 Shapely 多边形?
代码
from shapely.geometry import Point, shape
def plot(shapely_objects, figure_path='fig.png'):
from matplotlib import pyplot as plt
import geopandas as gpd
boundary = gpd.GeoSeries(shapely_objects)
boundary.plot(color=['red', 'green'])
plt.savefig(figure_path, dpi=300, bbox_inches="tight")
p = Point(12.12, 54.085)
name = ''
multi_poly = {'coordinates': (((12.11, 54.08), (12.11, 54.09),
(12.13, 54.09), (12.13, 54.08)),
((12.11, 54.08), (12.11, 54.09),
(12.13, 54.09), (12.13, 54.08))),
'type': 'Polygon'}
multi_poly = shape(multi_poly)
plot([p, multi_poly, p])
print('multi_poly.intersects(p) == {}'.format(multi_poly.intersects(p)))
print('multi_poly.touches(p) == {}'.format(multi_poly.touches(p)))
print('multi_poly.contains(p) == {}'.format(multi_poly.contains(p)))
print('p.within(multi_poly) == {}'.format(p.within(multi_poly)))
p, multi_poly = multi_poly, p
print('multi_poly.intersects(p) == {}'.format(multi_poly.intersects(p)))
print('multi_poly.touches(p) == {}'.format(multi_poly.touches(p)))
print('multi_poly.contains(p) == {}'.format(multi_poly.contains(p)))
print('p.within(multi_poly) == {}'.format(p.within(multi_poly)))
图片
绿点不在红色多边形内。洞缺失/填充。
解决方案
推荐阅读
- r - 如何在可变大小的 3 维数组上加快 R 循环操作?
- algorithm - 算法和确定 theta(b)
- python - 如何获得所需的尺寸作为 Tensorflow(Keras)输出?
- javascript - HTML 输入日期:不允许用户使用键盘输入未来日期
- node.js - 拒绝(TypeError):无法读取未定义的属性“setState”,未捕获(在承诺中)类型错误:无法读取未定义的属性“setState”
- c# - c#控制台应用程序按空格键而不在屏幕上显示
- ms-word - Word 加载项 (office-js) - createDocument 功能在 Word 在线中无法按预期工作
- c++ - 构造函数中需要什么,setter 函数中需要什么?
- javascript - 鼠标 - 平面上的 x 和 y 但为什么呢?
- python - 如何生成平均为 11.7 的随机正态分布(在 Python 中)