>>> from env_helper import info; info()
页面更新时间: 2023-06-23 10:03:37
运行环境:
    Linux发行版本: Debian GNU/Linux 12 (bookworm)
    操作系统内核: Linux-6.1.0-9-amd64-x86_64-with-glibc2.36
    Python版本: 3.11.2

3.5. RasterIO的其他技术细节

3.5.1. 读取栅格带的颜色解释

可以从数据集中读取栅格带的颜色解释。

>>> import rasterio
>>> src = rasterio.open("/gdata/rasterio/RGB.byte.tiff")
>>> src.colorinterp[0]
/usr/lib/python3/dist-packages/rasterio/__init__.py:304: NotGeoreferencedWarning: Dataset has no geotransform, gcps, or rpcs. The identity matrix will be returned.
  dataset = DatasetReader(path, driver=driver, sharing=sharing, **kwargs)
<ColorInterp.gray: 1>

3.5.2. 影像配准

栅格数据集的地理参考有两个部分:栅格像素所在的本地、区域或全局系统的定义;以及像素坐标转换为该系统中坐标的参数。

3.5.3. 坐标参考系

数据集的坐标参考系从其 crs 属性。

>>> import rasterio
>>> src = rasterio.open('/gdata/rasterio/RGB.byte.tiff')
>>> src.crs

Rasterio遵循pyproj,并使用dict格式的proj.4语法作为其本机CRS语法。如果您需要CRS的WKT表示,请参见CRS类的 wkt 属性。

如:

src.crs.wkt

打开新文件进行写入时,还可以使用CRS字符串作为参数。

>>> profile = {'driver': 'GTiff', 'height': 100, 'width': 100, 'count': 1, 'dtype': rasterio.uint8}
>>> with rasterio.open('/tmp/foo.tif', 'w', crs='EPSG:3857', **profile) as dst:
>>>      pass # write data to this Web Mercator projection dataset.
/usr/lib/python3/dist-packages/rasterio/__init__.py:314: NotGeoreferencedWarning: Dataset has no geotransform, gcps, or rpcs. The identity matrix will be returned.
  dataset = writer(

3.5.4. 坐标参考系坐标变换

数据集的像素坐标系的原点在“左上角”(想象它显示在屏幕上)。列索引向右增加,行索引向下增加。在数据集的参考系统中,这些坐标到“世界”坐标的映射是用仿射变换矩阵完成的。

>>> src.transform
Affine(1.0, 0.0, 0.0,
       0.0, 1.0, 0.0)

这个 Affine 对象是具有元素的命名元组 a, b, c, d, e, f 对应于下面矩阵方程中的元素,其中像素的图像坐标为 x, y 它的世界坐标是 x’, y’ ::

x' | | a b c | | x |
y' | = | d e f | | y |
1 | | 0 0 1 | | 1 |

3.5.5. 鸟瞰图

鸟瞰图是数据集的低分辨率版本,可以在不需要完全分辨率时加快渲染速度。通过预计算上采样的像素,缩小后的渲染速度会明显加快。

根据文件格式,可以在内部或外部存储概述。

在某些情况下,我们可能希望复制测试数据以避免更改原始数据。

>>> import shutil
>>> path = shutil.copy('/gdata/rasterio/RGB.byte.tiff', '/tmp/RGB.byte--2.tiff')

我们必须指定要为其构建概述的缩放因子。通常这些是2的指数

>>> factors = [2, 4, 8, 16]

为了控制视图的视觉质量,可以使用“最近”、“立方”、“平均”、“模式”和“高斯”重采样算法。这些可以通过 Resampling 枚举

>>> from rasterio.enums import Resampling

创建概述需要在中打开数据集 r+ 模式,这使我们能够在适当的位置更新数据。按照惯例,我们也在 rio_overview 命名空间,以便读者可以确定使用了什么重新采样方法。

>>> import rasterio
>>> dst = rasterio.open(path, 'r+')
>>> dst.build_overviews(factors, Resampling.average)
>>> dst.update_tags(ns='rio_overview', resampling='average')
>>> dst.close()
/usr/lib/python3/dist-packages/rasterio/__init__.py:306: NotGeoreferencedWarning: Dataset has no geotransform, gcps, or rpcs. The identity matrix will be returned.
  dataset = get_writer_for_path(path, driver=driver)(

我们可以读取更新后的数据集并确认存在概述

>>> src = rasterio.open(path, 'r')
>>> [src.overviews(i) for i in src.indexes]
[[2, 4, 8, 16]]
>>> src.tags(ns='rio_overview').get('resampling')
'average'
>>> src.read().shape
(1, 300, 500)
>>> src.read(out_shape=(3, int(src.height / 4), int(src.width / 4))).shape
---------------------------------------------------------------------------

DatasetIOShapeError                       Traceback (most recent call last)

Cell In [13], line 1
----> 1 src.read(out_shape=(3, int(src.height / 4), int(src.width / 4))).shape


File rasterio/_io.pyx:612, in rasterio._io.DatasetReaderBase.read()


File rasterio/_io.pyx:936, in rasterio._io.DatasetReaderBase._read()


File rasterio/_io.pyx:161, in rasterio._io.io_multi_band()


DatasetIOShapeError: Dataset indexes and destination buffer are mismatched