简体   繁体   English

从R中的空间点计算角度

[英]Calculating Angles from Spatial Points in R

I am looking at some dispersal data and would like to get distance between points and also the angle between those points. 我正在查看一些分散数据,希望获得点之间的距离以及这些点之间的角度。 So far, I have only been able to achieve the first part. 到目前为止,我只能实现第一部分。 Using the teal data from the adehabitatLT package I have done this: 使用adehabitatLT软件包中的深青色数据,我这样做:

require("adehabitatLT")
require("sp")
data("teal")
teal <- teal[1:10 ,]
capsd <- SpatialPointsDataFrame(coords = SpatialPoints(coords = 
    teal[, c("x","y")], proj4string = CRS("+proj=longlat +datum=WGS84 
    +ellps=WGS84 +towgs84=0,0,0")), data=teal)
capdistance <- as.data.frame(pointDistance(capsd))

The capdistance is a 10x10 dataframe displaying the distances between the first 10 points of the teal dataset. capdistance是一个10x10数据帧,显示了蓝绿色数据集的前10个点之间的距离。

Does anyone know how I would calculate the angle between these points to create a similar matrix to the capdistance data.frame? 有谁知道我将如何计算这些点之间的角度以创建与capdistance data.frame类似的矩阵? I have searched, but so far I have not found anything that would calculate the angle between two set locations. 我已经搜索过,但是到目前为止,我还没有发现任何可以计算两个设置位置之间的角度的东西。 Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT 编辑

So I have been looking around and it would seem that the bearing function from the geosphere package would be useful for this, but I am still (at least) a step away from working this all the way through: 因此,我一直在环顾四周,看来来自Geosphere软件包的方位函数对此很有用,但我仍然(至少)距离一路过关斩将:

require("geosphere")
capbearing1 <- bearing(capsd[1:10 ,], capsd[1 ,])
capbearing2 <- bearing(capsd[1:10 ,], capsd[2 ,])

I could repeat this ten times to achieve ten lists each one giving the angle of one of the ten points relative to all ten points (itself and the nine others); 我可以重复十次以得到十个列表,每个列表给出十个点之一相对于所有十个点(本身和另外九个点)的角度; however, I would really like this to operate smoothly to give all ten lists at once as a single matrix; 但是,我真的希望这样可以顺利运行,以单个矩阵的形式一次列出所有十个列表。 again, any help is very appreciated. 再次感谢您的帮助。

cygps gave some good code if you are utilizing UTMs in a single zone and have limited data points, so try that out if you have those parameters. 如果您在单个区域中使用UTM并且数据点有限,则cygps提供了一些不错的代码,因此,如果您有这些参数,请尝试一下。

foo <- function(df) {
      x1 <- x2 <- df$x
      y1 <- y2 <- df$y
      Xpair<-merge(x1,x2)
        names(Xpair)<-c("x1","x2")
      Ypair<-merge(y1,y2)
       names(Ypair)<-c("y1","y2")

      dist <- c(sqrt((Xpair$x1 - Xpair$x2)^2 + (Ypair$y1 - Ypair$y2)^2), NA)

      dx <- c(Xpair$x1 - Xpair$x2, NA)
      dy <- c(Ypair$y1 - Ypair$y2, NA)
      abs.angle <- ifelse(dist < 1e-07, NA, atan2(dy, dx))
      so <- list(dist,  abs.angle)
      return(so)
    }

I adapted this function from adehabitatLT:as.ltraj . 我从adehabitatLT:as.ltraj修改了此功能。 It produces your distance and absolute angle matrices (assuming you wanted distances and angles between all points, not time-ordered distances and angles). 它会生成您的距离和绝对角度矩阵(假设您想要所有点之间的距离和角度,而不是按时间顺序排列的距离和角度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM