You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R terra包读取NetCDF时遇投影异常与Conventions属性警告

NetCDF文件读取警告及投影异常问题处理

问题现象

  • 用terra读取含3年数据的NetCDF文件时,出现GDAL警告:

    gdp <- terra::rast(file.path(dir_ls$base, 'v3.nc'))
    

    警告信息:

    Warning message:
    In new_CppObject_xp(fields$.module, fields$.pointer, ...) :
       GDAL Message 1: No UNIDATA NC_GLOBAL:Conventions attribute
    
  • 检查投影返回NA,执行投影转换后仍无法识别:

    crs(gdp)
    # 返回 NA
    gdp <- terra::project(gdp, "epsg:4326")
    crs(gdp)
    # 依然返回 NA
    

环境与文件信息

依赖库版本

terra::gdal(lib="")

输出:

gdal    proj    geos 
"2.2.3" "4.9.3" "3.7.1"

NetCDF文件详情

执行print(gdp)得到文件元数据:

class       : SpatRaster 
dimensions  : 21600, 43200, 3  (nrow, ncol, nlyr)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : GDP_PPP_30arcsec_v3.nc 
varname     : GDP_PPP (Gross Domestic Production (GDP) (PPP)) 
names       :                             GDP_PPP_1,                             GDP_PPP_2,                             GDP_PPP_3 
unit        : constant 2011 international US dollar, constant 2011 
international US dollar, constant 2011 international US dollar

R会话信息

sessionInfo()

输出:

R version 4.0.5 (2021-03-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
[1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
[4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
[7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C        

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base      

other attached packages:
[1] rgdal_1.5-32      rgeos_0.5-9       raster_3.4-10     sp_1.4-5          geodata_0.4-9    
[6] sf_0.9-8          terra_1.6-7       forcats_0.5.1     stringr_1.4.0     dplyr_1.0.6      
[11] purrr_0.3.4       readr_1.4.0       tidyr_1.1.3       tibble_3.1.1      ggplot2_3.3.3    
[16] tidyverse_1.3.1   data.table_1.14.0

loaded via a namespace (and not attached):
[1] tinytex_0.31       tidyselect_1.1.1   xfun_0.22          lattice_0.20-44   
[5] haven_2.4.1        colorspace_2.0-1   vctrs_0.3.8        generics_0.1.0    
[9] utf8_1.2.1         rlang_0.4.11       e1071_1.7-6        pillar_1.6.0      
[13] glue_1.4.2         withr_2.4.2        DBI_1.1.1          dbplyr_2.1.1      
[17] modelr_0.1.8       readxl_1.3.1       lifecycle_1.0.0    munsell_0.5.0     
[21] gtable_0.3.0       cellranger_1.1.0   rvest_1.0.0        codetools_0.2-18  
[25] class_7.3-19       fansi_0.4.2        broom_0.7.6        Rcpp_1.0.6        
[29] KernSmooth_2.23-20 scales_1.1.1       backports_1.2.1    classInt_0.4-3    
[33] jsonlite_1.7.2     fs_1.5.0           hms_1.0.0          stringi_1.6.1     
[37] grid_4.0.5         cli_2.5.0          tools_4.0.5        magrittr_2.0.1    
[41] proxy_0.4-25       crayon_1.4.1       pkgconfig_2.0.3    ellipsis_0.3.2    
[45] xml2_1.3.2         reprex_2.0.0       lubridate_1.7.10   assertthat_0.2.1  
[49] httr_1.4.2         rstudioapi_0.13    R6_2.5.0           units_0.7-1       
[53] compiler_4.0.5    

解决步骤

  1. 消除GDAL警告
    警告源于文件缺少NC_GLOBAL:Conventions属性(符合CF规范的NetCDF通常会包含该属性),可通过GDAL命令行工具添加:

    gdal_edit.py -mo Conventions=CF-1.6 GDP_PPP_30arcsec_v3.nc
    

    该操作仅补充元信息,不影响数据本身。

  2. 手动设置投影信息
    从文件元数据看,数据范围和分辨率对应WGS84(EPSG:4326),但terra未识别,直接手动设置CRS而非用project(project用于投影转换,不是设置CRS):

    crs(gdp) <- "EPSG:4326"
    # 验证设置结果
    crs(gdp)
    
  3. 升级依赖库(可选但推荐)
    当前GDAL(2.2.3)和PROJ(4.9.3)版本较旧,对NetCDF元数据解析支持有限,建议升级到新版本:

    • Ubuntu系统通过PPA源升级依赖:
      sudo add-apt-repository ppa:ubuntugis/ppa
      sudo apt update
      sudo apt install libgdal-dev libproj-dev
      
    • 重新安装terra包以适配新依赖:
      install.packages("terra", type="source")
      

内容的提问来源于stack exchange,提问作者89_Simple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:12:18