Python的inpolygon-matplotlib.path.Path contains_points()方法的示例? [英] inpolygon for Python - Examples of matplotlib.path.Path contains_points() method?

查看:712
本文介绍了Python的inpolygon-matplotlib.path.Path contains_points()方法的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找MATLAB的inpolygon()的python替代品,并且遇到过contains_points是一个不错的选择.

I have been searching for a python alternative to MATLAB's inpolygon() and I have come across contains_points as a good option.

但是,文档有些空缺,没有表明contains_points期望的数据类型:

However, the docs are a little bare with no indication of what type of data contains_points expects:

contains_points(points,transform = None,radius = 0.0)

contains_points(points, transform=None, radius=0.0)

如果路径包含相应的点,则返回一个布尔数组,该数组为True.

Returns a bool array which is True if the path contains the corresponding point.

如果transform不为None,则将在执行测试之前对路径进行转换.

If transform is not None, the path will be transformed before performing the test.

半径可使路径变大或变小.

radius allows the path to be made slightly larger or smaller.

我将多边形存储为n * 2 numpy数组(其中n很大〜500).据我所知,我需要对此数据调用Path()方法,该方法似乎可以正常工作:

I have the polygon stored as an n*2 numpy array (where n is quite large ~ 500). As far as I can see I need to call the Path() method on this data which seems to work Ok:

poly_path = Path(poly_points)

目前,我还将要测试的点存储为另一个n * 2 numpy数组(catalog_points).

At the moment I also have the points I wish to test stored as another n*2 numpy array (catalog_points).

也许我的问题就在这里?就像我跑步时一样:

Perhaps my problem lies here? As when I run:

in_poly = poly_path.contains_points(catalog_points)

无论我使用的是哪一组点,我都会为每个值返回一个包含'False'的ndarray(我已经在多边形内的点数组上对此进行了测试).

I get back an ndarray containing 'False' for every value no matter the set of points I use (I have tested this on arrays of points well within the polygon).

推荐答案

通常在这些情况下,我会发现源头有启发性...

Often in these situations, I find the source to be illuminating...

我们可以看到 path.contains_point 的来源至少包含2个元素的容器. contains_points的源代码很难找到,因为它调用了C函数 Py_points_in_path .似乎此函数接受一个可迭代的元素,该元素产生的长度为2:

We can see the source for path.contains_point accepts a container that has at least 2 elements. The source for contains_points is a bit harder to figure out since it calls through to a C function Py_points_in_path. It seems that this function accepts a iterable that yields elements that have a length 2:

>>> from matplotlib import path
>>> p = path.Path([(0,0), (0, 1), (1, 1), (1, 0)])  # square with legs length 1 and bottom left corner at the origin
>>> p.contains_points([(.5, .5)])
array([ True], dtype=bool)

当然,我们也可以使用点的numpy数组:

Of course, we could use a numpy array of points as well:

>>> points = np.array([.5, .5]).reshape(1, 2)
>>> points
array([[ 0.5,  0.5]])
>>> p.contains_points(points)
array([ True], dtype=bool)

为了检查我们并不一定总是得到True:

And just to check that we aren't always just getting True:

>>> points = np.array([.5, .5, 1, 1.5]).reshape(2, 2)
>>> points
array([[ 0.5,  0.5],
       [ 1. ,  1.5]])
>>> p.contains_points(points)
array([ True, False], dtype=bool)

这篇关于Python的inpolygon-matplotlib.path.Path contains_points()方法的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆