[英]Calculating distance between projected coordinates within individuals (adding distance column?)
我有一個具有不同個體和投影坐標的運動數據集。 使用以下代碼將坐標投影到 UTM 中:
coordinates(data) = ~x+y #turns data into spatial data frame
proj4string(data) <- CRS("+init=epsg:4326")
data <- spTransform(data, CRS("+init=epsg:5321")) #denotes what CRS to use, this changes the data into UTMs from lat/long, which projects the data
data <- as.data.frame(data)
我想計算個體內部坐標之間的距離(以米為單位)。 只要點之間保持一致,我就可以接受相當大的誤差范圍。
這是我的數據集示例:
ID x y
Bear1 459486.4 7181992
Bear1 459652.6 7181904
Bear1 459661.5 7181880
Bear2 459604.7 7181898
Bear2 459639.6 7181894
Bear2 459565.1 7181960
最理想的是,我希望在此數據集中添加一個距離列,以顯示點內的距離。
請在下面找到一個 reprex,它顯示了sf
包的解決方案。
如果我沒記錯的話,你提供的數據已經在EPSG:5321中了。 假設這樣,您只需要輸入以下幾行代碼即可獲得所有可能的點對之間的距離矩陣。
正品
library(sf)
data <- st_as_sf(data, coords = c("x", "y"), crs = 5321)
distances <- st_distance(data)
distances
#> Units: [m]
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 0.00000 188.05967 207.85574 151.09894 181.86325 84.95699
#> [2,] 188.05967 0.00000 25.59707 48.27432 16.40122 103.88575
#> [3,] 207.85574 25.59707 0.00000 59.58389 25.99250 125.27155
#> [4,] 151.09894 48.27432 59.58389 0.00000 35.12848 73.56738
#> [5,] 181.86325 16.40122 25.99250 35.12848 0.00000 99.53015
#> [6,] 84.95699 103.88575 125.27155 73.56738 99.53015 0.00000
由reprex 包(v0.3.0) 於 2021 年 10 月 27 日創建
如果要從頭開始,即從地理坐標(即EPSG = 4326)開始,則必須按照以下步驟進行:
library(sf)
data <- st_as_sf(data, coords = c("x", "y"), crs = 4326)
data <- st_transform(data, crs = 5321)
distances <- st_distance(data)
distances
data <- data.frame(ID = c("Bear1", "Bear1", "Bear1", "Bear2", "Bear2", "Bear2"),
x = c(459486.4, 459652.6, 459661.5, 459604.7, 459639.6, 459565.1),
y = c(7181992, 7181904, 7181880, 7181898, 7181894, 7181960))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.