utils.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:gridded 作者: NOAA-ORR-ERD 项目源码 文件源码
def points_in_polys(points, polys, polyy=None):
    """
    :param points: Numpy array of Nx2 points
    :param polys: Numpy array of N polygons of degree M represented
    by Mx2 points (NxMx2) for each point, see if respective poly
    contains it. Returns array of True/False
    """

    result = np.zeros((points.shape[0],), dtype=bool)
    if isinstance(points, np.ma.masked_array):
        points = points.data
    if isinstance(polys, np.ma.masked_array):
        polys = polys.data
    if polyy is not None and isinstance(polyy, np.ma.masked_array):
        polyy = polyy.data
    pointsx = points[:, 0]
    pointsy = points[:, 1]
    v1x = v1y = v2x = v2y = -1
    for i in range(0, polys.shape[1]):
        if polyy is not None:
            v1x = polys[:, i - 1]
            v1y = polyy[:, i - 1]
            v2x = polys[:, i]
            v2y = polyy[:, i]
        else:
            v1x = polys[:, i - 1, 0]
            v1y = polys[:, i - 1, 1]
            v2x = polys[:, i, 0]
            v2y = polys[:, i, 1]
        test1 = (v2y > pointsy) != (v1y > pointsy)
        test2 = np.zeros(points.shape[0], dtype=bool)
        m = np.where(test1 == 1)[0]
        test2[m] = pointsx[m] < \
            (v1x[m] - v2x[m]) * (pointsy[m] - v2y[m]) / \
            (v1y[m] - v2y[m]) + v2x[m]
        np.logical_and(test1, test2, test1)
        np.logical_xor(result, test1, result)
    return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号