Skip to content

Instantly share code, notes, and snippets.

@kissmygritts
Created December 12, 2022 23:14
Show Gist options
  • Save kissmygritts/7c5e16305941963daf4da5907f518d47 to your computer and use it in GitHub Desktop.
Save kissmygritts/7c5e16305941963daf4da5907f518d47 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d54d7f6a-2b7e-4932-8703-362d8a30a80c",
"metadata": {},
"outputs": [],
"source": [
"import rasterio\n",
"from rasterio.enums import Compression\n",
"import numpy as np\n",
"\n",
"from utils.raster.raster_metadata_helpers import DEFAULT_GTIFF_PROFILE"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "4ade3998-0f2e-47d0-ab34-c34c567f2539",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/Users/mitchellgritts/Downloads/departure-rasters/D_NRV.float32.deflate.tif'"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filename = \"D_NRV\"\n",
"dtype=\"float32\"\n",
"nodata=0\n",
"src_raster=f\"/Users/mitchellgritts/Downloads/departure-rasters/{filename}.tiff\"\n",
"dst_raster=f\"/Users/mitchellgritts/Downloads/departure-rasters/{filename}.{dtype}.deflate.tif\"\n",
"dst_raster"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "dd1e2838-57d0-4f4d-a8f9-db32be38b2a5",
"metadata": {},
"outputs": [],
"source": [
"def change_raster_dtype(src_raster, dst_raster, dtype, nodata):\n",
" dtype = np.dtype(dtype)\n",
" with rasterio.open(src_raster, \"r\") as src:\n",
" src_profile = src.profile\n",
" src_data = src.read(1, masked=True)\n",
" \n",
" data = src_data.copy()\n",
" data.set_fill_value(nodata)\n",
" data = data.filled()\n",
" data = data.astype(dtype)\n",
" \n",
" profile = {**src_profile, **DEFAULT_GTIFF_PROFILE, **{\"nodata\": nodata, \"dtype\": dtype, \"compress\": Compression.deflate}}\n",
" print(profile)\n",
" \n",
" with rasterio.open(dst_raster, \"w\", **profile) as dst:\n",
" dst.write(data, 1)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "0fc5ad4c-af59-4495-805d-e3e146538d09",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'driver': 'GTiff', 'dtype': dtype('float32'), 'nodata': 0, 'width': 22257, 'height': 22668, 'count': 1, 'crs': CRS.from_epsg(3310), 'transform': Affine(5.0, 0.0, -100625.739272266,\n",
" 0.0, -5.0, 190467.673104919), 'blockysize': 512, 'tiled': True, 'interleave': 'pixel', 'blockxsize': 512, 'compress': <Compression.deflate: 'DEFLATE'>}\n"
]
}
],
"source": [
"change_raster_dtype(src_raster, dst_raster, dtype, nodata)"
]
},
{
"cell_type": "markdown",
"id": "3c349727-b3a8-4900-a997-5cbe6a7ab282",
"metadata": {},
"source": [
"## test compression equality"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "f7a590dd-51a4-4dfc-98d9-9b495c91acb2",
"metadata": {},
"outputs": [],
"source": [
"with rasterio.open(src_raster, \"r\") as src:\n",
" original_data = src.read(1, masked=True)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "e8e8e0a0-733c-4d8e-a71c-a17925d9a17a",
"metadata": {},
"outputs": [],
"source": [
"with rasterio.open(dst_raster, \"r\") as src:\n",
" compressed_data = src.read(1, masked=True)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "5a2dd370-3d4b-4030-b21a-a9a3349c0c9c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(22668, 22257)\n",
"(22668, 22257)\n"
]
}
],
"source": [
"print(original_data.shape)\n",
"print(compressed_data.shape)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "4875a834-4498-4f22-8d47-d94998ce3ccb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array_equal(original_data.data, compressed_data.data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26918d05-5b22-4a60-a53e-e1e0bd5226b1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment