Bre*_*per 5 random r spatial coordinates jitter
R 用户相当新手,我确信有一个简单的解决方案 - 但我找不到它。我有一个包含一系列空间坐标以及许多其他属性的数据框。许多空间坐标是完全相同的 - 我想向它们添加一定量的噪声,以便我可以将它们保持在一定的半径内 - 在本例中为 0.4 米或 40 厘米,同时还跟踪它们相关属性。
我本质上是在寻找这个问题的R版本:https://gis.stackexchange.com/questions/35479/adding-noise-to-overlapping-xy-coordinates-so-no-longer-in-exact-same-地方
...因为当我按照使用 ArcGIS 回答这个问题的说明时 - 我得到了一系列随机点,但我丢失了与这些点关联的属性,并且我无法轻松找到一种方法来匹配它们。
有没有办法使用 R 中的抖动函数或类似的函数并指定空间上下文(例如 40 厘米)中的半径,空间坐标在该范围内随机分布?我不明白如何操纵因子和数量参数以获得我想要的输出。
编辑:
这是一个带有假坐标的 df 示例。正如您所看到的,第一列和第三列中的坐标是相同的,因为动物在同一块岩石下两次。我希望能够为这些坐标添加抖动,使它们略有不同,但我想将抖动控制在40厘米以内(不超过岩石的大小)
mydf <- data.frame("point_id" = 1:6, "date_time" = c("6/5/2018 10:57","6/5/2018 14:30","6/6/2018 10:06","6/6/2018 11:06","6/7/2018 10:35","6/7/2018 15:50"), "Animal_ID" = c(4,5,4,5,4,6), "Rock_ID" = c(1,2,1,3,4,5), x_proj = c(831120.3759,831441.0415,831120.3759,831433.4414,831128.4778,831422.0822), y_proj = c(5877582.998,5875337.074,5877582.998,5875328.897,5877575.360,5875338.216))
#make a separate object for the coordinates#
xy <- mydf[,c(5,6)]
#Convert to a spatialpoints data frame (insert own epsg)
sp.mydf <- SpatialPointsDataFrame(coords = xy, data = tumbling_test, proj4string = CRS("+init=epsg:xxxxx"))
Run Code Online (Sandbox Code Playgroud)
我希望新生成的坐标仍包含其他列中的属性数据(即 Animal_ID、日期等),因为我在 ArcGIS 中使用的其他方法会生成一系列新的随机点,但我无法将它们与属性匹配。
另外,如果有一种方法可以仅将抖动添加到岩石上多次出现的点,那就太好了。例如这里我只需要向第1行和第3行添加抖动,因为其他坐标不重复。添加抖动后,我想将结果转换回常规数据帧,并将其导出为 .csv
有,而且如果你使用 sf 对象,那就容易多了。
您还需要深入研究坐标参考系统 (CRS),以便将点抖动到正确的距离。
如果您从 SpatialPointsdataframe 开始,请使用st_as_sf()返回 sf 对象。
下面是一个可重现的示例,其中点在 5 公里左右抖动。抖动有些随机,在本例中变化范围为约 2-5.5 公里。
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
library(tidyverse)
# load example data
nc <- read_sf(system.file('gpkg/nc.gpkg', package = 'sf'))
#make single points from polygons
nc_points <- st_centroid(nc)
#> Warning in st_centroid.sf(nc): st_centroid assumes attributes are constant over
#> geometries of x
#> Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
#> of_largest_polygon): st_centroid does not give correct centroids for longitude/
#> latitude data
# Transform to a crs that uses meters as the distance
nc_points <- st_transform(nc_points, 3358)
nc_points_jittered <- st_jitter(nc_points, amount = 5000)
p1 <- ggplot(nc_points) +
geom_sf() +
ggtitle('Original')
p2 <- ggplot(nc_points_jittered) +
geom_sf() +
ggtitle('Jittered')
p3 <- ggplot() +
geom_sf(data = nc_points, color = 'red') +
geom_sf(data = nc_points_jittered, color = 'black') +
ggtitle('Both')
cowplot::plot_grid(p1, p2, p3, ncol = 1)
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2020 年 1 月 12 日创建(v0.3.0)