Czy istnieje sposób, aby określić minimalną odległość między punktami w sf::st_sample dla type = losowa lub regularna?

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)
r sf
0
Igor Cobelo 16 październik 2020, 17:43

1 odpowiedź

Najlepsza odpowiedź

Chociaż nie jest możliwe określenie minimalnej odległości podczas pobierania próbek losowych (jak możemy? Uczyniłoby je uporządkowane, tj. Nie losowe) Możliwe jest przycinanie listy losowych punktów później, egzekwowanie minimalnej odległości.

Dla konkretnego przykładu należy rozważyć ten przepływ pracy, budynek na moim wcześniejszym wpisie na blogu na tym temacie - https://www.jla-data.net/pl/creating-In-Pruning-random-points- ipolygons/

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, pch = 4, col = "red", add = T) # pruned points

enter image description here

1
Jindra Lacko 21 październik 2020, 11:14