计算组内所有可能点之间的距离的方法。

huangapple go评论58阅读模式
英文:

How to calculate distance between all possible combinations of points within a group

问题

I have a sf object with 68 observation. In each location, I have 4 different points, with different PointIDs(SchlagID) but the same LocationIDs(FieldID). I want to now calculate the distance between each point by location in a way that there are a total of 6 values for each location (including the diagonal distances, assuming the points are in a square).

Till now, I have been able to calculate 4 values but not sure how to calculate the diagonal distances as well.

Here is what the data looks like

Here is the code I am working on:

pdist.df = data.frame(trapID=NA,LS=NA,dist=NA)
list.lsFeat = 1
counter=1

for(i in 1:nrow(raps_filtered_sf)){
  
  this.trap=raps_filtered_sf[i,]
  
  for(l in list.lsFeat){
    
    this.feat = raps_filtered_sf %>%
      filter(FieldID==this.trap$FieldID)
    
    #ind <- st_nearest_feature(this.trap, this.feat)
    dis <- st_distance(this.trap, this.feat)
    print(paste0("trap: ", this.trap$SchlagID, "//LS: ", this.feat$SchlagID, "//Dist: ", dis))
    
    pdist.df[counter,] = c(trapID=this.trap$SchlagID, LS=l, dist=dis)
    counter=counter+1
  }
}
英文:

I have a sf object with 68 observation. In each location I have 4 different points, with different PointIDs(SchlagID) but same LocationIDs(FieldID). I want to now calculate the distance between each point by location in a way that there a total of 6 values for each location (including the diagonal distances, assuming the points are in a square)

Till now I have been able to calculate 4 values but not sure how to calculate the diagonal distances as well.

Here is what the data looks like

Here is the code I am working on

pdist.df = data.frame(trapID=NA,LS=NA,dist=NA)
list.lsFeat = 1
counter=1


for(i in 1:nrow(raps_filtered_sf)){
    
  
  this.trap=raps_filtered_sf[i,]
  
  for(l in list.lsFeat){
    
    this.feat = raps_filtered_sf %&gt;%
      filter(FieldID==this.trap$FieldID)
    
    #ind &lt;- st_nearest_feature(this.trap, this.feat)
    dis &lt;- st_distance(this.trap, this.feat)
    print(paste0(&quot;trap: &quot;, this.trap$SchlagID, &quot;//LS: &quot;, this.feat$SchlagID, &quot;//Dist: &quot;, dis))
    
    pdist.df[counter,] = c(trapID=this.trap$SchlagID, LS=l, dist=dis)
    counter=counter+1
  }
}

答案1

得分: 1

我相信你正在寻找一个 sf::st_distance() 调用。当在单个 sf 数据框上使用它时,它会输出一个距离矩阵。

稍微调整一下名称可以更容易理解,但不是严格要求的。

举个例子,考虑下面这段代码,基于3个半随机的北卡罗来纳州城市(因为我深爱着附带 {sf} 包的 nc.shp 文件):

library(sf)
library(dplyr)

# 3 semi rancom cities in NC (because I *deeply love* the nc.shp file)
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
                     x = c(-78.633333, -79.819444, -77.912222),
                     y = c(35.766667, 36.08, 34.223333)) %>% 
  st_as_sf(coords = c("x", "y"), crs = 4326)

# prepare a distance matrix
mtx <- st_distance(cities)

# give the rows & cols meaningful names
colnames(mtx) <- cities$name
rownames(mtx) <- cities$name  

# see my work, and find it good...
mtx

# Units: [m]
#             Raleigh Greensboro Wilmington
# Raleigh         0.0   112342.9   183751.2
# Greensboro 112342.9        0.0   269595.9
# Wilmington 183751.2   269595.9        0.0

希望这能帮助你!

英文:

I believe you are looking for a sf::st_distance() call. When used on a single sf data frame it will output a distance matrix.

A little finetuning of names makes it easier to interpret, but is not strictly required.

For an example consider this piece of code, built on top of 3 semi-random North Carolina cities (because I am deeply in love with the nc.shp file that ships with {sf}):

library(sf)
library(dplyr)

# 3 semi rancom cities in NC (because I *deeply love* the nc.shp file)
cities &lt;- data.frame(name = c(&quot;Raleigh&quot;, &quot;Greensboro&quot;, &quot;Wilmington&quot;),
                     x = c(-78.633333, -79.819444, -77.912222),
                     y = c(35.766667, 36.08, 34.223333)) %&gt;% 
  st_as_sf(coords = c(&quot;x&quot;, &quot;y&quot;), crs = 4326)

# prepare a distance matrix
mtx &lt;- st_distance(cities)

# give the rows &amp; cols meaningful names
colnames(mtx) &lt;- cities$name
rownames(mtx) &lt;- cities$name  

# see my work, and find it good...
mtx

# Units: [m]
#             Raleigh Greensboro Wilmington
# Raleigh         0.0   112342.9   183751.2
# Greensboro 112342.9        0.0   269595.9
# Wilmington 183751.2   269595.9        0.0

huangapple
  • 本文由 发表于 2023年4月13日 21:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定