|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Import Libraries" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import os\n", |
| 17 | + "import sys\n", |
| 18 | + "import rasterio\n", |
| 19 | + "import numpy as np\n", |
| 20 | + "import pandas as pd\n", |
| 21 | + "import matplotlib.pyplot as plt\n", |
| 22 | + "import shutil\n", |
| 23 | + "from pathlib import Path\n", |
| 24 | + "from rasterio.mask import mask\n", |
| 25 | + "from rasterio.enums import Resampling\n", |
| 26 | + "from rasterio.warp import reproject, Resampling\n", |
| 27 | + "\n" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "# Import Constants" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 2, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "# Get the current working directory\n", |
| 44 | + "current_dir = os.path.abspath('')\n", |
| 45 | + "\n", |
| 46 | + "# Search for the 'constants.py' file starting from the current directory and moving up the hierarchy\n", |
| 47 | + "project_root = current_dir\n", |
| 48 | + "while not os.path.isfile(os.path.join(project_root, 'constants.py')):\n", |
| 49 | + " project_root = os.path.dirname(project_root)\n", |
| 50 | + "\n", |
| 51 | + "# Add the project root to the Python path\n", |
| 52 | + "sys.path.append(project_root)\n" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": 9, |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "from constants import (SERVER_PATH,\n", |
| 62 | + "OUTPUT_PATH, MASKED_RASTERS_SIM_DIR, SIMULATION_FEATURES_DIR)\n" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 10, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [ |
| 70 | + { |
| 71 | + "data": { |
| 72 | + "text/plain": [ |
| 73 | + "['/Users/romero61/../../capstone/pyforest/ml_data/output/sim_lup_features']" |
| 74 | + ] |
| 75 | + }, |
| 76 | + "execution_count": 10, |
| 77 | + "metadata": {}, |
| 78 | + "output_type": "execute_result" |
| 79 | + } |
| 80 | + ], |
| 81 | + "source": [ |
| 82 | + "SIMULATION_FEATURES_DIR" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 6, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "# Constants\n", |
| 92 | + "# update the first rasters_to_use_as_mask with the folder with the raster you want to use as a mask\n", |
| 93 | + "raster_to_use_as_mask25 = rasterio.open(os.path.join(MASKED_RASTERS_SIM_DIR[0], 'treecover_2010_masked_sim25.tif'))\n", |
| 94 | + "\n", |
| 95 | + "raster_to_use_as_mask50 = rasterio.open(os.path.join(MASKED_RASTERS_SIM_DIR[0], 'treecover_2010_masked_sim50.tif'))\n", |
| 96 | + "\n", |
| 97 | + "\n", |
| 98 | + "raster_to_use_as_mask5 = rasterio.open(os.path.join(MASKED_RASTERS_SIM_DIR[0], 'treecover_2010_masked_sim5.tif'))\n", |
| 99 | + "\n", |
| 100 | + "\n", |
| 101 | + "raster_to_use_as_maskhedges = rasterio.open(os.path.join(MASKED_RASTERS_SIM_DIR[0], 'treecover_2010_masked_simhedges.tif'))\n", |
| 102 | + "\n", |
| 103 | + "\n" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": 12, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "def treecover_mask(input_mask_ds, input_raster, output_raster_path):\n", |
| 113 | + " # Read the data from the mask raster\n", |
| 114 | + " mask = input_mask_ds.read(1)\n", |
| 115 | + "\n", |
| 116 | + " # Open the second raster\n", |
| 117 | + " with rasterio.open(input_raster) as second_ds:\n", |
| 118 | + " profile = second_ds.profile\n", |
| 119 | + " second_raster = second_ds.read(1)\n", |
| 120 | + "\n", |
| 121 | + " # Create a mask where forest is 0 (nonforested)\n", |
| 122 | + " mask = mask == 0\n", |
| 123 | + "\n", |
| 124 | + " # Apply the mask to the second raster, setting those values to -1\n", |
| 125 | + " second_raster[mask] = -1\n", |
| 126 | + "\n", |
| 127 | + " # Update the profile for the output raster\n", |
| 128 | + " profile.update(\n", |
| 129 | + " dtype=rasterio.float32,\n", |
| 130 | + " nodata=-1\n", |
| 131 | + " )\n", |
| 132 | + "\n", |
| 133 | + " # Save the result to a new file\n", |
| 134 | + " with rasterio.open(output_raster_path, 'w', **profile) as dst:\n", |
| 135 | + " dst.write(second_raster.astype(rasterio.float32), 1)" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "# Process Files" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": 8, |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [ |
| 150 | + { |
| 151 | + "data": { |
| 152 | + "text/plain": [ |
| 153 | + "['/Users/romero61/../../capstone/pyforest/ml_data/output/masked_rasters_sims']" |
| 154 | + ] |
| 155 | + }, |
| 156 | + "execution_count": 8, |
| 157 | + "metadata": {}, |
| 158 | + "output_type": "execute_result" |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "MASKED_RASTERS_SIM_DIR" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "## Stacks" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "markdown", |
| 174 | + "metadata": {}, |
| 175 | + "source": [ |
| 176 | + "# 25" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "output_folder = os.path.join(SERVER_PATH, 'ml_data', 'output','tree_masked_rasters_sim25')" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": 11, |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "# Mask land use plan\n", |
| 195 | + "input_lup_10 = os.path.join(SIMULATION_FEATURES_DIR[0], 'sim25_raster.tif')\n", |
| 196 | + "lup_output_raster = os.path.join(output_folder, 'lup_25_treemask.tif')\n", |
| 197 | + "treecover_mask(raster_to_use_as_mask25, input_lup_10, lup_output_raster)\n", |
| 198 | + "\n", |
| 199 | + "# Mask distance to river raster\n", |
| 200 | + "input_river = os.path.join(MASKED_RASTERS_DIR[0], 'river_masked.tif')\n", |
| 201 | + "river_output_raster = os.path.join(output_folder, 'river_treemask.tif')\n", |
| 202 | + "treecover_mask(raster_to_use_as_mask25, input_river, river_output_raster)\n", |
| 203 | + "\n", |
| 204 | + "\n", |
| 205 | + "# Mask distance to road raster\n", |
| 206 | + "input_road = os.path.join(MASKED_RASTERS_DIR[0], 'road_masked.tif')\n", |
| 207 | + "road_output_raster = os.path.join(output_folder, 'road_treemask.tif')\n", |
| 208 | + "treecover_mask(raster_to_use_as_mask25, input_road, road_output_raster)\n", |
| 209 | + "\n", |
| 210 | + "# Mask soil \n", |
| 211 | + "input_soil = os.path.join(MASKED_RASTERS_DIR[0], 'soil_masked.tif')\n", |
| 212 | + "soil_output_raster = os.path.join(output_folder, 'soil_treemask.tif')\n", |
| 213 | + "treecover_mask(raster_to_use_as_mask25, input_soil, soil_output_raster)\n", |
| 214 | + "\n", |
| 215 | + "# Mask precipitation \n", |
| 216 | + "input_precipitation = os.path.join(MASKED_RASTERS_DIR[0], 'precipitation_masked.tif')\n", |
| 217 | + "precipitation_output_raster = os.path.join(output_folder, 'precipitation_treemask.tif')\n", |
| 218 | + "treecover_mask(raster_to_use_as_mask25, input_precipitation, precipitation_output_raster)\n", |
| 219 | + "\n", |
| 220 | + "# Mask ports\n", |
| 221 | + "input_ports = os.path.join(MASKED_RASTERS_DIR[0], 'ports_masked.tif')\n", |
| 222 | + "ports_output_raster = os.path.join(output_folder, 'ports_treemask.tif')\n", |
| 223 | + "treecover_mask(raster_to_use_as_mask25, input_ports, ports_output_raster)\n", |
| 224 | + "\n", |
| 225 | + "# Mask cities\n", |
| 226 | + "input_cities = os.path.join(MASKED_RASTERS_DIR[0], 'cities_masked.tif')\n", |
| 227 | + "cities_output_raster = os.path.join(output_folder, 'cities_treemask.tif')\n", |
| 228 | + "treecover_mask(raster_to_use_as_mask25, input_cities, cities_output_raster)\n", |
| 229 | + "\n" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "markdown", |
| 234 | + "metadata": {}, |
| 235 | + "source": [ |
| 236 | + "# 50" |
| 237 | + ] |
| 238 | + }, |
| 239 | + { |
| 240 | + "cell_type": "code", |
| 241 | + "execution_count": null, |
| 242 | + "metadata": {}, |
| 243 | + "outputs": [], |
| 244 | + "source": [ |
| 245 | + "output_folder = os.path.join(SERVER_PATH, 'ml_data', 'output','tree_masked_rasters_sim25')" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "code", |
| 250 | + "execution_count": 13, |
| 251 | + "metadata": {}, |
| 252 | + "outputs": [], |
| 253 | + "source": [] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "markdown", |
| 257 | + "metadata": {}, |
| 258 | + "source": [ |
| 259 | + "# 5" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": null, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [], |
| 267 | + "source": [ |
| 268 | + "output_folder = os.path.join(SERVER_PATH, 'ml_data', 'output','tree_masked_rasters_sim25')" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": 14, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "markdown", |
| 280 | + "metadata": {}, |
| 281 | + "source": [ |
| 282 | + "# Hedges" |
| 283 | + ] |
| 284 | + }, |
| 285 | + { |
| 286 | + "cell_type": "code", |
| 287 | + "execution_count": null, |
| 288 | + "metadata": {}, |
| 289 | + "outputs": [], |
| 290 | + "source": [ |
| 291 | + "output_folder = os.path.join(SERVER_PATH, 'ml_data', 'output','tree_masked_rasters_sim25')" |
| 292 | + ] |
| 293 | + }, |
| 294 | + { |
| 295 | + "cell_type": "code", |
| 296 | + "execution_count": 15, |
| 297 | + "metadata": {}, |
| 298 | + "outputs": [], |
| 299 | + "source": [] |
| 300 | + }, |
| 301 | + { |
| 302 | + "cell_type": "code", |
| 303 | + "execution_count": null, |
| 304 | + "metadata": {}, |
| 305 | + "outputs": [], |
| 306 | + "source": [] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "code", |
| 310 | + "execution_count": 6, |
| 311 | + "metadata": {}, |
| 312 | + "outputs": [], |
| 313 | + "source": [] |
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "code", |
| 317 | + "execution_count": 7, |
| 318 | + "metadata": {}, |
| 319 | + "outputs": [], |
| 320 | + "source": [] |
| 321 | + } |
| 322 | + ], |
| 323 | + "metadata": { |
| 324 | + "kernelspec": { |
| 325 | + "display_name": "Python 3.11.3 ('pyforest')", |
| 326 | + "language": "python", |
| 327 | + "name": "python3" |
| 328 | + }, |
| 329 | + "language_info": { |
| 330 | + "codemirror_mode": { |
| 331 | + "name": "ipython", |
| 332 | + "version": 3 |
| 333 | + }, |
| 334 | + "file_extension": ".py", |
| 335 | + "mimetype": "text/x-python", |
| 336 | + "name": "python", |
| 337 | + "nbconvert_exporter": "python", |
| 338 | + "pygments_lexer": "ipython3", |
| 339 | + "version": "3.11.3" |
| 340 | + }, |
| 341 | + "orig_nbformat": 4, |
| 342 | + "vscode": { |
| 343 | + "interpreter": { |
| 344 | + "hash": "aca51edb778be56207d5a76d5369999259e96b3950a8b6f86c3be07548c77925" |
| 345 | + } |
| 346 | + } |
| 347 | + }, |
| 348 | + "nbformat": 4, |
| 349 | + "nbformat_minor": 2 |
| 350 | +} |
0 commit comments