Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Convert hdf to geotiff
- As arcgis is unable to retrieve the geotransform
- parameters, this script is used in the
- QGis environement to convert hdf raster into geotiff
- """
- import os
- from osgeo import gdal
- import numpy as np
- # directories
- out_dir = 'C:/Users/dadelforge/Desktop/hdf_conversion/raster2016_tif'
- hdf_dir = 'C:/Users/dadelforge/Desktop/hdf_conversion/raster2016_hdf'
- # hdf files paths
- hdf_files = [hdf_dir + '/' + fname for fname in os.listdir(hdf_dir) if (fname.endswith('.hdf') and ( not fname.startswith('._')))]
- # iterate hdf files
- for hdf_path in hdf_files:
- # get filename
- hdf_name = os.path.basename(hdf_path)
- # change extension
- tif_name = hdf_name[:-4] + '.tif'
- # create output file path
- tif_path = '{}/{}'.format(out_dir, tif_name)
- # convert hdf to tif
- out_ds = gdal.Translate(tif_path, hdf_path)
- # get band
- band = out_ds.GetRasterBand(1)
- # get data
- data = band.ReadAsArray()
- # set nodata value
- ndv = -9999
- data[data < 0] = 0 # assign 0 to negative value
- data[data != data] = ndv # assign ndv to np.nan
- # overwrite data
- band.WriteArray(data)
- band.SetNoDataValue(ndv)
- out_ds = None
- band = None
- print 'Done'
Add Comment
Please, Sign In to add comment