Commit f7c244cd authored by David Youssefi's avatar David Youssefi

ENH: Simplify notebook (step1,2,3)

parent f58b712a
......@@ -51,7 +51,7 @@
"\n",
"# Date list\n",
"date_buttons = ipywidgets.ToggleButtons(\n",
" options=[\"20180711\", \"20180706\", \"20180701\", \"20180621\"],\n",
" options=[\"20180711\", \"20180701\", \"20180621\"],\n",
" description=\"Dates:\")\n",
"\n",
"date_buttons"
......
......@@ -44,7 +44,7 @@
"\n",
"# Date list\n",
"date_buttons = ipywidgets.ToggleButtons(\n",
" options=[\"20180711\", \"20180706\", \"20180701\", \"20180621\"],\n",
" options=[\"20180711\", \"20180701\", \"20180621\"],\n",
" description=\"Dates:\")\n",
"\n",
"date_buttons"
......@@ -79,13 +79,13 @@
"# input / output files\n",
"DATE = date_buttons.value\n",
"im = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(DATE)))[0]\n",
"out = os.path.join(OUTPUT_DIR, \"NDVI.tif\")\n",
"ndvi = os.path.join(OUTPUT_DIR, \"NDVI.tif\")\n",
"\n",
"# Given example\n",
"def compute_ndvi():\n",
" app = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
" app.SetParameterStringList(\"il\",[im])\n",
" app.SetParameterString(\"out\", out)\n",
" app.SetParameterString(\"out\", ndvi)\n",
" app.SetParameterString(\"exp\", \"(im1b4-im1b1)/(im1b4+im1b1)\")\n",
" exit_code = app.ExecuteAndWriteOutput()\n",
"\n",
......@@ -100,7 +100,7 @@
"source": [
"import rasterio\n",
"import display_api\n",
"raster = rasterio.open(out)\n",
"raster = rasterio.open(ndvi)\n",
"m, dc = display_api.rasters_on_map([raster], OUTPUT_DIR, [DATE+\"_NDVI\"])\n",
"m"
]
......@@ -129,7 +129,7 @@
"# input / output files\n",
"DATE = date_buttons.value\n",
"im = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(DATE)))[0]\n",
"out = os.path.join(OUTPUT_DIR, \"NDWI.tif\")\n",
"ndwi = os.path.join(OUTPUT_DIR, \"NDWI.tif\")\n",
"\n",
"def compute_ndwi():\n",
" # Fill the compute_ndwi function\n",
......@@ -146,7 +146,7 @@
"source": [
"import rasterio\n",
"import display_api\n",
"raster = rasterio.open(out)\n",
"raster = rasterio.open(ndwi)\n",
"m, dc = display_api.rasters_on_map([raster], OUTPUT_DIR, [DATE+\"_NDWI\"])\n",
"m"
]
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img style=\"float: left; margin:0px 15px 15px 0px; width:120px\" src=\"https://www.orfeo-toolbox.org/wp-content/uploads/2016/03/logo-orfeo-toolbox.png\">\n",
"\n",
"# OTB Guided Tour - FOSS4G 2019 Bucharest\n",
"## Yannick TANGUY and David YOUSSEFI (CNES, French Space Agency)\n",
"\n",
"<br>\n",
"\n",
"<b> Press <span style=\"color:black;background:yellow\">SHIFT+ENTER</span> to execute the notebook interactively cell by cell </b></div>\n",
"\n",
"\n",
"## Step 2 : Compute Watermask (without OTB pipeline)\n",
"\n",
"The aim of this second exercise is to combine NDWI2 values to create a water mask : what kind of function do we need to implement ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from glob import glob\n",
"import ipywidgets\n",
"\n",
"# Data directory\n",
"DATA_DIR = \"data\"\n",
"\n",
"# Output directory\n",
"OUTPUT_DIR = \"output\"\n",
"\n",
"# Date list\n",
"DATE_LIST = [\"20180711\", \"20180701\", \"20180621\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute watermask for each image\n",
"\n",
"We are going to threshold NDWI to compute a watermask (0 for land, 1 for water)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import otbApplication\n",
"\n",
"# input / output files\n",
"def compute_ndwi(date):\n",
" im = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date)))[0]\n",
" ndwi = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date))\n",
" app = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
" app.SetParameterStringList(\"il\",[im])\n",
" app.SetParameterString(\"out\", ndwi)\n",
" app.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
" exit_code = app.ExecuteAndWriteOutput()\n",
" return ndwi\n",
"\n",
"# compute ndwi for each image\n",
"ndwi0 = compute_ndwi(DATE_LIST[0])\n",
"ndwi1 = compute_ndwi(DATE_LIST[1])\n",
"ndwi2 = compute_ndwi(DATE_LIST[2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import rasterio\n",
"import display_api\n",
"\n",
"rasters = list(map(rasterio.open, [ndwi0, ndwi1, ndwi2]))\n",
"m, dc = display_api.rasters_on_map(rasters, OUTPUT_DIR, DATE_LIST)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Threshold ndwi : <b> Fill the <span style=\"color:black;background:yellow\">threshold_ndwi</span> function </b> \n",
"\n",
"**Tips:** The formula to threshold the NDWI images can be written using\n",
"- binary operators:\n",
" - ‘+’ addition, ‘-‘ subtraction, ‘*’ multiplication, ‘/’ division\n",
" - ‘^’ raise x to the power of y\n",
" - ‘<’ less than, **‘>’ greater than**, ‘<=’ less or equal, ‘>=’ greater or equal\n",
" - ‘==’ equal, ‘!=’ not equal\n",
" - ‘||’ logical or, ‘&&’ logical and\n",
"- functions: exp(), log(), sin(), cos(), min(), **max()**, ...\n",
"\n",
"https://www.orfeo-toolbox.org/CookBook/Applications/app_BandMath.html\n",
"\n",
"**So combine the NDWI images and threshold the result...**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"watermask = os.path.join(OUTPUT_DIR, \"WATERMASK.tif\")\n",
"def threshold_ndwi(ndwi1, ndwi2, ndwi3):\n",
" # Fill the threshold_ndwi function\n",
" pass\n",
"\n",
"threshold_ndwi()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import rasterio\n",
"import display_api\n",
"\n",
"raster = rasterio.open(watermask)\n",
"m, dc = display_api.rasters_on_map([raster], OUTPUT_DIR, [\"WATERMASK\"])\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Go to the [Step 3](./step3_segment_watermask_threshold_NDWI.ipynb)"
]
}
],
"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",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img style=\"float: left; margin:0px 15px 15px 0px; width:120px\" src=\"https://www.orfeo-toolbox.org/wp-content/uploads/2016/03/logo-orfeo-toolbox.png\">\n",
"\n",
"# OTB Guided Tour - FOSS4G 2019 Bucharest\n",
"## Yannick TANGUY and David YOUSSEFI (CNES, French Space Agency)\n",
"\n",
"<br>\n",
"\n",
"<b> Press <span style=\"color:black;background:yellow\">SHIFT+ENTER</span> to execute the notebook interactively cell by cell </b></div>\n",
"\n",
"## Step 3 : Compute Watermask (with OTB pipeline)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from glob import glob\n",
"import ipywidgets\n",
"\n",
"# Data directory\n",
"DATA_DIR = \"data\"\n",
"\n",
"# Output directory\n",
"OUTPUT_DIR = \"output\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute Watermask without OTB Pipeline\n",
"\n",
"Chaining applications currently requires to write/read back images between applications, resulting in heavy I/O operations and a significant amount of time dedicated to writing temporary files, here NDWI files."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import otbApplication\n",
"import time\n",
"\n",
"tic = time.clock()\n",
"\n",
"watermask = os.path.join(OUTPUT_DIR, \"WATERMASK.tif\")\n",
"\n",
"# input parameters\n",
"date1 = \"20180711\"\n",
"im1 = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date1)))[0]\n",
"ndwi1 = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date1))\n",
"date2 = \"20180701\"\n",
"im2 = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date2)))[0]\n",
"ndwi2 = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date2))\n",
"date3 = \"20180621\"\n",
"im3 = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date3)))[0]\n",
"ndwi3 = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date3))\n",
"\n",
"# first app\n",
"app1 = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"app1.SetParameterStringList(\"il\",[im1])\n",
"app1.SetParameterString(\"out\", ndwi1)\n",
"app1.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
"app1.ExecuteAndWriteOutput()\n",
"\n",
"# second app\n",
"app2 = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"app2.SetParameterStringList(\"il\",[im2])\n",
"app2.SetParameterString(\"out\", ndwi2)\n",
"app2.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
"app2.ExecuteAndWriteOutput()\n",
"\n",
"# third app\n",
"app3 = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"app3.SetParameterStringList(\"il\",[im3])\n",
"app3.SetParameterString(\"out\", ndwi3)\n",
"app3.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
"app3.ExecuteAndWriteOutput()\n",
"\n",
"# thres app\n",
"thresapp = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"thresapp.SetParameterStringList(\"il\",[ndwi1, ndwi2, ndwi3])\n",
"thresapp.SetParameterString(\"out\", watermask)\n",
"thresapp.SetParameterString(\"exp\", \"max(im1b1, im2b1, im3b1) > 0.3\")\n",
"thresapp.ExecuteAndWriteOutput()\n",
"\n",
"toc = time.clock()\n",
"print (\"Duration time: {} seconds\".format(toc-tic))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute Watermask with OTB Pipeline\n",
"\n",
"Since OTB 5.8, it is possible to connect an output image parameter from one application to the input image parameter of the next parameter. This results in the wiring of the internal ITK/OTB pipelines together, permitting image streaming between the applications. Consequently, this removes the need of writing temporary images and improves performance. Only the last application of the processing chain is responsible for writing the final result images.\n",
"\n",
"<b> Please rewrite the <span style=\"color:black;background:yellow\"> code bellow </span> in order to only write the watermask file </b>\n",
"\n",
"**Tips:** Only call Execute() to setup the pipeline, not ExecuteAndWriteOutput() which would run it and write the output image and also use these functions to connect OTB applications :\n",
"- ```GetParameterOutputImage``` : get a pointer to an image object [instead of reading from file]\n",
"- ```AddImageToParameterInputImageList``` : add an image to an InputImageList parameter as an pointer to an image object pointer [instead of reading from file] (```SetParameterInputImage``` for an InputImageList)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import otbApplication\n",
"import time\n",
"\n",
"tic = time.clock()\n",
"\n",
"watermask = os.path.join(OUTPUT_DIR, \"WATERMASK.tif\")\n",
"\n",
"# input parameters\n",
"date1 = \"20180711\"\n",
"im1 = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date1)))[0]\n",
"ndwi1 = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date1))\n",
"date2 = \"20180701\"\n",
"im2 = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date2)))[0]\n",
"ndwi2 = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date2))\n",
"date3 = \"20180621\"\n",
"im3 = glob(os.path.join(DATA_DIR, \"*{}*.tif\".format(date3)))[0]\n",
"ndwi3 = os.path.join(OUTPUT_DIR, \"NDWI_{}.tif\".format(date3))\n",
"\n",
"# first app\n",
"app1 = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"app1.SetParameterStringList(\"il\",[im1])\n",
"app1.SetParameterString(\"out\", ndwi1)\n",
"app1.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
"app1.ExecuteAndWriteOutput() # to be modified\n",
"\n",
"# second app\n",
"app2 = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"app2.SetParameterStringList(\"il\",[im2])\n",
"app2.SetParameterString(\"out\", ndwi2)\n",
"app2.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
"app2.ExecuteAndWriteOutput() # to be modified\n",
"\n",
"# third app\n",
"app3 = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"app3.SetParameterStringList(\"il\",[im3])\n",
"app3.SetParameterString(\"out\", ndwi3)\n",
"app3.SetParameterString(\"exp\", \"(im1b2-im1b4)/(im1b2+im1b4)\")\n",
"app3.ExecuteAndWriteOutput() # to be modified\n",
"\n",
"# thres app\n",
"thresapp = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
"thresapp.SetParameterStringList(\"il\",[ndwi1, ndwi2, ndwi3]) # to be modified\n",
"thresapp.SetParameterString(\"out\", watermask)\n",
"thresapp.SetParameterString(\"exp\", \"max(im1b1, im2b1, im3b1) > 0.3\")\n",
"thresapp.ExecuteAndWriteOutput()\n",
"\n",
"toc = time.clock()\n",
"print (\"Duration time: {} seconds\".format(toc-tic))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import rasterio\n",
"import display_api\n",
"\n",
"raster = rasterio.open(watermask)\n",
"m, dc = display_api.rasters_on_map([raster], OUTPUT_DIR, [\"WATERMASK\"])\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Go to the [Step 4](step4_filter_shapefile.ipynb)"
]
}
],
"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",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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