gdal
在读取tif的时候,实际上行列读取顺序是和数组相反的,所以赋值的时候一定是行列互换,否则读取的时候就会越界
###分块读取数据的生成器, gdal 行和列与数组相反!!!!!!!!
def readTifAsBlockGenerator(im,n_band,row,col,input_size,width,height):
for i in range(row):
for j in range(col):
if j != col - 1 and i != row - 1:
xoff = int(0.5 * j * input_size)
yoff = int(0.5 * i * input_size)
# print(str(im))
im1 = mytest.readTifAsBlock(im, n_band=n_band, xoff=xoff, yoff=yoff,xsize=input_size,ysize=input_size)
yield im1
elif j != col - 1 and i == row - 1:
##########gdal不支持负号的index
xoff = int(0.5 * j * input_size)
yoff = height-input_size
im1 = mytest.readTifAsBlock(im, n_band=n_band, xoff=xoff, yoff=yoff, xsize=input_size, ysize=input_size)
yield im1
# X_tmp[tmp_num] = x[-input_size:,int(0.5 * j * input_size):int(0.5 * (j + 2) * input_size), :]
# Y_tmp[tmp_num] = y[-input_size:,int(0.5 * j * input_size):int(0.5 * (j + 2) * input_size), :]
elif j == col - 1 and i != row - 1:
##########gdal不支持负号的index
xoff = width-input_size
yoff = int(0.5 * i * input_size)
im1 = mytest.readTifAsBlock(im, n_band=n_band, xoff=xoff, yoff=yoff, xsize=input_size, ysize=input_size)
yield im1
# X_tmp[tmp_num] = x[int(0.5 * i * input_size):int(0.5 * (i + 2) * input_size),-input_size:, :]
# Y_tmp[tmp_num] = y[int(0.5 * i * input_size):int(0.5 * (i + 2) * input_size),-input_size:, :]
else:
##########gdal不支持负号的index
xoff = width-input_size
yoff = height - input_size
im1 = mytest.readTifAsBlock(im, n_band=n_band, xoff=xoff, yoff=yoff, xsize=input_size, ysize=input_size)
yield im1
例如,要读取从图1中所示的第6000行和第1400列开始的六列和三行,你可以执行以下操作:
data = band.ReadAsArray(1400, 6000, 6, 3)