使用rasterio读取文件时设置CRS。

5

我正在使用Rasterio在Python中读取一个jpg图像及其相关的world文件,代码如下:

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

图像文件及其相关的世界文件已被正确读取,然而CRS未定义(我猜这是因为世界文件不包含CRS)。以下是输出结果:

5000 5000
None
(1, 2, 3)

如何在Rasterio中手动设置CRS?
1个回答

10

若没有查看世界文件,我不能确定细节是否正确,但是在读取带有世界文件的文件后,我已经使用以下方法来添加转换和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

4
这对我来说会抛出错误:"DatasetAttributeError:只读属性"。 - Joseph Redfern
2
@JosephRedfern 更新具有读/写模式的版本。我认为原始版本在旧版rasterio中运行正常。 - jdmcbr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接