File: dist.R

package info (click to toggle)
r-cran-sf 0.9-7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,796 kB
  • sloc: cpp: 5,333; sh: 18; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
suppressPackageStartupMessages(library(sf))
library(sp)
suppressPackageStartupMessages(library(units))

x = st_sfc(
st_point(c(0,0)),
st_point(c(1,0)),
st_point(c(2,0)),
st_point(c(3,0)),
crs = 4326
)

y = st_sfc(
st_point(c(0,10)),
st_point(c(1,0)),
st_point(c(2,0)),
st_point(c(3,0)),
st_point(c(4,0)),
crs = 4326
)

d.sf = st_distance(x, y)
d.sp = spDists(as(x, "Spatial"), as(y, "Spatial"))
units(d.sp) = as_units("km")
round(d.sf - d.sp, 7)

#summary(unclass(d.sf) - d.sp)

st_crs(x) = st_crs(y) = NA
d.sf = st_distance(x, y)
d.sp = spDists(as(x, "Spatial"), as(y, "Spatial"))
round(d.sf - d.sp, 7)

# st_length:
st_crs(y) = 4326
(z = st_sfc(st_linestring(rbind(c(0,10), c(1,0), c(2,0), c(3,0), c(4,0))), crs = 4326))
d = st_distance(y, y)
round(d, 7)
st_length(z)
st_length(z) - sum(d[1,2], d[2,3], d[3,4], d[4,5])

# st_line_sample:
ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
	st_linestring(rbind(c(0,0),c(10,0))))
# set.seed(135)
st_line_sample(ls, density = 1)

ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
	st_linestring(rbind(c(0,0),c(.1,0))), crs = 4326)

st_length(ls)
try(st_line_sample(ls, density = 1/1000))
x = st_line_sample(st_transform(ls, 3857), density = 1/1000) # one per km