简体   繁体   English

R 模拟空间点数据,其中点与前一点的距离最大

[英]R simulate spatial point data with points a maximum distance from previous point

I want to generate point data within a polygon where I provide a starting point and generate n points, where each point is generated within a maximum radius of the previous point.我想在多边形内生成点数据,在该多边形中提供起点并生成 n 个点,其中每个点都在前一个点的最大半径内生成。 This is based on the assumption that an object that I'm tracking can only travel a maximum distance between two ping intervals.这是基于假设我正在跟踪的 object 只能在两个 ping 间隔之间移动最大距离。

The spatstat::rSSI function does something similar but it specifies a minimum distance between points whereas I want a maximum distance and for it to be from the previous point not all points. spatstat::rSSI function 做了类似的事情,但它指定了点之间的最小距离,而我想要一个最大距离,并且它来自前一点而不是所有点。 https://gis.stackexchange.com/questions/163287/randomly-sampling-points-in-r-with-minimum-distance-constraint https://gis.stackexchange.com/questions/163287/randomly-sampling-points-in-r-with-minimum-distance-constraint

here is a first approach这是第一种方法

#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 )

在此处输入图像描述

not implemented: polygon boundaries, since no sample data is provided and I'm a lazy boy;-) You could include a test inside the for loop to see if the new coordinates are within boundaries of the polygon, and if not;未实现:多边形边界,因为没有提供示例数据,而且我是个懒惰的男孩;-) 您可以在 for 循环中包含一个测试,以查看新坐标是否在多边形的边界内,如果不是; recalculate.重新计算。

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

相关问题 空间数据:计算点与最大点值的距离并绘图 - Spatial data: calculating the distance of points from the maximum point value and plotting 点距离内的点 - Points within a distance from a point 计算从多边形到空间点的最小距离 - Calculate minimum distance from polygon to spatial point 如何使用R计算点与参考点之间的距离? - How to Calculate Distance between points from a reference point using R? 从 R 中的空间点数据中删除每隔一行和一列 - Delete every second row and column from spatial point data in R 在R中的空间点数据周围创建缓冲区,并计算缓冲区中有多少个点 - Create buffer around spatial point data in R and count how many points are in the buffer 点到固定目标点的平均距离 - Mean distance of the points from a fixed target point 查找指定方向到空间点的最近距离 - Find nearest distance from spatial point with direction specified 使用R将空间点数据集与空间网格数据集合并。(主数据集为SP点格式) - Merge spatial point dataset with Spatial grid dataset using R. (Master dataset is in SP Points format) 从 SpatialPointsDataFrame 中的每个点到第二个 shapefile 中最近的点/线的最快笛卡尔距离 (R) - Fastest cartesian distance (R) from each point in SpatialPointsDataFrame to closest points/lines in 2nd shapefile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM