配置文件和写入文件

打开文件时如何使用配置文件。

就像 Python 的内置 open() 函数, rasterio.open() 有两个主要参数:路径(或url)和可选模式 ('r''w''r+''w+' )此外,还有许多关键字参数,创建新数据集时需要其中几个参数:

  • 驱动

  • 宽度、高度

  • 计数

  • D型

  • crs

  • 转型

这些相同的参数出现在数据集中 profile 属性。利用概要文件和数据集打开关键字参数之间的对称性是很好的栅格用法。

with rasterio.open('first.jp2') as src_dataset:

    # Get a copy of the source dataset's profile. Thus our
    # destination dataset will have the same dimensions,
    # number of bands, data type, and georeferencing as the
    # source dataset.
    kwds = src_dataset.profile

    # Change the format driver for the destination dataset to
    # 'GTiff', short for GeoTIFF.
    kwds['driver'] = 'GTiff'

    # Add GeoTIFF-specific keyword arguments.
    kwds['tiled'] = True
    kwds['blockxsize'] = 256
    kwds['blockysize'] = 256
    kwds['photometric'] = 'YCbCr'
    kwds['compress'] = 'JPEG'

    with rasterio.open('second.tif', 'w', **kwds) as dst_dataset:
        # Write data to the destination dataset.

这个 rasterio.profiles 模块包含在应用程序中可能有用的命名配置文件示例:

class DefaultGTiffProfile(Profile):
    """Tiled, band-interleaved, LZW-compressed, 8-bit GTiff."""

    defaults = {
        'driver': 'GTiff',
        'interleave': 'band',
        'tiled': True,
        'blockxsize': 256,
        'blockysize': 256,
        'compress': 'lzw',
        'nodata': 0,
        'dtype': uint8
    }

它可以用来创建新的数据集。注意,它不计算乐队,而且 count 创建配置文件时需要传递关键字参数。

from rasterio.profiles import DefaultGTiffProfile

with rasterio.open(
        'output.tif', 'w', **DefaultGTiffProfile(count=3)) as dst_dataset:
    # Write data to the destination dataset.