简体   繁体   English

如何使用 R 将区域从 shapefile 传输到 netcdf 文件?

[英]How to transfer regions from a shapefile to a netcdf file using R?

I have a regional netcdf file with chlorophyll data and want to add a new variable "region" that maps information about the maritime region.我有一个包含叶绿素数据的区域 netcdf 文件,并希望添加一个新变量“区域”来映射有关海洋区域的信息。 So for each coordinate point there would be an information to which region this point belongs.因此,对于每个坐标点,都会有该点所属区域的信息。

So for all points in the netcdf file laying in the region of the Mediterranean sea, the variable "region" would for example contain the value 0 and for all points laying in the region of the North Sea, the variable "region" would for example contain the value 1 and so on...因此,对于 netcdf 文件中位于地中海区域的所有点,变量“region”将包含值 0,对于位于北海区域的所有点,变量“region”将例如包含值 1 等等...

I found a shapefile containing maritime regions based on biogeochemical processes (Longhurst Provinces).我发现了一个包含基于生物地球化学过程的海洋区域的 shapefile(朗赫斯特省)。 Now I want to use this shapefile to define my "region" variable in the netcdf file.现在我想使用这个 shapefile 在 netcdf 文件中定义我的“区域”变量。 Does anybody know how I could do that in R?有谁知道我怎么能在 R 中做到这一点?

I think my question is close to this post by DKRZ but I don't want to extract/mask only one region but map all regions that are defined in the shapefile.我认为我的问题与DKRZ的这篇文章很接近,但我不想只提取/屏蔽一个区域,而是 map 在 shapefile 中定义的所有区域。

The files can be found here:这些文件可以在这里找到:

https://drive.google.com/file/d/1kgPpHFapmuclDyUvw2TH_10i018GE9YH/view?usp=sharing https://drive.google.com/file/d/1kgPpHFapmuclDyUvw2TH_10i018GE9YH/view?usp=sharing

https://drive.google.com/file/d/1WLYEUs6XllZv6i0xjRif-N0syhR2lX01/view?usp=sharing https://drive.google.com/file/d/1WLYEUs6XllZv6i0xjRif-N0syhR2lX01/view?usp=sharing

Thanks a lot already!已经非常感谢了!

EDIT编辑
I found this post that helped me to solve my problem: https://www.r-bloggers.com/2014/05/converting-shapefiles-to-rasters-in-r/ )我发现这篇文章帮助我解决了我的问题: https://www.r-bloggers.com/2014/05/converting-shapefiles-to-rasters-in-r/

This is what I could put together based on Map Lat/Lon Points to a Shape File in R .这是我可以根据Map Lat/Lon Points to a Shape File in R 组合起来的 The main work is done by over() , which takes a few minutes.主要工作由over()完成,这需要几分钟。

library(ncdf4)
nc_data = nc_open('/mnt/d/Downloads/CCI_ALL-v5.0-MONTHLY.nc', T)    # write
lon = ncvar_get(nc_data, "lon")
lat = ncvar_get(nc_data, "lat")
library(rgdal)
shapefile = readOGR('/mnt/d/Downloads/Longhurst_world_v4_2010.shp')
grid = expand.grid(lon=lon, lat=lat)
coordinates(grid) = ~lon+lat
grid@proj4string = shapefile@proj4string
# for each grid point, find the province it belongs to (if available):
prov = over(grid, shapefile['ProvCode'])
# define dimensions for the new variable
londim = ncdim_def("lon", ncatt_get(nc_data, "lon")$units, lon)
latdim = ncdim_def("lat", ncatt_get(nc_data, "lat")$units, lat)
# define the new variable
region = ncvar_def("region", "", list(londim, latdim), prec="integer")
# add the new variable to the netCDF file
nc_data = ncvar_add(nc_data, region)
# write the variable's values to the file
ncvar_put(nc_data, region, prov$ProvCode)
nc_close(nc_data)

rgdal version etc.: rgdal 版本等:

Loading required package: sp
rgdal: version: 1.4-8, (SVN revision 845)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 3.0.4, released 2020/01/28
 Path to GDAL shared files:
 GDAL binary built with GEOS: TRUE
 Loaded PROJ.4 runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 630]
 Path to PROJ.4 shared files: (autodetected)
 Linking to sp version: 1.3-2
OGR data source with driver: ESRI Shapefile
Source: "/mnt/d/Downloads/Longhurst_world_v4_2010.shp", layer: "Longhurst_world_v4_2010"
with 54 features
It has 2 fields

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM