1suppressPackageStartupMessages(library(sf)) 2library(sp) 3suppressPackageStartupMessages(library(units)) 4 5x = st_sfc( 6st_point(c(0,0)), 7st_point(c(1,0)), 8st_point(c(2,0)), 9st_point(c(3,0)), 10crs = 4326 11) 12 13y = st_sfc( 14st_point(c(0,10)), 15st_point(c(1,0)), 16st_point(c(2,0)), 17st_point(c(3,0)), 18st_point(c(4,0)), 19crs = 4326 20) 21 22d.sf = st_distance(x, y) 23d.sp = spDists(as(x, "Spatial"), as(y, "Spatial")) 24units(d.sp) = as_units("km") 25round(d.sf - d.sp, 7) 26 27#summary(unclass(d.sf) - d.sp) 28 29st_crs(x) = st_crs(y) = NA 30d.sf = st_distance(x, y) 31d.sp = spDists(as(x, "Spatial"), as(y, "Spatial")) 32round(d.sf - d.sp, 7) 33 34# st_length: 35st_crs(y) = 4326 36(z = st_sfc(st_linestring(rbind(c(0,10), c(1,0), c(2,0), c(3,0), c(4,0))), crs = 4326)) 37d = st_distance(y, y) 38round(d, 7) 39st_length(z) 40st_length(z) - sum(d[1,2], d[2,3], d[3,4], d[4,5]) 41 42# st_line_sample: 43ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))), 44 st_linestring(rbind(c(0,0),c(10,0)))) 45# set.seed(135) 46st_line_sample(ls, density = 1) 47 48ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))), 49 st_linestring(rbind(c(0,0),c(.1,0))), crs = 4326) 50 51st_length(ls) 52try(st_line_sample(ls, density = 1/1000)) 53x = st_line_sample(st_transform(ls, 3857), density = 1/1000) # one per km 54