其他分享
首页 > 其他分享> > nc to tif in R

nc to tif in R

作者:互联网

wd <- "G:/Rdata/Tmp/"
setwd(wd)

library(pacman)
p_load(raster, ncdf4,sf,ggplot2,RColorBrewer)

tmp2018_nc <- nc_open("tmp_2018.nc")  
View(tmp2018_nc)
names(tmp2018_nc$var)
varname <- 'tmp'
##
raster <- stack("G:/Rdata/Tmp/tmp_2018.nc",varname = varname)
x1 <- c(1:12)
x2 <- paste("2018",x1,sep = "_")

for(i in 1:12)
{
  writeRaster(raster[[i]],filename = x2[[i]], format = "GTiff")
}



tmp2018_1 <- raster("G:/Rdata/Tmp/2018_1.tif")
crs(tmp2018_1)
# CRS arguments: +proj=longlat +datum=WGS84 +no_defs 
plot(tmp2018_1)


shp <- st_read("G:/Rdata/China/GeoAltas/china.shp")
crs(shp)
# CRS arguments: +proj=longlat +datum=WGS84 +no_defs
tmp_mask <- mask(tmp2018_1, shp)
plot(tmp_mask)

tmp_mask_crop <- crop(tmp_mask,extent(shp))
tmp_mask_crop <- tmp_mask_crop/10
plot(tmp_mask_crop)


tmp_mask_crop_df <- as.data.frame(as(tmp_mask_crop,"Raster"),xy = TRUE)%>%
  na.omit()
View(tmp_mask_crop_df)



map<- ggplot()+
  geom_sf(data = shp, aes(fill = NULL))+
  geom_tile(data = tmp_mask_crop_df,aes(x = x,y = y,fill = X2018_1))+
  scale_fill_distiller(palette = "Spectral",direction = -1,
                       breaks = c(-40, -30, -20, -10, 0, 10, 20, 30))+
  labs(title = "中国2018年1月温度")

map

在这里插入图片描述
在这里插入图片描述

标签:tmp,raster,wd,omit,df,format,nc,tif
来源: https://blog.csdn.net/qq_41718357/article/details/116202613