Is there a way to write a GDAL raster to a HDF4 or HDF5 dataset using GDAL? I have found many examples that show how to read HDF datasets, but none on how to write them.

  • HDF5 is an optional driver and is read only, HDF4 supports creation, but I'm not sure if the HDF4 driver is in a default install. GDAL can be installed from a number of sources, your distro may not have the driver which you will need to add manually. Have a read of gdal.org/drivers/raster/index.html which tabulates the capabilities of each driver. Theoretically if you use GDAL_Translate (or QGIS::Raster::Translate) and specify the output driver as HDF4 gdal.org/drivers/raster/hdf4.html#raster-hdf4 you should be able to convert your existing raster in another format to HDF4. – Michael Stimson Aug 12 '20 at 0:01
  • Thanks! I'm running GDAL 3.1.2 in python via the conda-forge distribution. When looking at my HDF4 driver metadata, I've noticed that the DCAP_CREATE and DCAP_CREATECOPY features are not enabled. Do you know if there is a way that I can install the creation-enabled version of the driver? – Forrest Williams Aug 12 '20 at 1:42
  • Is that Linux? Perhaps hdfeos.org/software/gdal.php would help. I'm on Windows and use the GISInternals download gisinternals.com/release.php which contains gdal_HDF4.dll gdal_HDF4Image.dll in the gdalplugins directory. – Michael Stimson Aug 12 '20 at 1:54
  • I'm on Windows 10. To be clear @Micheal Stimson, can you create a HDF4 file using your setup? Here is some example python code: import gdal driver = gdal.GetDriverByName('HDF4') dst_ds = driver.Create('test.hdf', xsize=512, ysize=512) – Forrest Williams Aug 12 '20 at 2:29

You're right, I get ERROR 6: GDALDriver::Create() ... no create method implemented for this format in both 32bit and 64bit GDAL for HDF4 but driver HDF4Image will create a HDF4 raster.

To test your installed drivers use GDALInfo --formats and you should see a full list of the available formats:

Supported Formats:
  HDF4 -raster- (ros): Hierarchical Data Format Release 4
  HDF4Image -raster- (rw+): HDF4 Dataset
  VRT -raster- (rw+v): Virtual Raster
  GTiff -raster- (rw+vs): GeoTIFF
  NITF -raster- (rw+vs): National Imagery Transmission Format
  RPFTOC -raster- (rovs): Raster Product Format TOC format
... and more to follow

The HDF4 driver is ros which means read only, the HDF4Image is rw+ which means read, write and create.

  • Using HDF4Image works, thank you. On a related note, do you know how to create a name subdataset for the HDF4Image file? – Forrest Williams Aug 12 '20 at 23:33
  • No I don't know how to create a subdataset, it's not a task that I've done before. The API gdal.org/python/osgeo.gdal.Dataset-class.html has GetSubDatasets() to get a reference to all the suddatasets in the raster but no SetSubDataset method. A subdataset is not a band as each can have differing extent, pixel type and cell size. Perhaps it's something simple like specifying the name in the form HDF4_SDS:subdataset_type:file_name:subdataset_index; if you have an existing HDF4 you could use GDALInfo to get the name strings and see if that works, if not please post a new question. – Michael Stimson Aug 17 '20 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.