Skip to content

Instantly share code, notes, and snippets.

@agila5
Created April 10, 2021 07:47
Show Gist options
  • Save agila5/926ab9c224635961dd6b0efd572300c6 to your computer and use it in GitHub Desktop.
Save agila5/926ab9c224635961dd6b0efd572300c6 to your computer and use it in GitHub Desktop.
# packages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(sfnetworks)

# create network
roxel_sfn <- as_sfnetwork(roxel, directed = FALSE)

# sample two points in that area
my_points <- st_sample(st_as_sfc(st_bbox(roxel)), size = 2)
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar

# shortest paths
st_network_paths(
  x = roxel_sfn, 
  from = my_points[1], 
  to = my_points[2]
)
#> although coordinates are longitude/latitude, st_nearest_points assumes that they are planar
#> although coordinates are longitude/latitude, st_nearest_points assumes that they are planar
#> Registered S3 method overwritten by 'cli':
#>   method     from         
#>   print.boxx spatstat.geom
#> # A tibble: 1 x 2
#>   node_paths edge_paths
#>   <list>     <list>    
#> 1 <int [24]> <int [23]>

# shortest paths
suppressMessages(st_network_paths(
  x = roxel_sfn, 
  from = my_points[1], 
  to = my_points[2]
))
#> # A tibble: 1 x 2
#>   node_paths edge_paths
#>   <list>     <list>    
#> 1 <int [24]> <int [23]>

Created on 2021-04-10 by the reprex package (v1.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment