计算多边形和R中的点之间的距离 [英] Calculating the distance between polygon and point in R

查看:91
本文介绍了计算多边形和R中的点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(不一定是凸的)多边形,没有交集,并且在该多边形之外有一个点.我想知道如何在二维空间中最有效地计算欧几里得距离. R中有标准方法吗?

I have a, not necessarily convex, polygon without intersections and a point outside this polygon. I'm wondering how calculate the Euclidian distance most efficiently in a 2-dimensional space. Is there a standard method in R?

我的第一个想法是计算多边形所有线的最小距离(无限扩展,因此它们是线,而不是线段),然后使用线段的起点计算从点到每条线的距离和毕达哥拉斯.

My first idea was to calculate the minimum distance of all the lines of the polygon (extended infinitely so they are line, not line pieces) and then calculate the distance from the point to each individual line using the start of the line piece and Pythagoras.

您知道实现高效算法的软件包吗?

Do you know about a package that implements an efficient algorithm?

推荐答案

您可以使用 rgeos包gDistance方法.这将需要您准备几何形状,从拥有的数据中创建spgeom对象(我假设它是data.frame或类似的东西). rgeos文档非常详细(请参见CRAN页面的软件包的PDF手册),这是gDistance文档中的一个相关示例:

You could use the rgeos package and the gDistance method. This will require you to prepare your geometries, creating spgeom objects from the data you have (I assume it is a data.frame or something similar). The rgeos documentation is very detailed (see the PDF manual of the package from the CRAN page), this is one relevant example from the gDistance documentation:

pt1 = readWKT("POINT(0.5 0.5)")
pt2 = readWKT("POINT(2 2)")
p1 = readWKT("POLYGON((0 0,1 0,1 1,0 1,0 0))")
p2 = readWKT("POLYGON((2 0,3 1,4 0,2 0))")
gDistance(pt1,pt2)
gDistance(p1,pt1)
gDistance(p1,pt2)
gDistance(p1,p2)

readWKT也包含在rgeos中.

readWKT is included in rgeos as well.

Rgeos基于GEOS库,GEOS库是几何计算中的事实上的标准之一.如果您不想重新发明轮子,这是一个不错的选择.

Rgeos is based on the GEOS library, one of the de facto standards in geometric computing. If you don't feel like reinventing the wheel, this is a good way to go.

这篇关于计算多边形和R中的点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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