Guest User

Untitled

a guest
Nov 25th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. """Convert hdf to geotiff
  2.  
  3. As arcgis is unable to retrieve the geotransform
  4. parameters, this script is used in the
  5. QGis environement to convert hdf raster into geotiff
  6. """
  7. import os
  8. from osgeo import gdal
  9. import numpy as np
  10.  
  11. # directories
  12.  
  13. out_dir = 'C:/Users/dadelforge/Desktop/hdf_conversion/raster2016_tif'
  14. hdf_dir = 'C:/Users/dadelforge/Desktop/hdf_conversion/raster2016_hdf'
  15.  
  16. # hdf files paths
  17. hdf_files = [hdf_dir + '/' + fname for fname in os.listdir(hdf_dir) if (fname.endswith('.hdf') and ( not fname.startswith('._')))]
  18.  
  19. # iterate hdf files
  20.  
  21. for hdf_path in hdf_files:
  22.  
  23. # get filename
  24.  
  25. hdf_name = os.path.basename(hdf_path)
  26.  
  27. # change extension
  28.  
  29. tif_name = hdf_name[:-4] + '.tif'
  30.  
  31. # create output file path
  32.  
  33. tif_path = '{}/{}'.format(out_dir, tif_name)
  34.  
  35. # convert hdf to tif
  36.  
  37. out_ds = gdal.Translate(tif_path, hdf_path)
  38.  
  39. # get band
  40.  
  41. band = out_ds.GetRasterBand(1)
  42.  
  43. # get data
  44.  
  45. data = band.ReadAsArray()
  46.  
  47. # set nodata value
  48.  
  49. ndv = -9999
  50. data[data < 0] = 0 # assign 0 to negative value
  51. data[data != data] = ndv # assign ndv to np.nan
  52.  
  53. # overwrite data
  54.  
  55. band.WriteArray(data)
  56. band.SetNoDataValue(ndv)
  57.  
  58. out_ds = None
  59. band = None
  60.  
  61. print 'Done'
Add Comment
Please, Sign In to add comment