R模拟空间点数据,点与前一个点的最大距离
R simulate spatial point data with points a maximum distance from previous point
我想在一个多边形内生成点数据,我提供一个起点并生成 n 个点,其中每个点都在前一个点的最大半径内生成。这是基于我正在跟踪的对象只能在两个 ping 间隔之间移动最大距离的假设。
spatstat::rSSI 函数做类似的事情,但它指定点之间的最小距离,而我想要最大距离,并且它是从前一个点开始的,而不是所有点。
https://gis.stackexchange.com/questions/163287/randomly-sampling-points-in-r-with-minimum-distance-constraint
这是第一种方法
#set start point
start <- matrix( data = c( 5, 52), ncol = 2, dimnames = list( "", c("lon", "lat") ) )
# lon lat
# 5 52
n = 10 #how many points from startpoint?
max_dist = 100 #maximum distance to previous point in metres
#pre-allocate
L <- vector( mode = "list", length = 1+n )
#and initialise startpoint
L[[1]] <- start
library( geosphere )
set.seed(123) #for reproduction only, remove in production!
for ( i in 2:(n+1) ) {
L[[i]] <- geosphere::destPoint( p = L[[i-1]], #start = previous point
b = runif(1, 0, 360), #random bearing bewteen 0 and 360 degrees
d = runif(1, 0, max_dist ) #random distanxe between 0 and max_dist
)
}
#transform to a data.table
library( data.table )
ans <- data.table::rbindlist( lapply( L, as.data.table ), use.names = TRUE, fill = TRUE)
# lon lat
# 1: 5.000000 52.00000
# 2: 5.000336 51.99966
# 3: 5.000835 51.99979
# 4: 5.000546 52.00016
# 5: 5.000907 51.99989
# 6: 5.001065 51.99983
# 7: 5.000414 52.00002
# 8: 5.001240 52.00046
# 9: 5.001055 52.00062
#10: 5.000992 52.00113
#11: 5.000553 52.00109
#visual confirmation
library(sf)
library(leaflet)
leaflet( data = ans) %>% addTiles() %>%
addCircleMarkers( lng = ~lon, lat = ~lat ) %>%
addPolylines( lng = ~lon, lat = ~lat )
未实现:多边形边界,因为没有提供示例数据,而且我是个懒惰的孩子;-)
您可以在 for 循环中包含一个测试,以查看新坐标是否在多边形的边界内,如果不在;重新计算。
我想在一个多边形内生成点数据,我提供一个起点并生成 n 个点,其中每个点都在前一个点的最大半径内生成。这是基于我正在跟踪的对象只能在两个 ping 间隔之间移动最大距离的假设。
spatstat::rSSI 函数做类似的事情,但它指定点之间的最小距离,而我想要最大距离,并且它是从前一个点开始的,而不是所有点。 https://gis.stackexchange.com/questions/163287/randomly-sampling-points-in-r-with-minimum-distance-constraint
这是第一种方法
#set start point
start <- matrix( data = c( 5, 52), ncol = 2, dimnames = list( "", c("lon", "lat") ) )
# lon lat
# 5 52
n = 10 #how many points from startpoint?
max_dist = 100 #maximum distance to previous point in metres
#pre-allocate
L <- vector( mode = "list", length = 1+n )
#and initialise startpoint
L[[1]] <- start
library( geosphere )
set.seed(123) #for reproduction only, remove in production!
for ( i in 2:(n+1) ) {
L[[i]] <- geosphere::destPoint( p = L[[i-1]], #start = previous point
b = runif(1, 0, 360), #random bearing bewteen 0 and 360 degrees
d = runif(1, 0, max_dist ) #random distanxe between 0 and max_dist
)
}
#transform to a data.table
library( data.table )
ans <- data.table::rbindlist( lapply( L, as.data.table ), use.names = TRUE, fill = TRUE)
# lon lat
# 1: 5.000000 52.00000
# 2: 5.000336 51.99966
# 3: 5.000835 51.99979
# 4: 5.000546 52.00016
# 5: 5.000907 51.99989
# 6: 5.001065 51.99983
# 7: 5.000414 52.00002
# 8: 5.001240 52.00046
# 9: 5.001055 52.00062
#10: 5.000992 52.00113
#11: 5.000553 52.00109
#visual confirmation
library(sf)
library(leaflet)
leaflet( data = ans) %>% addTiles() %>%
addCircleMarkers( lng = ~lon, lat = ~lat ) %>%
addPolylines( lng = ~lon, lat = ~lat )
未实现:多边形边界,因为没有提供示例数据,而且我是个懒惰的孩子;-) 您可以在 for 循环中包含一个测试,以查看新坐标是否在多边形的边界内,如果不在;重新计算。