通过RGL从“自然地球”形状文件中以球坐标绘制土地数据

时间:2015-07-16 13:24:13

标签: r plot 3d surface rgl

我从Natural Earth [1]下载了陆地多边形形状文件,我能够在球面坐标系上绘制/绘制海岸线。问题是我如何填补土地区域!我一直在搜索这几天,我提出的资源如下。我还添加了一个如何绘制/绘制海岸线的功能。有什么想法吗?

require(rgl)
require(maptools)

shapefile_lines <- function() {
  save <- par3d(skipRedraw=TRUE)
  on.exit(par3d(save))
  r <- 6378100 # earth radius in meters
  shp <- readShapeSpatial(fn = "ne_10m_coastline/ne_10m_coastline.shp")
  # Combine lines in a single matrix
  mat <- do.call(rbind, sapply(1:length(shp@lines), function(i) rbind(shp@lines[[i]]@Lines[[1]]@coords,c(NA,NA))))
  # Convert spherical to cartesian
  xyz <- rgl.sph2car(mat[,2], mat[,1], radius=r + 200)
  bg3d("black")
  # Draw sphere
  rgl.sphere(ng=100, radius=r, col="gray50",specular = "black", add=T)
  plot3d(xyz, col = "white", add = T, type = "l")
}

rgl.sphere <- function (x=0, y=NULL, z=NULL, ng=50, radius = 1, color="white", add=F, set_normals=T, ...) {

  if(length(ng)==1) ng <- c(ng, ng)
  nlon <- ng[1]; nlat <- ng[2]

  lat <- matrix(seq(90, -90, len = nlat)*pi/180, nlon, nlat, byrow = TRUE)
  long <- matrix(seq(-180, 180, len = nlon)*pi/180, nlon, nlat)

  vertex  <- rgl:::rgl.vertex(x, y, z)
  nvertex <- rgl:::rgl.nvertex(vertex)
  radius  <- rbind(vertex, rgl:::rgl.attr(radius, nvertex))[4,]
  color  <- rbind(vertex, rgl:::rgl.attr(color, nvertex))[4,]

  for(i in 1:nvertex) {
    add2 <- if(!add) i>1 else T
    x <- vertex[1,i] + radius[i]*cos(lat)*cos(long)
    y <- vertex[2,i] + radius[i]*cos(lat)*sin(long)
    z <- vertex[3,i] + radius[i]*sin(lat)
    if(set_normals)
      persp3d(x, y, z, add=add2, color=color[i], normal_x=x, normal_y=y, normal_z=z, ...)
    else
      persp3d(x, y, z, add=add2, color=color[i], ...)
  }
}

rgl.sph2car <- function(lat=0, lon=0, radius=1, deg=T, precise=T) {
  if(deg) {
    if(precise){
      lat <- lat/180
      lon <- lon/180
      x <- radius*cospi(lat)*cospi(lon)
      y <- radius*cospi(lat)*sinpi(lon)
      z <- radius*sinpi(lat)
    }else{
      lat <- lat*pi/180
      lon <- lon*pi/180
      x <- radius*cos(lat)*cos(lon)
      y <- radius*cos(lat)*sin(lon)
      z <- radius*sin(lat)
    }
  }
  return(matrix(c(x,y,z),nrow=length(x), ncol=3, dimnames=list(NULL, c("x","y","z"))))
}

enter image description here

其他可能的相关资源:

1- 3d surface plot with xyz coordinates

2- https://gis.stackexchange.com/questions/90635/what-programs-would-allow-for-the-mapping-of-a-geoid-in-3d

3- Mesh generation from points with x, y and z coordinates

4- Z - Values for polygon (shapefile) in R

0 个答案:

没有答案