python - 如何检查多边形是否包含点?
问题描述
假设我有
from shapely.geometry import Polygon, Point
polygon = Polygon(((0, 0), (0, 1), (1, 1), (0.5, 0.5), (1, 0)))
point = Point(0, 1)
如何检查是否point
在里面polygon
?
解决方案
有多种为 shape 实现的关系方法。从我目前所见,他们完全遵循英语:
shape_a.contains(shape_b)
shape_a.intersects(shape_b)
shape_a.overlaps(shape_b)
shape_a.touches(shape_b)
代码
为了检查行为,我编写了以下代码:
from shapely.geometry import Polygon, Point
polygon = Polygon(((0, 0), (0, 1), (1, 1), (0.5, 0.5), (1, 0)))
point_on_corner = Point(0, 1)
point_on_edge = Point(0, 0.5)
point_inside = Point(0.25, 0.5)
point_outside_inside_hull = Point(0.75, 0.5)
point_outside_outside_hull = Point(1.1, 0.5)
points = [('point_inside', point_inside),
('point_on_corner', point_on_corner),
('point_on_edge', point_on_edge),
('point_outside_inside_hull', point_outside_inside_hull),
('point_outside_outside_hull', point_outside_outside_hull)]
max_len = max([len(name) for name, _ in points])
formatter = '{:<' + str(max_len) + '}: {}'
print('## contains')
for point_name, point in points:
print(formatter.format(point_name, polygon.contains(point)))
print('## intersection')
for point_name, point in points:
print(formatter.format(point_name, polygon.intersection(point)))
print('## Reverse order intersection')
for point_name, point in points:
print(formatter.format(point_name, point.intersection(polygon)))
# Check with the 'geometry.is_empty' attribute (not a function call)
print('## touches')
for point_name, point in points:
print(formatter.format(point_name, point.touches(polygon)))
print('## intersects')
for point_name, point in points:
print(formatter.format(point_name, point.intersects(polygon)))
print('## overlaps')
for point_name, point in points:
print(formatter.format(point_name, point.overlaps(polygon)))
这导致:
## contains
point_inside : True
point_on_corner : False
point_on_edge : False
point_outside_inside_hull : False
point_outside_outside_hull: False
## intersection
point_inside : POINT (0.25 0.5)
point_on_corner : POINT (0 1)
point_on_edge : POINT (0 0.5)
point_outside_inside_hull : GEOMETRYCOLLECTION EMPTY
point_outside_outside_hull: GEOMETRYCOLLECTION EMPTY
## Reverse order intersection
point_inside : POINT (0.25 0.5)
point_on_corner : POINT (0 1)
point_on_edge : POINT (0 0.5)
point_outside_inside_hull : GEOMETRYCOLLECTION EMPTY
point_outside_outside_hull: GEOMETRYCOLLECTION EMPTY
## touches
point_inside : False
point_on_corner : True
point_on_edge : True
point_outside_inside_hull : False
point_outside_outside_hull: False
## intersects
point_inside : True
point_on_corner : True
point_on_edge : True
point_outside_inside_hull : False
point_outside_outside_hull: False
## overlaps
point_inside : False
point_on_corner : False
point_on_edge : False
point_outside_inside_hull : False
point_outside_outside_hull: False
推荐阅读
- mysql - 我可以在 Sequelize.js 中的单个查询中获取 ID 为 1 的类别和该类别的所有子类别吗?
- flutter - 无法在测试中打开/读取本地文件。引发 FileSystemException:无法打开文件、路径
- java - 使用 Spring ResponseEntity 转发
- java - 日历 API 调用开始产生“SSL 对等体关闭”
- swift - Swift:无法在 WKWebView 中加载本地图像
- amazon-web-services - 无服务器应用程序规范文档无效。发现的错误数:2. ID 为 [**] 的资源无效。属性“角色”的类型无效
- vue.js - Vue:在组件中查看 v-model 的“值”
- python - 如何优化 PyTorch 中循环损失函数的内存使用?
- java - Android资源链接失败错误:写入数据无效。(13)
- selenium - 如何在selenium xpath中遍历祖先并返回孩子?