0

I want to store multiple GeoTiff files in one HDF5 file to use it for further analysis since the function I am supposed to use can just deal with HDF5 (so basically like a raster stack in R but stored in a HDF5). I have to use Python. I am relatively new to HDF5 format (and geoanalysis in Python generally) and don't really know how to approach this issue. Especially keeping the geolocation/projection inforation seems tricky to me. So far I tried:

import h5py
import rasterio

r1 = rasterio.open("filename.tif")
r2 = rasterio.open("filename2.tif")

with h5py.File('path/test.h5', 'w') as hdf:
    hdf.create_dataset('GeoTiff1', data=r1)
    hdf.create_dataset('GeoTiff2', data=r2)

Yielding the following errror:

TypeError: Object dtype dtype('O') has no native HDF5 equivalent

I am pretty sure this not at all the correct approach and I'm happy about any suggestions.

3 Answers 3

0

What you can try is to do this:

import numpy as np
spec_dtype = h5py.special_dtype(vlen=np.dtype('float64'))

Just make a spec_dtype variable with float64 type then apply this to create_dataset:

with h5py.File('path/test.h5', 'w') as hdf:
    hdf.create_dataset('GeoTiff1', data=r1,, dtype=spec_dtype)
    hdf.create_dataset('GeoTiff2', data=r2,, dtype=spec_dtype)

Apply these and hopefully it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks for the suggestion. When I implement that I get the follow error: AttributeError: 'DatasetReader' object has no attribute 'dtype' Which I think has something to do with the rasterio.open(). Any suggestions?
0

Using HDFql in Python, your use-case could be solved as follows:

import HDFql

HDFql.execute("CREATE DATASET path/test.h5 GeoTiff1 VALUES FROM BINARY FILE filename.tif")

HDFql.execute("CREATE DATASET path/test.h5 GeoTiff2 VALUES FROM BINARY FILE filename2.tif")

Comments

0

Quite old question, but the issue is that you're trying to save the file reference and not the actual data.

import h5py
import rasterio

with rasterio.open("filename.tif") as f:
    r1 = f.read()
with rasterio.open("filename2.tif") as f:
    r2 = f.read()

with h5py.File('path/test.h5', 'w') as hdf:
    hdf.create_dataset('GeoTiff1', data=r1)
    hdf.create_dataset('GeoTiff2', data=r2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.