如何使用 Rasterio 从多数据集 MODIS 图像中打开特定数据集?
我在 GitHub 上发布了一些示例数据:https ://github.com/SteveObert/rasterIO_question/tree/master/data
如果我打开一个只有一个波段的 MODIS HDF 文件,下面的代码会按照我想要的方式工作:
import rasterio
rasterfileMulti = 'MOD10A1_multiband_HEGOUT.hdf'
rasterfileSingle = 'MOD10A1_singleband_HEGOUT.hdf'
shapeFile = 'SFP_drainage.shp'
# Read an HDF into an array
dataset = rasterio.open('rasterfileSingle')
band1 = dataset.read(1)
print(band1)
输出:
array([[ 53, 53, 250, ..., 255, 255, 255],
[ 56, 56, 56, ..., 255, 255, 255],
[ 56, 56, 49, ..., 255, 255, 255],
...,
[ 78, 78, 78, ..., 53, 50, 50],
[ 72, 78, 78, ..., 57, 57, 57],
[ 72, 72, 72, ..., 61, 61, 61]], dtype=uint8)
但是,如果我尝试打开包含多个数据集的 MODIS HDF 文件,则会收到错误“Rasterio IndexError: band index out of range”,如下所示。
rasterfileMulti = MOD10A1_multiband_HEGOUT.hdf
dataset2 = rasterio.open('rasterfileMulti')
band1 = dataset.read(1)
print(band1)
上面代码中的错误如下所示:
/Users/steve/anaconda3/lib/python3.6/site-packages/rasterio/__init__.py:193: UserWarning: Dataset has no geotransform set. Default transform will be applied (Affine.identity())
s.start()
Traceback (most recent call last):
File "<ipython-input-9-584312f89d76>", line 3, in <module>
band1 = dataset.read(1)
File "rasterio/_io.pyx", line 720, in rasterio._io.RasterReader.read
IndexError: band index out of range
最终,我想将栅格剪辑到 shapefile。只要 Modis 图像只有一个波段,在本例中为“NDSI_Snow_Cover”,下面的代码就会按照我想要的方式工作。
import fiona
import rasterio
rasterfileSingle = MOD10A1_singleband_HEGOUT.hdf
shapeFile = SFP_drainage.shp
with fiona.open(shapeFile, 'r') as shapefile:
features = [feature['geometry'] for feature in shapefile]
with rasterio.open(rasterfileMulti) as src:
out_image, out_transform = rasterio.mask.mask(src, features,
crop=True)
out_meta = src.meta.copy()
out_meta.update({'driver': 'GTiff',
'height': out_image.shape[1],
'width': out_image.shape[2],
'transform': out_transform})
with rasterio.open('/Users/steve/Documents/classes/Geos_505/project_Payette/working/data_files/test_clip_out.tif', 'w', **out_meta) as dest:
dest.write(out_image)