Search code examples
rgpsgisr-sf

simple feature - create new points with certain properties


I've a simple features with a simple point, arround this point I like to create 6 new points equal distributed and 100 meter away from the orginal point. Is this easily solvable in R (sf) and do you have perhaps an example how I could implement something like this?


Solution

  • Start by buffering the point by 100 meters, cast it to a linestring, and then sample the buffered linestring:

    library(sf)
    #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
    library(tidyverse)
    
    
    set.seed(41)
    point <- st_sfc(st_point(c(0, 51.5))) %>% st_set_crs(4326) # a point around London
    
    buffer <- st_buffer(point, dist = 100) %>% 
      st_cast('LINESTRING')
    
    six_points <- st_sample(buffer, size = 6, type = 'regular')
    #> although coordinates are longitude/latitude, st_sample assumes that they are
    #> planar
    
    #distances:
    six_points %>% st_as_sf() %>% st_cast('POINT') %>% st_distance(point)
    #> Units: [m]
    #>          [,1]
    #> [1,] 100.5366
    #> [2,] 100.2739
    #> [3,] 100.7187
    #> [4,] 100.5599
    #> [5,] 100.2410
    #> [6,] 100.6761
    

    Pretty close. Choosing a better crs would help if you need cm accuracy.

    
    ggplot() +
      geom_sf(data = point, col = 'red') + 
      geom_sf(data = buffer) +
      geom_sf(data = six_points, color = 'turquoise', size = 4) +
      theme_void()
    

    On a map:

    library(mapview)
    mapview(point) +
      buffer +
      six_points
    
    

    library(mapview)
mapview(point) +
buffer +
six_points

    Created on 2023-03-31 by the reprex package (v2.0.1)