160 lines
3.4 KiB
Plaintext
160 lines
3.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# GDAL vs Matplotlib\n",
|
|
"\n",
|
|
"We have in data fusion contest dataset TIFF file with 32 bit per sample. For now TRISKELE only work with less than 17 bps: `BOOST_ASSERT (bits < 17);`. I want to ensure that we can oppen such data with Python.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import gdal\n",
|
|
"from pathlib import Path\n",
|
|
"import subprocess"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"file = Path('../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"info = subprocess.Popen(['tiffinfo', file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
|
|
"print(info.communicate()[0].decode())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mat_data = plt.imread(file)\n",
|
|
"mat_data.shape, mat_data.dtype"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"gdl_data = gdal.Open(str(file))\n",
|
|
"gdl_data.GetMetadata(), gdl_data.RasterCount"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"gdl_data.GetRasterBand(1).ReadAsArray().shape, gdl_data.GetRasterBand(1).ReadAsArray().dtype"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## There.\n",
|
|
"\n",
|
|
"`matplotlib` is derping around with bps. \n",
|
|
"\n",
|
|
"Maybe each byte is split `[a, b, c, d]` as $V = a 2^{24} + b 2^{16} + c 2^8 + d$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"(np.power(2, (np.arange(4)[::-1] * 8)) * mat_data).sum(axis=2).shape"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"raster = gdl_data.GetRasterBand(1).ReadAsArray()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.figure(figsize=(32,9))\n",
|
|
"plt.imshow(raster)\n",
|
|
"plt.colorbar()\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.hist(raster.reshape(-1), 100, log=True)\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"plt.figure(figsize=(32,9))\n",
|
|
"plt.imshow(raster * (raster < .25 * 1e38))\n",
|
|
"plt.colorbar()\n",
|
|
"plt.show()\n",
|
|
"plt.hist(raster.reshape(-1) * (raster.reshape(-1) < .25 * 1e38), 100, log=True)\n",
|
|
"plt.show()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|