简体   繁体   English

在 Python 的 HDF5 文件中存储多个 GeoTiff

[英]Storing multiple GeoTiffs in HDF5 file in Python

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).我想将多个 GeoTiff 文件存储在一个 HDF5 文件中以用于进一步分析,因为我应该使用的 function 只能处理 HDF5(所以基本上就像 R 中的光栅堆栈,但存储在 HDF5 中)。 I have to use Python.我必须使用 Python。 I am relatively new to HDF5 format (and geoanalysis in Python generally) and don't really know how to approach this issue.我对 HDF5 格式相对较新(通常在 Python 中进行地理分析)并且真的不知道如何解决这个问题。 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.我很确定这根本不是正确的方法,我很高兴有任何建议。

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:只需使用 float64 类型创建一个 spec_dtype 变量,然后将其应用于 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.应用这些,希望它会起作用。

Using HDFql in Python, your use-case could be solved as follows:在 Python 中使用HDFql ,您的用例可以解决如下:

import HDFql

HDFql.execute("SHOW FILE SIZE filename.tif, filename2.tif")

HDFql.cursor_next()

HDFql.execute("CREATE DATASET path/test.h5 GeoTiff1 AS OPAQUE(%d) VALUES FROM BINARY FILE filename.tif" % HDFql.cursor_get_bigint())

HDFql.cursor_next()

HDFql.execute("CREATE DATASET path/test.h5 GeoTiff2 AS OPAQUE(%d) VALUES FROM BINARY FILE filename2.tif" % HDFql.cursor_get_bigint())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM