判断一个点是否在多边形内

4
我正在尝试检测一个给定的点(x,y)是否在一个n*2数组的多边形内。但是似乎一些位于多边形边界上的点会返回不包括在内的结果。
def point_inside_polygon(x,y,poly):

    n = len(poly)
    inside =False

    p1x,p1y = poly[0]
    for i in range(n+1):
        p2x,p2y = poly[i % n]
        if y > min(p1y,p2y):
            if y <= max(p1y,p2y):
                if x <= max(p1x,p2x):
                    if p1y != p2y:
                        xinters = (y-p1y)*(p2x-p1x)/float((p2y-p1y))+p1x
                    if p1x == p2x or x <= xinters:
                        inside = not inside
        p1x,p1y = p2x,p2y

    return inside

1
这些坐标是整数还是浮点数?使用Python 2还是Python 3? - Jean-François Fabre
浮点数和Python 2。我还将除法更改为浮点数,但在某些边界点上会得到错误的结果。 - Rabih Assaf
如果是浮点数,比较 p1x == p2x 是不好的:它可能相等也可能不相等,存在精度损失问题。 - Jean-François Fabre
这个回答解决了你的问题吗?检查一个点是否在多边形内部 - Georgy
这个回答解决了你的问题吗?如何确定一个二维点是否在多边形内? - Fabrizio
3个回答

7

这里有一种简单的方式,包含多个选项

from shapely.geometry import Point, Polygon

# Point objects(Geo-coordinates)
p1 = Point(24.952242, 60.1696017)
p2 = Point(24.976567, 60.1612500)

# Polygon
coords = [(24.950899, 60.169158), (24.953492, 60.169158), (24.953510, 60.170104), (24.950958, 60.169990)]
poly = Polygon(coords)

方法一:

在函数中使用:

语法:point.within(polygon)

# Check if p1 is within the polygon using the within function
print(p1.within(poly))
# True

# Check if p2 is within the polygon
print(p2.within(poly))
# False

方法二:

使用 contains 函数:

语法:polygon.contains(point)

# Check if polygon contains p1 
print(poly.contains(p1))
# True

# Check if polygon contains p2
print(poly.contains(p2))
# False

检查点是否在多边形边界上:

使用touches函数:

语法:polygon.touches(point)

poly1 = Polygon([(0, 0), (1, 0), (1, 1)])
point1 = Point(0, 0)

poly1.touches(point1)
# True

如果你想加速这个过程,请使用下列方法:
import shapely.speedups
shapely.speedups.enable()

利用Geopandas


参考资料:

  1. https://automating-gis-processes.github.io/CSC18/lessons/L4/point-in-polygon.html#point-in-polygon-using-geopandas

  2. https://shapely.readthedocs.io/en/latest/manual.html

  3. https://streamhacker.com/2010/03/23/python-point-in-polygon-shapely/


2
您可以使用来自matplotlib.pathcontains_point函数,使用小的负半径和正半径(小技巧)。像这样:

import matplotlib.path as mplPath
import numpy as np

crd = np.array([[0,0], [0,1], [1,1], [1,0]])# poly
bbPath = mplPath.Path(crd)
pnts = [[0.0, 0.0],[1,1],[0.0,0.5],[0.5,0.0]] # points on edges
r = 0.001 # accuracy
isIn = [ bbPath.contains_point(pnt,radius=r) or bbPath.contains_point(pnt,radius=-r) for pnt in pnts]

结果如下:

这是结果

[True, True, True, True]

默认情况下(或r=0),边界上的所有点都不包括在内,结果为

[False, False, False, False]

2
这是包含边缘的正确代码:

Here is the correct code that includes edges:

def point_inside_polygon(x, y, poly, include_edges=True):
    '''
    Test if point (x,y) is inside polygon poly.

    poly is N-vertices polygon defined as 
    [(x1,y1),...,(xN,yN)] or [(x1,y1),...,(xN,yN),(x1,y1)]
    (function works fine in both cases)

    Geometrical idea: point is inside polygon if horisontal beam
    to the right from point crosses polygon even number of times. 
    Works fine for non-convex polygons.
    '''
    n = len(poly)
    inside = False

    p1x, p1y = poly[0]
    for i in range(1, n + 1):
        p2x, p2y = poly[i % n]
        if p1y == p2y:
            if y == p1y:
                if min(p1x, p2x) <= x <= max(p1x, p2x):
                    # point is on horisontal edge
                    inside = include_edges
                    break
                elif x < min(p1x, p2x):  # point is to the left from current edge
                    inside = not inside
        else:  # p1y!= p2y
            if min(p1y, p2y) <= y <= max(p1y, p2y):
                xinters = (y - p1y) * (p2x - p1x) / float(p2y - p1y) + p1x

                if x == xinters:  # point is right on the edge
                    inside = include_edges
                    break

                if x < xinters:  # point is to the left from current edge
                    inside = not inside

        p1x, p1y = p2x, p2y

    return inside

更新:修复了一个错误


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接