Commit f58b712a authored by David Youssefi's avatar David Youssefi

ENH: invert data list + some simplifications

parent f8a6f6b7
......@@ -5,6 +5,7 @@ Some functions for Jupyter displaying
"""
import os
import datetime
import numpy as np
import matplotlib.pyplot as plt
......@@ -110,7 +111,7 @@ def rasters_on_map(rasters_list, out_dir, overlay_names_list, geojson_data=None)
# - plot quicklook
for raster, overlay_name in zip(rasters_list, overlay_names_list):
bounds = transform_bounds(raster.crs, epsg4326, *raster.bounds)
quicklook_url = os.path.join(out_dir, os.path.splitext(overlay_name.replace("/", "_"))[0] + "_PREVIEW.JPG")
quicklook_url = os.path.join(out_dir, "PREVIEW_{}.JPG".format(datetime.datetime.now()))
write_quicklook(raster, quicklook_url)
quicklook = ImageOverlay(
url=quicklook_url,
......
......@@ -51,7 +51,7 @@
"\n",
"# Date list\n",
"date_buttons = ipywidgets.ToggleButtons(\n",
" options=[\"20180706\", \"20180621\", \"20180701\", \"20180711\"],\n",
" options=[\"20180711\", \"20180706\", \"20180701\", \"20180621\"],\n",
" description=\"Dates:\")\n",
"\n",
"date_buttons"
......
......@@ -6,7 +6,7 @@
"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 - JURSE 2019 Vannes France - May 21th\n",
"# OTB Guided Tour - FOSS4G 2019 Bucharest\n",
"## Yannick TANGUY and David YOUSSEFI (CNES, French Space Agency)\n",
"\n",
"<br>\n",
......@@ -26,13 +26,6 @@
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Given example: compute ndvi"
]
},
{
"cell_type": "code",
"execution_count": null,
......@@ -40,11 +33,8 @@
"outputs": [],
"source": [
"import os\n",
"import otbApplication\n",
"import rasterio\n",
"\n",
"import display_api\n",
"import utils\n",
"from glob import glob\n",
"import ipywidgets\n",
"\n",
"# Data directory\n",
"DATA_DIR = \"data\"\n",
......@@ -52,43 +42,30 @@
"# Output directory\n",
"OUTPUT_DIR = \"output\"\n",
"\n",
"print(utils.list_images(DATA_DIR,\"_RVBPIR.tif\"))\n",
"\n",
"def compute_ndvi(images, out_dir):\n",
" \"\"\"\n",
" Compute NDVI from images with Red, Green, Blue and NIR bands\n",
" :param images: list of images. Their filenames match the pattern [...]_RVBPIR.tif\n",
" :param out_dir: path to the output directory\n",
" \"\"\"\n",
" out_list = []\n",
" for im in images:\n",
" out = os.path.join(out_dir, os.path.basename(im.replace(\"_RVBPIR.tif\",\"_NDVI.tif\")))\n",
" # Here we create an application\n",
" app = otbApplication.Registry.CreateApplication(\"BandMath\")\n",
" # 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\", out)\n",
" # the main parameter is the mathematical expression\n",
" # here, we compute NDVI : (nir - red) / (nir + red)\n",
" # The corresponding bands for NIR and Red are respectively the 4th and the 3rd bands (b4, b3)\n",
" # of the first image (im1)\n",
" app.SetParameterString(\"exp\",\"(im1b4-im1b1)/(im1b4+im1b1)\")\n",
" app.ExecuteAndWriteOutput()\n",
" out_list.append(out)\n",
" \n",
" return out_list"
"# Date list\n",
"date_buttons = ipywidgets.ToggleButtons(\n",
" options=[\"20180711\", \"20180706\", \"20180701\", \"20180621\"],\n",
" description=\"Dates:\")\n",
"\n",
"date_buttons"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Compute radiometric indices (NDVI & NDWI2) on a stack of images\n",
"ndvi_filenames_list = compute_ndvi(utils.list_images(DATA_DIR,\"_RVBPIR.tif\"), OUTPUT_DIR)\n",
"print (ndvi_filenames_list)"
"### Given example: compute ndvi\n",
"\n",
"Here we create an application with ```otbApplication.Registry.CreateApplication(\"BandMath\")```\n",
"\n",
"BandMath takes a list of images as input, so we have to give a Python list with ```\"il\"``` parameter : [image], or [image1, image2, .., imageN] and the main parameter is the mathematical expression ```\"exp\"```.\n",
"\n",
"Here, we compute NDVI :\n",
"\\begin{equation}\n",
"\\mbox{NDVI}=\\frac{(Xnir - Xred)}{(Xnir + Xred)}\n",
"\\end{equation}\n",
"\n",
"The corresponding bands for NIR and Red are respectively the 4th and the 1st bands (b4, b1) of the first image (im1)"
]
},
{
......@@ -97,34 +74,48 @@
"metadata": {},
"outputs": [],
"source": [
"# Display on a map\n",
"rasters = list(map(rasterio.open, ndvi_filenames_list))\n",
"overnames = list(map(os.path.basename, ndvi_filenames_list))\n",
"m, dc = display_api.rasters_on_map(rasters, OUTPUT_DIR, overnames)\n",
"m"
"import otbApplication\n",
"\n",
"# 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",
"\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(\"exp\", \"(im1b4-im1b1)/(im1b4+im1b1)\")\n",
" exit_code = app.ExecuteAndWriteOutput()\n",
"\n",
"compute_ndvi()"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Compute ndwi : <b> Fill the <span style=\"color:black;background:yellow\">compute_ndwi</span> function </b> "
"import rasterio\n",
"import display_api\n",
"raster = rasterio.open(out)\n",
"m, dc = display_api.rasters_on_map([raster], OUTPUT_DIR, [DATE+\"_NDVI\"])\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"def compute_ndwi(images, out_dir):\n",
" \"\"\"\n",
" Compute NDWI2 from images with Red, Green, Blue and NIR bands\n",
" :param images: list of images. Their filenames match the pattern [...]_RVBPIR.tif\n",
" :param out_dir: path to the output directory\n",
" \"\"\"\n",
" # TODO: fill the compute_ndwi_function\n",
" return None"
"### Compute ndwi : <b> Fill the <span style=\"color:black;background:yellow\">compute_ndwi</span> function </b> \n",
"\n",
"**Reminder** : NDWI2 is computed from green and nir bands (defined by McFeeters, 1996):\n",
"\n",
"\\begin{equation}\n",
"\\mbox{NDWI2}=\\frac{(Xgreen - Xnir)}{(Xgreen + Xnir)}\n",
"\\end{equation}"
]
},
{
......@@ -133,12 +124,18 @@
"metadata": {},
"outputs": [],
"source": [
"# Compute radiometric indices (NDVI & NDWI2) on a stack of images\n",
"ndwi_filenames_list = compute_ndwi(utils.list_images(DATA_DIR,\"_RVBPIR.tif\"), OUTPUT_DIR)\n",
"if ndwi_filenames_list is not None:\n",
" print (ndwi_filenames_list)http://localhost:8888/notebooks/step1_compute_radiometric_indices.ipynb#\n",
"else:\n",
" print (\"TODO: fill the compute_ndwi_function\")"
"import otbApplication\n",
"\n",
"# 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",
"\n",
"def compute_ndwi():\n",
" # Fill the compute_ndwi function\n",
" pass\n",
" \n",
"compute_ndwi()"
]
},
{
......@@ -147,10 +144,10 @@
"metadata": {},
"outputs": [],
"source": [
"# Display on a map\n",
"rasters = list(map(rasterio.open, ndwi_filenames_list))\n",
"overnames = list(map(os.path.basename, ndwi_filenames_list))\n",
"m, dc = display_api.rasters_on_map(rasters, OUTPUT_DIR, overnames)\n",
"import rasterio\n",
"import display_api\n",
"raster = rasterio.open(out)\n",
"m, dc = display_api.rasters_on_map([raster], OUTPUT_DIR, [DATE+\"_NDWI\"])\n",
"m"
]
},
......
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