猿问

如何检查多边形是否包含一个点?

假设我有


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?


POPMUISE
浏览 191回答 1
1回答

慕姐4208626

有多种针对 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, Pointpolygon = 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),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ('point_on_corner', point_on_corner),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ('point_on_edge', point_on_edge),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ('point_outside_inside_hull', point_outside_inside_hull),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ('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:&nbsp; &nbsp; print(formatter.format(point_name, polygon.contains(point)))print('## intersection')for point_name, point in points:&nbsp; &nbsp; print(formatter.format(point_name, polygon.intersection(point)))print('## Reverse order intersection')for point_name, point in points:&nbsp; &nbsp; 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:&nbsp; &nbsp; print(formatter.format(point_name, point.touches(polygon)))print('## intersects')for point_name, point in points:&nbsp; &nbsp; print(formatter.format(point_name, point.intersects(polygon)))print('## overlaps')for point_name, point in points:&nbsp; &nbsp; print(formatter.format(point_name, point.overlaps(polygon)))这导致:## containspoint_inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Truepoint_on_corner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Falsepoint_on_edge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Falsepoint_outside_inside_hull : Falsepoint_outside_outside_hull: False## intersectionpoint_inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : POINT (0.25 0.5)point_on_corner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: POINT (0 1)point_on_edge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: POINT (0 0.5)point_outside_inside_hull : GEOMETRYCOLLECTION EMPTYpoint_outside_outside_hull: GEOMETRYCOLLECTION EMPTY## Reverse order intersectionpoint_inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : POINT (0.25 0.5)point_on_corner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: POINT (0 1)point_on_edge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: POINT (0 0.5)point_outside_inside_hull : GEOMETRYCOLLECTION EMPTYpoint_outside_outside_hull: GEOMETRYCOLLECTION EMPTY## touchespoint_inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Falsepoint_on_corner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Truepoint_on_edge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Truepoint_outside_inside_hull : Falsepoint_outside_outside_hull: False## intersectspoint_inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Truepoint_on_corner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Truepoint_on_edge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Truepoint_outside_inside_hull : Falsepoint_outside_outside_hull: False## overlapspoint_inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : Falsepoint_on_corner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Falsepoint_on_edge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Falsepoint_outside_inside_hull : Falsepoint_outside_outside_hull: False
随时随地看视频慕课网APP

相关分类

Python
我要回答