pyModis開發(一)convertmodis_gdal module模塊

pyModis開發(一)convertmodis_gdal module模塊

pyModis是一個專門針對MODIS數據的開源python開發包。該開發包主要實現MODIS數據的批量下載和投影轉換(正弦投影轉其他投影等)、拼接、裁切、數據抽取等處理功能。對於MODIS數據的處理,MRT和Gdal是兩個常用的工具和開發庫。pyModis結合了兩者,提供了多種處理方式。pyMODIS開發包庫包含:downmodis module 、parsemodis module 、convertmodis module、convertmodis_gdal module 、qualitymodis module、optparse_required module 和
optparse_gui module 七個主要模塊。

convertmodis_gdal module模塊

  利用python gdal庫來轉換MODISHDF文件。可以創建GeoTiff文件(或其他gdal支持的文件格式)或者多個帶的HDF拼接文件。模塊包括三個類和三個函數:
Classes:

  • file_info
  • createMosaicGDAL
  • convertModisGDAL

Functions:

  • getResampling()
  • raster_copy()
  • raster_copy_with_nodata()

convertModisGDAL類

 convertModisGDAL(hdfname, prefix, subset, res, outformat=“GTiff”,
epsg=None, wkt=None, resampl=‘NEAREST_NEIGHBOR’, vrt=False)
  利用GDAL庫將MODIS數據從hdf格式轉換成GDAL格式,可以實現hdf的投影轉換、子數據集(波段數據)抽取、格式轉換、重採樣等功能。

參數

  • hdfname (str) – 輸入數據的文件路徑列表,list。
  • prefix (str) – 輸出數據的前綴
  • subset (str) – 子數據集或者波段數據,可以是"(0 1 1)"類似的字符串(支持的字符串格式還包括:‘011’,'0,1,1’等等),也可以是0,1,1的列表,0表示不需要處理,1表示處理。如果設爲None,則表示對整個數據集裏所有子集進行處理。
  • res (int) – 輸出分辨率,如果設置爲0,則採用源數據的分辨率。
  • outformat (str) – 輸出格式字符串, GDAL支持的格式都可以,默認“GTiff“。
  • epsg (int) – 輸出文件投影的EPSG編碼。
  • wkt (str) – 輸出文件投影的WKT字符串。
  • resampl (str) – 重採樣類型,默認爲’NEAREST_NEIGHBOR’,支持的重採樣類型還包括:‘AVERAGE’, ‘BILINEAR’, ‘CUBIC’, ‘CUBIC_SPLINE’, ‘LANCZOS’,‘MODE’。
  • vrt (bool) – 輸入文件是否爲通過createMosaicGDAL創建的GDAL VRT 文件,True爲使用VRT文件,False爲不使用VRT。注意,使用vrt時,只處理第一個子數據集(波段數據)。當需要處理VRT中的多個波段或子數據集時,該操作無效。

主要函數

run(quiet=False)

  處理主函數,對已選圖層(波段)子集進行重投影變換。如果vrt爲True,程序會直接調用run_vrt_separated(),且只會處理vrt的第一個子數據集(波段數據)。

run_vrt_separated()

  針對VRT文件的處理函數,對createMosaicGDAL創建的VRT文件進行重投影變換,對應的createMosaicGDAL類的函數write_vrt的參數separated設置爲True。

用法示例

對MODIS數據進行多幅拼接

from pymodis import convertmodis_gdal

    mosaic_g = createMosaicGDAL(self.__hdfs, self.__subset)
    if mosaic_g is None:
        return False
    else:
        mosaic_name_list = []
        out_name_list = []
        for k in list(mosaic_g.file_infos.keys()):
            mosaic_names = "{pref}_{band}.vrt".format(pref=self.__mosaic_name, band=k)
            out_names = "{pref}_{band}".format(pref=self.__outname, band=k)
            mosaic_name_list.append(mosaic_names)
            out_name_list.append(out_names)
        if mosaic_g.write_vrt(self.__mosaic_name):
            self.__mosaic_files =  mosaic_name_list
            self.__out_names = out_name_list
            return True
        else:
            return False

createMosaicGDAL類

 createMosaicGDAL(hdfnames, subset, outformat=‘HDF4Image’)
  利用GDAL拼接MODIS數據從hdf格式到GDAL支持的文件格式。

參數:

  • hdfnames (list) – 待拼接的文件路徑列表
  • subset (str) – 待拼接的子集或波段數據,可以是"(0 1 1)"類似的字符串(支持的字符串格式還包括:‘011’,'0,1,1’等等),也可以是0,1,1的列表,0表示不需要處理,1表示處理。如果設爲None,則表示對整個數據集裏所有子集進行處理。
  • outformat (str) – 輸出格式字符串,支持的格式包括:HDF4Image、GTiff、HFA,不支持VRT文件輸出。

主要函數

run(output, quiet=False)

  處理主函數,創建拼接處理過程。

參數

output (st) - 輸出文件的名稱。

write_mosaic_xml(prefix)

  輸出MODIS拼接的XML元數據文件。

參數

  prefix(str) - 包含元數據的XML文件的名稱前綴

write_vrt(output, separate=True, quiet=False)

  輸出VRT文件

參數
  • output(str) - 輸出文件的前綴
  • separate(bool) - 是否爲各個波段各自輸出一個VRT文件,True表示爲每個波段輸出一個VRT文件,False表示輸出到一個文件。

file_info類

  GDAL文件信息類,該類拷貝自gdal_merge.py

參數

filename(str) - 待讀取的文件名稱列表

返回值

   1表示成功,0表示文件無法打開

主要函數

copy_into(t_fh, s_band=1, t_band=1, nodata_arg=None)

  將此文件映像複製到目標文件中。
這個方法將計算file_info對象文件和目標gdal的重疊區域。對象,並複製用於公共窗口區域的圖像數據。假設這些文件在一個相同的投影中。不作檢查或糾正。但是,如果目標文件的分辨率不同,或者圖像像素類型不同,則會執行適當的重採樣和轉換(使用常規的GDAL提升/降級規則)。

參數
  • t_fh – gdal.Dataset object for the file into which some or all of this file may be copied.
  • s_band –
  • t_band –
  • nodata_arg –
返回值

  1代表成功(或者不需要拷貝),0表示失敗。

init_from_name(filename)

  初始化

getResampling(res)函數

getResampling(res)
Return the GDAL resampling method

Parameters: res (str) – the string of resampling method

raster_copy函數

raster_copy(s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, nodata=None)[source]¶
Copy a band of raster into the output file.

Function copied from gdal_merge.py
raster_copy_with_nodata函數
raster_copy_with_nodata(s_fh, s_xoff, s_yoff, s_xsize, s_ysize, s_band_n, t_fh, t_xoff, t_yoff, t_xsize, t_ysize, t_band_n, nodata)[source]
Copy a band of raster into the output file with nodata values.

Function copied from gdal_merge.py

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章