-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgreatCircle.R
45 lines (39 loc) · 1.08 KB
/
greatCircle.R
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
# author Robert Hijmans
# October 2009
# version 0.1
# license GPL
greatCircle <- function(p1, p2, n=360, sp=FALSE) {
p1 <- .pointsToMatrix(p1)
p2 <- .pointsToMatrix(p2)
p <- cbind(p1[,1], p1[,2], p2[,1], p2[,2], n)
p1 <- p[,1:2]
p2 <- p[,3:4]
n <- pmax(round(p[,5]), 1)
if (nrow(p) == 1) {
lon <- (1:n * 360 / n) - 180
lat <- gcLat(p1, p2, lon)
res <- cbind(lon,lat)
if (sp) {
lat <- gcLat(p1, p2, 180)
res <- list(rbind(cbind(-180, lat), res))
res <- SpatialLines( list( Lines( list( Line (res)), ID=as.character(1)) ), CRS("+proj=longlat +ellps=WGS84"))
}
} else {
res <- list()
for (i in 1:nrow(p1)) {
lon <- (1:n[i] * 360 / n[i]) - 180
lat <- gcLat(p1[i,], p2[i,], lon)
res[[i]] <- cbind(lon, lat)
}
if (sp) {
for (i in 1:length(res)) {
lat <- gcLat(p1[i,], p2[i,], 180)
res[[i]] <- rbind(cbind(-180, lat), res[[i]])
res[[i]] <- Lines( list( Line (res[[i]])), ID=as.character(i))
}
res <- SpatialLines(res, CRS("+proj=longlat +ellps=WGS84"))
}
}
return(res)
}
#greatCircle(rbind(cbind(5,52), cbind(5,15)), c(-120,37), n=12)