簡體   English   中英

如何使用Rasterio從多數據集MODIS圖像中打開特定的數據集(波段)? Rasterio IndexError:波段索引超出范圍

[英]How can I open a specific dataset (band) from a multi dataset MODIS image using Rasterio? Rasterio IndexError: band index out of range

如何使用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:波段索引超出范圍”,如下所示。

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)

首先,我們需要知道子數據集的名稱。 gdalinfo給出以下7個子數據集:

Subdatasets:
  SUBDATASET_1_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:NDSI_Snow_Cover
  SUBDATASET_1_DESC=[350x831] NDSI_Snow_Cover MOD_Grid_Snow_500m_16 (8-bit unsigned integer)           
  SUBDATASET_2_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:NDSI_Snow_Cover_Basic_QA
  SUBDATASET_2_DESC=[350x831] NDSI_Snow_Cover_Basic_QA MOD_Grid_Snow_500m_16 (8-bit unsigned integer)
  SUBDATASET_3_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:NDSI_Snow_Cover_Algorithm_Flags_QA
  SUBDATASET_3_DESC=[350x831] NDSI_Snow_Cover_Algorithm_Flags_QA MOD_Grid_Snow_500m_16 (8-bit unsigned integer)
  SUBDATASET_4_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:NDSI
  SUBDATASET_4_DESC=[350x831] NDSI MOD_Grid_Snow_500m_16 (16-bit integer)
  SUBDATASET_5_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:Snow_Albedo_Daily_Tile
  SUBDATASET_5_DESC=[350x831] Snow_Albedo_Daily_Tile MOD_Grid_Snow_500m_16 (8-bit unsigned integer)
  SUBDATASET_6_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:orbit_pnt
  SUBDATASET_6_DESC=[350x831] orbit_pnt MOD_Grid_Snow_500m_16 (8-bit integer)
  SUBDATASET_7_NAME=HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:granule_pnt
  SUBDATASET_7_DESC=[350x831] granule_pnt MOD_Grid_Snow_500m_16 (8-bit unsigned integer)

現在,您可以打開將子數據集的GDAL完全限定名稱作為參數傳遞的任何子集。 例如:

rasterio.open('HDF4_EOS:EOS_GRID:"MOD10A1_multiband_HEGOUT.hdf":MOD_Grid_Snow_500m_16:NDSI_Snow_Cover')

編輯:我根據作者的建議添加了外部單引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM