import rasterio
src = rasterio.open("/data/gdata/geotiff_file.tif")
src.colorinterp[0]
<ColorInterp.red: 3>
import rasterio
src = rasterio.open("/data/gdata/geotiff_file.tif")
src.crs
CRS.from_wkt('PROJCS["Albers_Beijing54",GEOGCS["Unknown datum based upon the Krassowsky 1940 ellipsoid",DATUM["Not_specified_based_on_Krassowsky_1940_ellipsoid",SPHEROID["Krassowsky 1940",6378245,298.3,AUTHORITY["EPSG","7024"]],AUTHORITY["EPSG","6024"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["latitude_of_center",0],PARAMETER["longitude_of_center",105],PARAMETER["standard_parallel_1",25],PARAMETER["standard_parallel_2",47],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]')
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.
/opt/conda/lib/python3.12/site-packages/rasterio/__init__.py:366: NotGeoreferencedWarning: Dataset has no geotransform, gcps, or rpcs. The identity matrix will be returned. dataset = writer(
src.transform
Affine(30.0, 0.0, 1868454.913, 0.0, -30.0, 5353126.266)
这个 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 |
import shutil
path = shutil.copy("/data/gdata/geotiff_file.tif", '/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()
我们可以读取更新后的数据集并确认存在概述
src = rasterio.open(path, 'r')
[src.overviews(i) for i in src.indexes]
[[2, 4, 8, 16], [2, 4, 8, 16], [2, 4, 8, 16]]
src.tags(ns='rio_overview').get('resampling')
'average'
src.read().shape
(3, 900, 1500)
src.read(out_shape=(3, int(src.height / 4), int(src.width / 4))).shape
(3, 225, 375)