用形状指向多边形吗? [英] Point in polygon using shapely?

查看:59
本文介绍了用形状指向多边形吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下脚本,我相信应该为多边形中的点返回TRUE,但返回FALSE.

I am running the following script which I believe should be returning TRUE for the point being in the polygon but it is returning FALSE.

from shapely import geometry

polygon = [(-1571236.8349707182, 8989180.222117377), (1599362.9654156454, 8924317.946336618), (-1653179.0745812152, 8922145.163675062), (-1626237.6614402141, 8986445.107619021)]

Point_X = -1627875.474
Point_Y = 8955472.968

line = geometry.LineString(polygon)
point = geometry.Point(Point_X, Point_Y)

print(line.contains(point))

当我在Matlab中绘制多边形和点时,得到以下形状

When I plot the polygon and point in Matlab I get the following shape

from matplotlib import pylab as plt
poly = [[-1571236.8349707182, 8989180.222117377],
    [1599362.9654156454, 8924317.946336618],
    [-1653179.0745812152, 8922145.163675062],
    [-1626237.6614402141, 8986445.107619021]]

x = [point[0] for point in poly]
y = [point[1] for point in poly]

p1 = [-1627875.474, 8955472.968]
p2 = [-1627875.474, 8955472.968]
plt.plot(x,y,p1[0],p1[1],'*r',p2[0],p2[1],'*b')
plt.show()

您知道为什么整形脚本返回FALSE吗?

Any idea why the shapely script is returning FALSE?

推荐答案

您要测试的是您的观点是否在对象 LineString 上.

What you are testing is whether your point is on the object LineString.

如果要测试点在多边形中,则必须使用类 Polygon

If you want to test that the point is in the polygon you must use the contains methods of class Polygon

from shapely import geometry

polygon = [(-1571236.8349707182, 8989180.222117377), (1599362.9654156454, 8924317.946336618), (-1653179.0745812152, 8922145.163675062), (-1626237.6614402141, 8986445.107619021)]

Point_X = -1627875.474
Point_Y = 8955472.968

line = geometry.LineString(polygon)
point = geometry.Point(Point_X, Point_Y)
polygon = geometry.Polygon(line)

print(polygon.contains(point))

输出

True

请参见 https://shapely.readthedocs.io/en/latest/manual.html

这篇关于用形状指向多边形吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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