Load and save images to HDF5 files
Project description
h5image
This package provides standard functions for interacting with HDF5 files as part of the CriticalMAAS project.
This package will create HDF5 files with the following structure:
- group : all data associated with a single map
json
: the raw json file used to create the map and layersmap
: the actual map imagedata
: the actual bytes of the imagecorners
: the corners of the map where layers have datapatches
: for each layer (key) a list of patches as a tuple (x, y)layers_patch
: for each patch (key is x_y) a list of layersvalid_patche
: a list of locations across all layers as a tuple (x, y)
<layer>
: a layer of the map, the name of the layer is the name of filedata
: the actual layer datapatches
: a list of patches as a tuple (x, y) for this specific layer
Installation
Install using pip:
pip install h5image
To convert a folder of images to a HDF5 file use the h5create
program.
Quickstart example
The following code will create a new HDF5 file with a map and a layer, find the patch with most layers, and load the map and the layers at that patch.
import matplotlib.pyplot as plt
import numpy as np
import rasterio
from h5image import H5Image
# create map file
map = "CA_Sage"
h5i = H5Image(f"{map}.hdf5", "w")
h5i.add_image(f"{map}.json", folder="./data")
# get biggest patch
patches = h5i.get_patches(map, True)
sorted_patches = {k: v for k, v in sorted(patches.items(), key=lambda item: len(item[1]), reverse=True)}
loc, loc_layers = next(iter(sorted_patches.items()))
row = int(loc.split("_")[0])
col = int(loc.split("_")[1])
print(f"Biggest number of layers ({len(loc_layers)}) for {map} is at ( {row}, {col})")
# plot map
rgb = h5i.get_patch(row, col, map)
plt.imshow(rgb)
plt.title('map')
plt.axis('off')
plt.show()
# plot layers and legend
for layer in loc_layers:
rgb = h5i.get_patch(row, col, map, layer=layer)
plt.imshow(rgb, cmap=plt.colormaps['plasma'])
plt.title(layer)
plt.axis('off')
plt.show()
rgb = h5i.get_legend(map, layer)
plt.imshow(rgb)
plt.title("legend")
plt.axis('off')
plt.show()
# saving image as geotiff
# can also use h5i.save_image(map, 'map')
dset = h5i.get_map(map)
if dset.ndim == 3:
image = dset[...].transpose(2, 0, 1) # rasterio expects bands first
else:
image = np.array(dset[...], ndmin=3)
rasterio.open(f"{map}.tif", 'w', driver='GTiff', compress='lzw',
height=image.shape[1], width=image.shape[2], count=image.shape[0], dtype=image.dtype,
crs=h5i.get_crs(map, layer), transform=h5i.get_transform(map, layer)).write(image)
# close file
h5i.close()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
h5image-0.4.1.tar.gz
(8.6 kB
view details)
Built Distribution
File details
Details for the file h5image-0.4.1.tar.gz
.
File metadata
- Download URL: h5image-0.4.1.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f997b4e7767204dbdb4f02270d28f01a90e1c97699871b1c9fa88cf23fa7be8d |
|
MD5 | baa237d5e188d10c16350da9bca55125 |
|
BLAKE2b-256 | 09e2f7c1c51c93af1fc832091db0c31d7a8c63ae03a93fb735f4a59b0380b13e |
File details
Details for the file h5image-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: h5image-0.4.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3eb67d04c2c1ad231d607ef5a5ba5985cf89ffbe7ad5f3964486fe248cdc7dd |
|
MD5 | 7d185738d9a6e3b035b91e81f1bd5972 |
|
BLAKE2b-256 | 8f4923b2645444016a30e818650933a54f8ff48849c7d088bb22a3e766859f5f |