我正在使用gdal
逐行读取jp2
,下面是代码:
def open(self):
if self.ds is None:
self.ds = gdal.Open(self.file_path, gdal.GA_ReadOnly)
self.geo_transform = self.ds.GetGeoTransform()
self.rows = self.ds.RasterYSize
self.cols = self.ds.RasterXSize
def read_strip(self, y_start, read_y_size):
"""
y_start : y in projection coordinate
"""
self.open()
if not self.ds:
raise IOError("Could not open '%s'" % self.file_path)
y_off = int((y_start - self.geo_transform[3]) / self.geo_transform[5])
if y_off < 0 or y_off >= self.rows:
return None
else:
read_y_size_in_data = min(self.rows - y_off, read_y_size)
try:
read_data = self.ds.ReadAsArray(0, y_off, ysize=read_y_size_in_data)
band_data = read_data.astype(float)
self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
return band_data
except Exception:
self.logger.exception('this file is Nonetype, file: {}'.format(self.file_path))
self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
self.close()
这是错误消息:
Traceback (most recent call last): File "scene_reader.py", line 62, in read_strip band_data = read_data.astype(float), 'NoneType' object has no attribute 'astype'
每次运行代码时,我都没有得到任何IOError,这意味着jp2文件已成功打开,而ReadAsArray()出现了“ Nonetype”错误。顺便说一句,我已经尝试过仅使用gdal.Open()和ReadAsArray()在ipython中通过几行读取同一文件,一切正常,因此,我敢肯定jp2文件本身没有问题。 所以,有人可以帮我吗?
答案 0 :(得分:0)
我认为您必须选择一个频段,即使它是单个频段数据集也是如此。 ds.GetRasterBand(1).ReadAsArray()
这样。我总是参考这个GDAL API Tutorial,其中包含简单的示例。