如何从st_sample(R包sf)中获取六边形样本? [英] How to obtain hexagonal type sample from st_sample (R package sf)?

查看:40
本文介绍了如何从st_sample(R包sf)中获取六边形样本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个区域创建一些样本点.这些点必须给人一种密度的印象.我希望它们不是随机的,以避免人们认为它们是真实"的观察结果.我希望它们在整个区域呈六边形分布.如何获得这样的样本?st_sample 带有 type = "hexagonal" 并不能解决问题.

一个可重复的例子:

库(sf)nc <- st_read(system.file("shape/nc.shp", package="sf"))# 这有效:nc_samples_random <- st_sample(nc[1,], 100, type = "random")# 这不是:nc_samples_hexagonal <- st_sample(nc[1,], 100, type = "hexagonal")

最后一行代码给出了这个错误信息:

seq_len(nrow(xy)) 中的错误:参数必须强制转换为非负整数

非常感谢任何帮助!

解决方案

已请参阅底部的上一个答案

我认为 st_sample 上存在一个错误

使用替代方法的先前答案

使用st_make_grid对非随机六边形点进行采样的替代方法:

 库(sf)nc <- st_read(system.file("shape/nc.shp", package = "sf"))# 六边形网格nc_samples_hexagonal = st_make_grid(nc[1,],what =角落",平方 = F,n = 20)# 额外:将原始形状缩小到 95% 以擦除靠近边缘的点polys = st_geometry(st_cast(nc[1,] , 多边形"))cntrd = st_geometry(st_centroid(polys))polyred = (polys - cntrd) * 0.95 + cntrdst_crs(polyred) <- st_crs(nc[1,])nc_samples_hexagonal = nc_samples_hexagonal[st_contains(polyred, nc_samples_hexagonal, sparse = F)]绘图(st_geometry(nc [1,]))绘图(st_geometry(nc_samples_hexagonal),添加= T)

密度可以通过 cellsizen 参数进行调整,在 reprex n=20 中.

I want to create some sample points from an area. The points have to give an impression of density. I want them not to be random, to avoid people thinking they are "real" observations. I want them to be hexagonal distributed across the area. How to get such a sample? st_sample with type = "hexagonal" does not do the trick.

A reproducible example:

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

The last line of code gives this error message:

Error in seq_len(nrow(xy)) : argument must be coercible to non-negative integer

Any help is much appreciated!

解决方案

EDITED: See previous answer on the bottom

I think there is a bug on st_sample source code. For unprojected shapes (i.e. EPSG:4326) the area is computed in meters whereas the bbox limits are taken as longitude and latitude, which gives the exception described in your question.

As long as you are fine projecting your shape you can achieve your goal. A point on that, it seems that there is some degree of randomness on st_sample, so if you need an exact number of points you can play with the seed to get the right number.

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

PREVIOUS ANSWER W/ ALTERNATIVE APPROACH

Alternative approach for sampling non-random hexagonal points by using st_make_grid:

    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)

Density can be adjusted either by the cellsize or the n paramater, in the reprex n=20.

这篇关于如何从st_sample(R包sf)中获取六边形样本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆