lundi 22 novembre 2021

Generating random points within sub geometries using st_sample from the sf package

Basically I'm trying to create 5 random spatial points within polgons of a shapefile. I am trying to use st_sample() from the sf package, but am having trouble with my for loop.

Example:

library(terra)
library(sf)

#10 polygons
v <- vect(system.file("ex/lux.shp", package="terra"))
v <- v[c(1:10)] 

#Empty list to store values
empty_list <- list()

#for loop
for(i in 1:length(v$ID_1)){
  
  empty_list[i] <- st_sample(x = v[i,], size = 5, 
                             type = "random", exact =T, by_polygon = T)
}

The loop seems fairly simple and straightforward. I think the issue is that st_sample() is only storing 1 value with each iteration. Should I use something other than a list to store output values, or is a for loop not the correct option here?




Aucun commentaire:

Enregistrer un commentaire