Open mdsumner opened 5 years ago
This:
llh2xyz <- function(lonlatheight, rad = 6370997, exag = 1) {
d2r <- pi / 180.0
cosLat = cos(lonlatheight[,2] * d2r)
sinLat = sin(lonlatheight[,2] * d2r)
cosLon = cos(lonlatheight[,1] * d2r)
sinLon = sin(lonlatheight[,1] * d2r)
x = rad * cosLat * sinLon
y = rad * cosLat * cosLon
z = (lonlatheight[,3] * exag) + rad * sinLat
cbind(x, y,-z)
}
library(sp)
library(rgl)
data("wrld_simpl", package = "maptools")
## raw coordinates from maptools
ll <- coordinates(as(as(wrld_simpl, "SpatialLines"), "SpatialPoints"))
a <- 6378137
xyz <- llh2xyz(cbind(ll, 0), rad = a)
laea <- "+proj=laea +lat_0=-90 +datum=WGS84"
pxy <- reproj::reproj(ll, laea, source = 4326)[,1:2]
## these are the projected map points on the plane
pxyz <- cbind(pxy, a)
clear3d()
## plot
bg3d(bg = "white")
plot3d(xyz, col = "dodgerblue", axes = FALSE)
points3d(pxyz, col = "#6AB787FF")
aspect3d("iso")
for (i in sample(seq_len(nrow(xyz)), 50)) {
line <- cbind(ll[i, 1], seq(90,0, length.out = 36))
dxyz <- c(0, 0, a) - xyz[i, ]
line_xyz <- llh2xyz(cbind(line, 0), rad = sqrt(sum(dxyz^2)))
line_xyz[,3] <- line_xyz[,3] + a
lines3d(line_xyz, alpha = 1, col = "white")
lines3d(rbind(c(0, 0, a), xyz[i, ]), col = "yellow")
points3d(xyz[i, , drop = FALSE], size = 8, col = "firebrick")
}
(using PROJ for geocent gives a kind of confusing orientation, for another day)