无法使用光栅创建新的tiff文件?

2024-09-19 23:28:21 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我尝试用rasterio和numpy创建一个新的tiff文件,这是我的代码

#import rasterio
import rasterio
#import show from rasterio.plot
from rasterio.plot import show
#import numpy
import numpy
from affine import Affine

#read the tif file
dataset = rasterio.open('montreal_30m.tif')
#obtain information of dataset
dataset.transform
band = dataset.read(1)
rows = []
for i in range (0,len(band),3):
    cols = []
    for j in range (0,len(band[0]),3):
        cols.append(band[i,j])
    rows.append(cols)
newband = numpy.array(rows, dtype = numpy.float32)

xcellsize = dataset.transform[0]*3
ycellsize = dataset.transform[4]*3

newtransform = Affine(xcellsize, 0.0, -73.629102716,0.0, ycellsize, 45.548661929)

with rasterio.open(
    'montreal_90m.tif',
    'w', 
    driver = 'GTiff', 
    height = len(newband), 
    width = len(newband[0]), 
    count = 1, 
    dtype = numpy.float32, 
    crs = dataset.crs, 
    transform = newtransform
) as newraster:
    newraster.write(newband, 1)

我收到了这个错误信息

CPLE_OpenFailedError                      Traceback (most recent call last)
<ipython-input-69-aa86b394c59e> in <module>
     26     dtype = numpy.float32,
     27     crs = dataset.crs,
---> 28     transform = newtransform
     29 ) as newraster:
     30     newraster.write(newband, 1)

/opt/conda/envs/python/lib/python3.7/site-packages/rasterio/env.py in wrapper(*args, **kwds)
    421 
    422         with env_ctor(session=session):
--> 423             return f(*args, **kwds)
    424 
    425     return wrapper

/opt/conda/envs/python/lib/python3.7/site-packages/rasterio/__init__.py in open(fp, mode, driver, width, height, count, crs, transform, dtype, nodata, sharing, **kwargs)
    223                                               transform=transform,
    224                                               dtype=dtype, nodata=nodata,
--> 225                                               **kwargs)
    226         else:
    227             raise ValueError(

rasterio/_io.pyx in rasterio._io.DatasetWriterBase.__init__()

rasterio/_err.pyx in rasterio._err.exc_wrap_pointer()

CPLE_OpenFailedError: Attempt to create new tiff file 'montreal_90m.tif' failed: No such file or directory

看起来可能有什么东西与转换,但我不确定这可能是什么,因为一切看起来都很好,我。它还说没有这样的文件或目录,所以可能有一个目录问题?我不确定这怎么可能,因为我可以使用rasterio fine打开原始tiff文件。老实说,在这一点上我只是感到困惑


Tags: 文件inimportnumpybandlentransformdataset