我通過投影是剛出來的觀點地球外的點,並與這些「出」點填充可見多邊形一起解決它。然後用黑色多邊形覆蓋溢出的陸地。
#Get contiguous country coordinates
contigcoord <- function (database = "world", regions = ".", exact = FALSE, boundary = TRUE, interior = TRUE, fill = FALSE, xlim = NULL, ylim = NULL){
if (is.character(database))
as.polygon = fill
else as.polygon = TRUE
coord <- maps:::map.poly(database, regions, exact, xlim, ylim, boundary,
interior, fill, as.polygon)
return(coord)
}
我也寫投影功能:
爲了做到這一點,我使用下面的函數(從地圖包修改)中檢索連續的陸塊的座標。 「前」 = 1,如果點應該是可見的:
#Lat/Long to x-y (0,1 range) in orthographic projection. Cen is the centre point of map
mapproj <- function(lat,long,cenlat,cenlong){
d2r=pi/180; lat=lat*d2r; long=long*d2r; cenlat=cenlat*d2r; cenlong=cenlong*d2r
x=cos(lat)*sin(long-cenlong)
y=cos(cenlat)*sin(lat)-sin(cenlat)*cos(lat)*cos(long-cenlong)
front=sin(cenlat)*sin(lat)+cos(cenlat)*cos(lat)*cos(long-cenlong) > 0
return(list(x=x,y=y,front=front))
}
用於填充陸地如下(cenlat和岑隆是在可見地球的中心的座標)的代碼:
xy <- contigcoord("world",fill=TRUE)
coord <- cbind(xy$x,xy$y)
coordtr <- mapproj(coord[,2],coord[,1],cenlat,cenlong)
coord <- cbind(coord,coordtr$x,coordtr$y,coordtr$front)
naloc <- (1:nrow(coord))[!complete.cases(coord)]
naloc <- c(0,naloc)
for(i in 2:length(naloc)){
thispoly <- coord[(naloc[i-1]+1):(naloc[i]-1),3:5,drop=F]
thispoly <- rbind(thispoly,thispoly[1,])
unq <- unique(thispoly[,3])
if(length(unq) == 1){
if(unq == 1){ #Polygon is fully on front side
polygon(thispoly[,1],thispoly[,2],col=rgb(0.5,0.8,0.5),border=NA)
}
} else { #front and back present
ind <- thispoly[,3] == 0
#project points "outside" the globe
temdist <- pmax(sqrt(rowSums(as.matrix(thispoly[ind,1:2]^2))),1e-5)
thispoly[ind,1:2] <- thispoly[ind,1:2]*(2-temdist)/temdist
polygon(thispoly[,1],thispoly[,2],col=rgb(0.5,0.8,0.5),border=NA)
}
}
結果看起來是這樣的(應用在全球範圍內的黑色層前):
附註:我不知道爲什麼,但我根本無法把在「映射」標籤 - 它someh ow自動更改爲「詞典」... – David
是的,它的工作。謝謝。 – David