How to open HDF files on Sinusoidal Coordinate System in QGIS3 with PyQGIS - Tutorial

HDF_Reproject.PNG

For a normal GIS user, QGIS 3 brings a lot of new tools, new forms to perform spatial analysis but also it doesn´t bring (yet) some options available in QGIS 2. This is the case for the representation of HDF raster files that is not available in QGIS 3 but it is available in QGIS 2. Research have been performed to address this issue and many options were evaluated to open the HDF files and perform a geotransformation from Sinusoidal Coordinate System to Geographical Coordinate System.

The solution came from the powerful gdal library and some core Python functions. This tutorial show the procedure to open a layer of a MOD13A2, a MODIS data product for vegetation evaluation and reproject it to geographical coordinate system (lat/lon).

 

Python code

This is the Python code that open the HDF file, reproject one layer to WGS84 and bring it to canvas.

import gdal, os

## List input raster files
os.chdir('C:\\Users\\USER\\Documents\\Infohataris\\HDFReproyectionPyQGIS\\inputRaster')
rasterFiles = os.listdir(os.getcwd())
print(rasterFiles)

## Open HDF file
hdflayer = gdal.Open(rasterFiles[0], gdal.GA_ReadOnly)

# Open raster layer
rlayer = gdal.Open(hdflayer.GetSubDatasets()[0][0], gdal.GA_ReadOnly)

# Define output raster and warp-reproject
outputName = rlayer.GetMetadata_Dict()['LOCALGRANULEID'][:-4]+'_NDVI.tiff'
outputRaster = '..\\outputRaster\\'+ outputName
gdal.Warp(outputRaster,rlayer,dstSRS='EPSG:4326')

# Add output raster to canvas
iface.addRasterLayer(outputRaster, outputName)

 

Tutorial

 

Input files

You can download the input files for this tutorial here.

 

Links

Website of NADA Earthdata Search to download MOD13A2 files:

https://search.earthdata.nasa.gov

More information on the MOD13A2 vegetation data product:

https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mod13a2_v006

Saul Montoya

Saul Montoya es Ingeniero Civil graduado de la Pontificia Universidad Católica del Perú en Lima con estudios de postgrado en Manejo e Ingeniería de Recursos Hídricos (Programa WAREM) de la Universidad de Stuttgart con mención en Ingeniería de Aguas Subterráneas y Hidroinformática.

Smiley face

Subscribe to our free e-newsletter for tutorials, articles, webminars, courses and more.