正在写入数据集

以写模式打开文件比用python打开文本文件要复杂一些。必须指定栅格数据集的尺寸、数据类型和特定格式。

以下是基本栅格功能的示例。数组将写入新的单波段TIFF。

# Register GDAL format drivers and configuration options with a
# context manager.
with rasterio.Env():

    # Write an array as a raster band to a new 8-bit file. For
    # the new file's profile, we start with the profile of the source
    profile = src.profile

    # And then change the band count to 1, set the
    # dtype to uint8, and specify LZW compression.
    profile.update(
        dtype=rasterio.uint8,
        count=1,
        compress='lzw')

    with rasterio.open('example.tif', 'w', **profile) as dst:
        dst.write(array.astype(rasterio.uint8), 1)

# At the end of the ``with rasterio.Env()`` block, context
# manager exits and all drivers are de-registered.

写入数据的工作方式主要与Python文件相同。有一些特定格式的差异。

支持的驱动程序

GTiff 是唯一支持直接写入磁盘的驱动程序。geotiff使用 RasterUpdater 并充分利用 GDALCreate 功能。我们强烈建议使用geotiff驱动程序进行编写,因为它是经过最佳测试和支持的格式。

其他一些GDAL可写的格式也可以由Rasterio编写。这些使用一个 IndirectRasterUpdater 它不直接创建,而是使用临时内存数据集和 GDALCreateCopy 产生最终的输出。

已知某些格式使用 IndirectRasterUpdater . 这些格式将引发 RasterioIOError 如果你试图写信给。目前这适用于 netCDF 驱动程序,但请告诉我们,如果您在编写其他格式时遇到问题。