shapely和matplotlib指向多边形不准确的地理位置 [英] shapely and matplotlib point-in-polygon not accurate with geolocation

查看:198
本文介绍了shapely和matplotlib指向多边形不准确的地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用matplotlib测试多边形中的点函数。



这是一个地图包含一个百慕大三角形多边形。



Google地图的多边形点功能清楚地显示 testingPoint testingPoint2 位于正确结果的多边形内部。



如果我测试<

 在[1]中:从matplotlib.path导入 matplotlib ,并且很体面,只有point2通过测试。路径

在[2]中:p = Path([[25.774252,-80.190262],[18.466465,-66.118292],[32.321384,-64.75737]])

In [3]:p1 = [27.254629577800088,-76.728515625]

在[4]中:p2 = [27.254629577800088,-74.928515625]

In [5]:p.contains_point p1)
出[5]:0

在[6]:p.contains_point(p2)
出[6]:1



身材显示相同的r作为matplotlib做的。

  In [1]:from shapely.geometry import Polygon,Point 

在[2]中:poly = Polygon(([25.774252,-80.190262],[18.466465,-66.118292],[32.321384,-64.75737]))

In [3]:p1 = Point(27.254629577800088 ,-76.728515625)

在[4]中:p2 =点(27.254629577800088,-74.928515625)

In [5]:poly.contains(p1)
Out [5]:False

在[6]中:poly.contains(p2)
Out [6]:True

这里究竟发生了什么? Google的算法比那两个更好吗?



谢谢 t flat!如果Google Maps的投影是您想要的答案,则需要将地理坐标投影到球形墨卡托来获得一组不同的X和Y坐标。 Pyproj 可以帮助你做到这一点,只要确保你在坐标轴之前(即:X, Y或经度,纬度)。

 从shapely.geometry导入pyproj 
导入Polygon,Point
从shapely.ops从functools导入变换
导入部分

project = partial(
pyproj.transform,
pyproj.Proj(init ='epsg:4326'),
pyproj.Proj('+ proj = merc + a = 6378137 + b = 6378137 + lat_ts = 0.0 + lon_0 = 0.0 + x_0 = 0.0 + y_0 = 0 + k = 1.0 + units = m + nadgrids = @ null + no_defs'))

poly =多边形(([ - 80.190262,25.774252],[-66.118292,18.466465],[-64.75737,32.321384]))
p1 = Point(-76.728515625 ,27.254629577800088)

#使用长/纬度坐标的旧答案
poly.contains(p1)#假
poly.distance(p1)#0.01085626429747994度

#转换为球形墨卡托投影或Google投影
poly_g =变换(项目,多边形)
p1_g = transform(project,p1)

poly_g.contains(p1_g)#True
poly_g.distance(p1_g)#0.0米

好像得到了正确答案。


I am testing the point-in-polygon function with matplotlib and shapely.

Here is a map contains a Bermuda triangle polygon.

Google maps's point-in-polygon functions clearly shows testingPoint and testingPoint2 are inside of the polygon which is a correct result.

if I test the two points in matplotlib and shapely, only point2 passes the test.

In [1]: from matplotlib.path import Path

In [2]: p = Path([[25.774252, -80.190262], [18.466465, -66.118292], [32.321384, -64.75737]]) 

In [3]: p1=[27.254629577800088, -76.728515625]

In [4]: p2=[27.254629577800088, -74.928515625]

In [5]: p.contains_point(p1)
Out[5]: 0

In [6]: p.contains_point(p2)
Out[6]: 1

shapely shows the same result as matplotlib does.

In [1]: from shapely.geometry import Polygon, Point

In [2]: poly = Polygon(([25.774252, -80.190262], [18.466465, -66.118292], [32.321384, -64.75737]))

In [3]: p1=Point(27.254629577800088, -76.728515625)

In [4]: p2=Point(27.254629577800088, -74.928515625)

In [5]: poly.contains(p1)
Out[5]: False

In [6]: poly.contains(p2)
Out[6]: True

What is actually going on here? Is Google's algorithm better than those two?

Thanks

解决方案

Remember: the world isn't flat! If Google Maps' projection is the answer you want, you need to project the geographic coordinates onto spherical Mercator to get a different set of X and Y coordinates. Pyproj can help with this, just make sure you reverse your coordinate axes before (i.e.: X, Y or longitude, latitude).

import pyproj
from shapely.geometry import Polygon, Point
from shapely.ops import transform
from functools import partial

project = partial(
    pyproj.transform,
    pyproj.Proj(init='epsg:4326'),
    pyproj.Proj('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'))

poly = Polygon(([-80.190262, 25.774252], [-66.118292, 18.466465], [-64.75737, 32.321384]))
p1 = Point(-76.728515625, 27.254629577800088)

# Old answer, using long/lat coordinates
poly.contains(p1)  # False
poly.distance(p1)  # 0.01085626429747994 degrees

# Translate to spherical Mercator or Google projection
poly_g = transform(project, poly)
p1_g = transform(project, p1)

poly_g.contains(p1_g)  # True
poly_g.distance(p1_g)  # 0.0 meters

Seems to get the correct answer.

这篇关于shapely和matplotlib指向多边形不准确的地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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