Created
March 2, 2023 20:44
-
-
Save kissmygritts/5037601679a85dfdb4dc9be3bc07aaba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/Users/mitchellgritts/Documents/projects/vp-airflow/dags\n" | |
] | |
} | |
], | |
"source": [ | |
"cd ../.." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"import os\n", | |
"import numpy as np\n", | |
"import rasterio\n", | |
"from rasterio.transform import Affine\n", | |
"from rasterio import CRS\n", | |
"from common.bounding_box import BoundingBox\n", | |
"\n", | |
"downloads_dir = \"/Users/mitchellgritts/Downloads\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# make raster helper work locally\n", | |
"def make_raster(\n", | |
" filename: str,\n", | |
" data = np.ones((3, 3), dtype=np.float32),\n", | |
" profile = None,\n", | |
" resolution = (5, -5),\n", | |
" bounding_box: BoundingBox = None,\n", | |
" crs = None,\n", | |
") -> str:\n", | |
" raster_path = os.path.join(downloads_dir, filename)\n", | |
"\n", | |
" # If no profile is provided, create a default profile\n", | |
" default_profile = {\n", | |
" \"count\": 1,\n", | |
" \"compress\": \"lzw\",\n", | |
" \"crs\": CRS.from_epsg(3310),\n", | |
" \"driver\": \"GTiff\",\n", | |
" \"dtype\": data.dtype,\n", | |
" \"height\": data.shape[0],\n", | |
" \"interleave\": \"band\",\n", | |
" \"nodata\": 0.0,\n", | |
" \"tiled\": False,\n", | |
" \"transform\": Affine.translation(0, 5)\n", | |
" * Affine.scale(resolution[0], resolution[1]),\n", | |
" \"width\": data.shape[1],\n", | |
" }\n", | |
"\n", | |
" if profile is None:\n", | |
" new_profile = default_profile\n", | |
" else:\n", | |
" new_profile = {**default_profile, **profile}\n", | |
"\n", | |
" # If bounds are provided, overwrite the transform\n", | |
" if bounding_box is not None:\n", | |
" new_profile[\"transform\"] = rasterio.transform.from_bounds(\n", | |
" *bounding_box.to_rasterio(), new_profile[\"width\"], new_profile[\"height\"]\n", | |
" )\n", | |
"\n", | |
" # If a CRS is provided, update the profile\n", | |
" if crs is not None:\n", | |
" new_profile[\"crs\"] = crs\n", | |
"\n", | |
" with rasterio.open(raster_path, \"w\", **new_profile) as dst:\n", | |
" if new_profile[\"count\"] > 1:\n", | |
" dst.write(data) # handles multiband writes\n", | |
" else:\n", | |
" dst.write(data, 1)\n", | |
" return str(raster_path)\n", | |
"\n", | |
"# ref_raster = make_raster(\"rasterize-ref-raster.tif\", np.ones((4, 4), np.float32), crs=CRS.from_epsg(26910))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a = np.array([\n", | |
" [0, 1, 0, 1, 0],\n", | |
" [1, 0, 1, 0, 1],\n", | |
" [0, 1, 0, 1, 0],\n", | |
" [1, 0, 1, 0, 1],\n", | |
" [0, 1, 0, 1, 0],\n", | |
"], np.int16)\n", | |
"\n", | |
"ref_raster = make_raster(\"raster-nodata.tif\", a, crs=CRS.from_epsg(26910))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.0\n", | |
"1.0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"masked_array(\n", | |
" data=[[[0, --, 0, --, 0],\n", | |
" [--, 0, --, 0, --],\n", | |
" [0, --, 0, --, 0],\n", | |
" [--, 0, --, 0, --],\n", | |
" [0, --, 0, --, 0]]],\n", | |
" mask=[[[False, True, False, True, False],\n", | |
" [ True, False, True, False, True],\n", | |
" [False, True, False, True, False],\n", | |
" [ True, False, True, False, True],\n", | |
" [False, True, False, True, False]]],\n", | |
" fill_value=1,\n", | |
" dtype=int16)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"with rasterio.open(ref_raster, \"r+\") as src:\n", | |
" print(src.nodata)\n", | |
" # changes no data value\n", | |
" src.nodata = 1 # <- change this value between 1 and 0\n", | |
"\n", | |
"with rasterio.open(ref_raster) as src:\n", | |
" print(src.nodata)\n", | |
" data = src.read(masked=True)\n", | |
"\n", | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.0\n", | |
"32767.0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"masked_array(\n", | |
" data=[[[0, 1, 0, 1, 0],\n", | |
" [1, 0, 1, 0, 1],\n", | |
" [0, 1, 0, 1, 0],\n", | |
" [1, 0, 1, 0, 1],\n", | |
" [0, 1, 0, 1, 0]]],\n", | |
" mask=[[[False, False, False, False, False],\n", | |
" [False, False, False, False, False],\n", | |
" [False, False, False, False, False],\n", | |
" [False, False, False, False, False],\n", | |
" [False, False, False, False, False]]],\n", | |
" fill_value=32767,\n", | |
" dtype=int16)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"with rasterio.open(ref_raster, \"r+\") as src:\n", | |
" print(src.nodata)\n", | |
" # changes no data value to proper no data value, but it doesn't exist in the data\n", | |
" src.nodata = 32767\n", | |
"\n", | |
"with rasterio.open(ref_raster) as src:\n", | |
" print(src.nodata)\n", | |
" data = src.read(masked=True)\n", | |
"\n", | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.0\n" | |
] | |
} | |
], | |
"source": [ | |
"# only safe way to do the above, start with a fresh raster\n", | |
"ref_raster = make_raster(\"raster-nodata.tif\", a, crs=CRS.from_epsg(26910))\n", | |
"\n", | |
"with rasterio.open(ref_raster) as src:\n", | |
" print(src.nodata)\n", | |
" src_nodata = src.nodata\n", | |
" src_profile = src.profile\n", | |
" data = src.read() # <- note, not masked\n", | |
"\n", | |
"# change old nodata to new nodata \n", | |
"data[data == src_nodata] = 32767\n", | |
"out_profile = src_profile | {\"nodata\": 32767}\n", | |
"\n", | |
"with rasterio.open(ref_raster, \"w\", **out_profile) as dst:\n", | |
" dst.write(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"32767.0\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[32767, 1, 32767, 1, 32767],\n", | |
" [ 1, 32767, 1, 32767, 1],\n", | |
" [32767, 1, 32767, 1, 32767],\n", | |
" [ 1, 32767, 1, 32767, 1],\n", | |
" [32767, 1, 32767, 1, 32767]]], dtype=int16)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# confirm it worked\n", | |
"with rasterio.open(ref_raster) as src:\n", | |
" print(src.nodata)\n", | |
" data = src.read()\n", | |
" maksed_data = src.read(masked=True)\n", | |
"\n", | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"masked_array(\n", | |
" data=[[[--, 1, --, 1, --],\n", | |
" [1, --, 1, --, 1],\n", | |
" [--, 1, --, 1, --],\n", | |
" [1, --, 1, --, 1],\n", | |
" [--, 1, --, 1, --]]],\n", | |
" mask=[[[ True, False, True, False, True],\n", | |
" [False, True, False, True, False],\n", | |
" [ True, False, True, False, True],\n", | |
" [False, True, False, True, False],\n", | |
" [ True, False, True, False, True]]],\n", | |
" fill_value=32767,\n", | |
" dtype=int16)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"maksed_data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "vp-notebooks", | |
"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.10" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "c6cb751f7a84b15249cfcdc14cfd0ad8b76f646306bb007baa1bfc7184868437" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment