How to obtain hexagonal type sample from st_sample (R package sf)?

痴心易碎 提交于 2020-06-29 06:52:42

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/59208954/how-to-obtain-hexagonal-type-sample-from-st-sample-r-package-sf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!