R:计算两点层之间的最短距离 [英] R: Calculating the shortest distance between two point layers

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

问题描述

我需要计算两个点矩阵之间的最短距离.我是R的新手,也不知道如何执行此操作.这是我用来调用数据并将其转换为点的代码

I need to calculate the shortest distance between two point matrices. I am new to R and have no clue how to do this. This is the code that I used to call in the data and convert them to points

library(dismo)  
laurus <- gbif("Laurus", "nobilis")
locs <- subset(laurus, select = c("country", "lat", "lon"))
#uk observations
locs.uk <-subset(locs, locs$country=="United Kingdom")
#ireland observations
locs.ire <- subset(locs, locs$country=="Ireland")

uk_coord <-SpatialPoints(locs.uk[,c("lon","lat")])
ire_coord <-SpatialPoints(locs.ire[,c("lon","lat")])
crs.geo<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")  # geographical, datum WGS84
proj4string(uk_coord) <-crs.geo #define projection
proj4string(ire_coord) <-crs.geo #define projection

我需要计算从爱尔兰的点到英国的点的最短距离(欧几里得).换句话说,我需要计算从爱尔兰的每个点到英国的点层中其壁橱点的距离. 有人可以告诉我执行此操作需要使用什么功能或程序包.我查看了 gdistance ,但找不到找到最短距离的函数.

I need to calculate the shortest distance (Euclidean) from points in Ireland to points in UK. In other words I need to calculate the distance from each point in Ireland to its closet point in the UK points layer. Can some one tell me what function or package I need to use in order to do this. I looked at gdistance and could not find a function that calculate the shortest distance.

推荐答案

您可以使用FNN软件包,该软件包使用空间树来提高搜索效率.它适用于欧几里得几何,因此您应该将点转换为平面坐标系.我将使用rgdal包将其转换为英国网格参考(在爱尔兰进行一些扩展以在爱尔兰使用它,但是您的原始数据是纽约,因此您应该使用纽约平面坐标系):

You can use the FNN package which uses spatial trees to make the search efficient. It works with euclidean geometry, so you should transform your points to a planar coordinate system. I'll use rgdal package to convert to UK grid reference (stretching it a bit to use it over ireland here, but your original data was New York and you should use a New York planar coord system for that):

> require(rgdal)
> uk_coord = spTransform(uk_coord, CRS("+init=epsg:27700"))
> ire_coord = spTransform(ire_coord, CRS("+init=epsg:27700"))

现在我们可以使用FNN:

Now we can use FNN:

> require(FNN)
> g = get.knnx(coordinates(uk_coord), coordinates(ire_coord),k=1)
> str(g)
List of 2
 $ nn.index: int [1:69, 1] 202 488 202 488 253 253 488 253 253 253 ...
 $ nn.dist : num [1:69, 1] 232352 325375 87325 251770 203863 ...

g是最接近69个爱尔兰点的uk点的索引和距离的列表.距离以米为单位,因为坐标系以米为单位.

g is a list of indexes and distances of the uk points that are nearest to the 69 irish points. The distances are in metres because the coordinate system is in metres.

您可以通过绘制点然后将爱尔兰点1与英国点202,爱尔兰2与英国488,爱尔兰3与英国202等进行绘制来说明这一点.在代码中:

You can illustrate this by plotting the points then joining irish point 1 to uk point 202, irish 2 to uk 488, irish 3 to uk 202 etc. In code:

> plot(uk_coord, col=2, xlim=c(-1e5,6e5))
> plot(ire_coord, add=TRUE)
> segments(coordinates(ire_coord)[,1], coordinates(ire_coord)[,2], coordinates(uk_coord[g$nn.index[,1]])[,1], coordinates(uk_coord[g$nn.index[,1]])[,2])

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

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