为使用光栅读取的文件设置 CRS

Kar*_*Kid 4 python gis rasterio

我正在使用 Rasterio 在 Python 中读取 jpg 图像及其关联的世界文件,如下所示:

import rasterio
with rasterio.open('/path/to/file.jpg') as src:
    print(src.width, src.height)
    print(src.crs)
    print(src.indexes)
Run Code Online (Sandbox Code Playgroud)

图像文件及其关联的世界文件被正确读取,但是 CRS 未定义(我猜这是因为世界文件不包含 CRS)。这是输出:

5000 5000
None
(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

读取文件后如何在Rasterio中手动设置CRS?

jdm*_*cbr 8

在没有看到世界文件的情况下,我不确定这是否详细正确,但是在读取带有世界文件的文件后,我使用以下内容将变换和 CRS 添加到栅格:

from affine import Affine
import rasterio.crs

a, d, b, e, c, f = np.loadtxt(world_filename)    # order depends on convention
transform = Affine(a, b, c, d, e, f)
crs = rasterio.crs.CRS({"init": "epsg:4326"})    # or whatever CRS you know the image is in    
with rasterio.open('/path/to/file.jpg', mode='r+') as src:
    src.transform = transform
    src.crs = crs
Run Code Online (Sandbox Code Playgroud)

  • 这会引发错误:“DatasetAttributeError:只读属性”。 (3认同)
  • @JosephRedfern 更新了读/写模式。我认为原始版本适用于较旧的光栅版本。 (2认同)