微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在sf :: st_sample中指定点之间的最小距离?

如何解决如何在sf :: st_sample中指定点之间的最小距离?

是否有一种方法可以为sf::st_sample =随机或常规指定type中点之间的最小距离?

library(sf)

#Data download

download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.dbf",destfile = "SP_3543907_USO.dbf",mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.prj",destfile = "SP_3543907_USO.prj",mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shp",destfile = "SP_3543907_USO.shp",mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shx",destfile = "SP_3543907_USO.shx",mode = "wb")

#Data import

uso <- sf::st_read("SP_3543907_USO.shp") # pode demorar
uso

#Data plot

plot(uso$geometry)

#Random points

pts <- st_sample(uso,size = 20,type="random")
pts <- st_sf(pts)

解决方法

虽然在对随机点进行采样时无法指定最小距离(我们怎么能使它们有序,即不是随机的),但可以在之后修剪随机点列表,从而强制最小距离。 / p>

在我之前有关该主题的博客文章-https://www.jla-data.net/eng/creating-and-pruning-random-points-and-polygons/

的基础上,请考虑此工作流程的具体示例
library(sf)

#Data download

download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.dbf",destfile = "SP_3543907_USO.dbf",mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.prj",destfile = "SP_3543907_USO.prj",mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shp",destfile = "SP_3543907_USO.shp",mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shx",destfile = "SP_3543907_USO.shx",mode = "wb")

#Data import

uso <- sf::st_read("SP_3543907_USO.shp") # pode demorar
uso

#Data plot

plot(uso$geometry)

#Random points

pts <- st_sample(uso,size = 20,type="random")
pts <- st_sf(pts)

original <- pts # for comparison later :)


i <- 1 # iterator start

buffer_size <- 5000 # minimal distance to be enforced (in units of your CRS)

repeat( {
  #  create buffer around i-th point
  buffer <- st_buffer(pts[i,],buffer_size ) 
  
  offending <- pts %>%  # start with the intersection of master points... 
    st_intersects(buffer,sparse = F) # ... and the buffer,as a vector
  
  # i-th point is not really offending - it is the origin (not to be excluded)
  offending[i] <- FALSE
  
  # if there are any offending points left - re-assign the master points,# with the offending ones excluded / this is the main pruning part :)
  pts <- pts[!offending,] 
  
  if ( i >= nrow(pts)) {
    # the end was reached; no more points to process
    break 
  } else {
    # rinse & repeat
    i <- i + 1 
  }
  
} )


plot(original,pch = 4,col = "grey25") # result of the st_sample()
plot(pts,col = "red",add = T) # pruned points

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。