首页 > 解决方案 > 用 shapefile 屏蔽 nc 文件并将其保存为 nc 格式

问题描述

所以我有 netCDF 文件,想用形状文件掩盖它。我在下面的代码可以完成这项工作,但结果是 GTiff 格式。关于如何修改代码以将其保存为 netCDF 格式的任何建议?

import fiona
import rasterio
import rasterio.mask
import netCDF4 as nc
import numpy as np
from rasterio.plot import show


with fiona.open('/shape_file/scandinavia_shp.shp', "r") as shapefile:
    shapes = [feature["geometry"] for feature in shapefile]
    
with rasterio.open('/input/data1.nc') as src:
    out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)
    out_meta = src.meta
    
out_meta.update({"driver": "GTiff",
                  "height": out_image.shape[1],
                  "width": out_image.shape[2],
                  "transform": out_transform})

with rasterio.open('/output/data2.tiff', "w", **out_meta) as dest:
    dest.write(out_image)

标签: pythonnetcdfshapefile

解决方案


推荐阅读