Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从st_样品(R包装sf)中获得六角型样品?_R_Sf - Fatal编程技术网

如何从st_样品(R包装sf)中获得六角型样品?

如何从st_样品(R包装sf)中获得六角型样品?,r,sf,R,Sf,我想从一个区域创建一些采样点。这些点必须给人一种密度的印象。我希望它们不是随机的,以避免人们认为它们是“真实的”观察结果。我希望它们在整个地区呈六边形分布。如何获得这样的样品st_样本与type=“hexagon”不起作用 一个可重复的例子: library(sf) nc <- st_read(system.file("shape/nc.shp", package="sf")) # this works: nc_samples_random <- st_sample(nc[1,],

我想从一个区域创建一些采样点。这些点必须给人一种密度的印象。我希望它们不是随机的,以避免人们认为它们是“真实的”观察结果。我希望它们在整个地区呈六边形分布。如何获得这样的样品<代码>st_样本与
type=“hexagon”
不起作用

一个可重复的例子:

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# this works:
nc_samples_random <- st_sample(nc[1,], 100, type = "random")
# this does not:
nc_samples_hexagonal <- st_sample(nc[1,], 100, type = "hexagonal")
非常感谢您的帮助

已编辑:参见底部的上一个答案

我认为
st_sample
上有一个bug。对于未投影形状(即,EPSG:4326),面积以米为单位计算,而
bbox
限制以经度和纬度为单位,这给出了问题中描述的例外情况

只要你能很好地突出你的形状,你就能实现你的目标。关于这一点,似乎在
st_sample
上存在一定程度的随机性,因此如果您需要确切的点数,您可以使用
种子
来获得正确的点数

library(sf)
library(units)

nc <- st_read(system.file("shape/nc.shp", package = "sf"))

# Project shape
nc_3857 = st_transform(nc[1, ], 3857)

#Reduce a little bit via negative buffer to avoid dots on the edge
nc_3857_red = st_buffer(nc_3857, dist = set_units(-2, "km"))

#Seed and sample
set.seed(2421)
nc_samples_hexagonal <-
  st_sample(nc_3857_red, 100, type = "hexagonal")

nc_unproj = st_transform(nc_3857, 4326)
nc_samples_hexagonal_unproj = st_transform(nc_samples_hexagonal, 4326)

plot(st_geometry(nc_unproj))
plot(st_geometry(nc_samples_hexagonal_unproj), add = T)
title(main = paste("N Dots Grid =", length(nc_samples_hexagonal)))


密度可以通过reprex
n=20
中的
cellsize
n
参数进行调整,这主意不错,收缩部分也不错。您将如何使用此方法获取一定数量的点的坐标?我在
sf
repo上打开了一个问题来检查这一点:
library(sf)
library(units)

nc <- st_read(system.file("shape/nc.shp", package = "sf"))

# Project shape
nc_3857 = st_transform(nc[1, ], 3857)

#Reduce a little bit via negative buffer to avoid dots on the edge
nc_3857_red = st_buffer(nc_3857, dist = set_units(-2, "km"))

#Seed and sample
set.seed(2421)
nc_samples_hexagonal <-
  st_sample(nc_3857_red, 100, type = "hexagonal")

nc_unproj = st_transform(nc_3857, 4326)
nc_samples_hexagonal_unproj = st_transform(nc_samples_hexagonal, 4326)

plot(st_geometry(nc_unproj))
plot(st_geometry(nc_samples_hexagonal_unproj), add = T)
title(main = paste("N Dots Grid =", length(nc_samples_hexagonal)))
    library(sf)
    nc <- st_read(system.file("shape/nc.shp", package = "sf"))

    # Hexagonal grid
    nc_samples_hexagonal = st_make_grid(nc[1,],
                                        what = "corners",
                                        square = F,
                                        n = 20)

    # Extra: Shrink original shape to 95% to erase dots close to the edge
    polys = st_geometry(st_cast(nc[1,] , "POLYGON"))
    cntrd = st_geometry(st_centroid(polys))
    polyred = (polys - cntrd)  * 0.95 + cntrd
    st_crs(polyred) <- st_crs(nc[1,])
    nc_samples_hexagonal = nc_samples_hexagonal[st_contains(polyred,  nc_samples_hexagonal, sparse = F)]

    plot(st_geometry(nc[1,]))
    plot(st_geometry(nc_samples_hexagonal) , add = T)