rrastershapefiler-rasterrgdal

Is there a way to randomly create points (geo-referenced) in a raster within the area defined by a shapefile? Rstudio


Ok so, this might actually be very straightforward and I apologize in advance.

I am using R for a geospatial analysis. I want to create a series of geo-referenced points which are randomly (but uniformly) generated across the area delimited by the limits of a shapefile.

Let's say that I Have a raster of the american continent and the shapefiles of the major lakes (just an example). I would like to create points for one species inside lake 1, then for another inside lake 2.

Is this at all possible? Using shapefiles to limit the geographical extent and then randomly sampling a pre-determined (and equidistanced) number of points?

Thank you very much in advance for any help and insight.

I have yet to try anything because I have actually no idea where to start in terms of randomly generating the points but according to the specificity of them being equidistant

Looking into this question using ggmap to plot points randomly distributed within boundaries of shape file that was suggested by stack overflow, part of my question is answered. However, how would I go to get, let's say, a point every km in a type of grid inside the shape?

Cheers Francisco


Solution

  • Here is an example showing how you can get 10 regularly distributed points for each of the second level subdivisions of Luxembourg.

    library(terra)
    v <- vect(system.file("ex/lux.shp", package="terra"))
    set.seed(12)
    s <- spatSample(v, 10, "regular", strata="NAME_2")
    
    plot(v)
    points(s, col="red")
    

    enter image description here