Commit 6f64ad65 authored by Yannick TANGUY's avatar Yannick TANGUY

Add step2

parent a425da5a
......@@ -16,9 +16,48 @@
"\n",
"## Step 2 : \n",
"\n",
"The aim of this second exercise is to compute a water mask, from NDWI2 values\n",
"The aim of this second exercise is to combine NDWI2 values to create a water mask\n",
"- what kind of function do we need to implement ?\n",
"- are all the pixels valid ?"
"- are all the pixels valid ?\n",
"- we want to maximize water extent\n",
"\n",
"We are also going to use OTB pipeline, to avoid writing intermediate file : \n",
"- the first step will be plugged as input of the second step\n",
"- only the final application will launch the pipeline, with ExecuteAndWriteOutput()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import otbApplication\n",
"import rasterio\n",
"\n",
"import display_api\n",
"import utils\n",
"\n",
"# Data directory\n",
"DATA_DIR = \"data\"\n",
"\n",
"# Output directory\n",
"OUTPUT_DIR = \"output\"\n",
"\n",
"# NDWI synthesis\n",
"NDWI_synthesis = os.path.join(OUTPUT_DIR,\"synthesis_NDWI2.tif\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Compute the NDWI2** \n",
"\n",
"Do we need to adapt the formula to deal with invalid pixels ? \n",
"\n",
"What happens with clouds ?"
]
},
{
......@@ -35,16 +74,84 @@
" # BandMath takes a list of images as input,\n",
" # so we have to give a Python list [ image ], or [ image1, image2, .., imageN]\n",
" app.SetParameterStringList(\"il\",[im])\n",
" app.SetParameterString(\"out\",im.replace(\"RVBPIR.tif\",\"_ndwi2.tif\"))\n",
" out = os.path.join(out_dir, os.path.basename(im.replace(\"_RVBPIR.tif\",\"_NDWI2.tif\")))\n",
" app.SetParameterString(\"out\",out)\n",
" app.SetParameterString(\"exp\",\"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
" # Plug the application in the pipeline \n",
" # does NOT launch the pipeline yet !\n",
" app.Execute()\n",
" # we save the application in a list\n",
" if (index == 0):\n",
" tab_App = [app]\n",
" else:\n",
" tab_App.append(app)\n",
" index += 1\n",
" # the list of applications is returned\n",
" return tab_App"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Compute watermask from a list of NDWI2 images**\n",
"\n",
"We are going to use a list of images to compute a watermask (0 for land, 1 for water) : \n",
"- the inputs must be set in a list with app.AddImageToParameterInputImageList\n",
"- the expression will be use all the images im1b1, im2b1, etc."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compute_watermask(list_app):\n",
" app = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
" index = 1\n",
" exp = \"?????\"\n",
" for appli in list_app:\n",
" app.AddImageToParameterInputImageList(\"il\",appli.GetParameterOutputImage(\"out\"))\n",
" if index==1:\n",
" exp = exp # + \"some function that involves im1b1\"\n",
" else:\n",
" exp = exp # +\", \"+\"im\"+str(index)+\"b1\"\n",
" index += 1\n",
"\n",
"\n",
" print(exp)\n",
"\n",
" \n",
" app.SetParameterString(\"out\",NDWI_synthesis)\n",
" app.SetParameterString(\"exp\",exp)\n",
" app.ExecuteAndWriteOutput()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute radiometric indices (NDVI & NDWI2) on a stack of images\n",
"list_app = app_radiometricindices(utils.list_images(DATA_DIR,\"_RVBPIR.tif\"), OUTPUT_DIR)\n",
"compute_watermask(os.path.join(OUTPUT_DIR, NDWI_synthesis), list_app)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment