Commit 778cfd41 authored by Manuel Grizonnet's avatar Manuel Grizonnet

ENH: add script with solutions for otb-python exercise

parent f0853cda
#! /usr/bin/python
import otbApplication
if __name__ == "__main__":
# Print the available applications
print str(otbApplication.Registry.GetAvailableApplications())
# Initialization of a dictonnary for the input data paths
d= {}
###############
# FILL THE GAP 1 : Complete your dataset_folder with the
# WorkshopDataset app-python/data path
#
# Exemple:
# d["dataset_folder"] = "/home/WorkshopData/app-python/images"
#
d["dataset_folder"] = "images"
#
# END OF GAP
###############
d["image_name"] = "SENTINEL2A_20170407-154054-255_L2A_T17MNP_D_V1-4"
d["input_path"] = d["dataset_folder"] + "/" + d["image_name"] +"/"
d["B3_image"] = d["image_name"] + "_FRE_B3.tif"
d["B4_image"] = d["image_name"] + "_FRE_B4.tif"
d["B5_image"] = d["image_name"] + "_FRE_B5.tif"
d["B8A_image"] = d["image_name"] + "_FRE_B8A.tif"
d["B11_image"] = d["image_name"] + "_FRE_B11.tif"
#############################################
# Application 1 : Resampling operation #
############################################ #
###############
# FILL THE GAP 2 : Create an instance of the Superimpose application
# WorkshopDataset app-python/data path
#
# Exemple:
# Superimpose = otbApplication.Registry.CreateApplication("???")
#
Superimpose = otbApplication.Registry.CreateApplication("Superimpose")
#
# END OF GAP
###############
# The following lines set the necessary application parameters:
Superimpose.SetParameterString("inr",str( d["input_path"] + d["B4_image"]))
Superimpose.SetParameterString("inm",str( d["input_path"] + d["B8A_image"]))
Superimpose.SetParameterString("out", "B8A_10m.tif")
# The following line execute the application
Superimpose.ExecuteAndWriteOutput()
#! /usr/bin/python
import otbApplication
if __name__ == "__main__":
# Initialization of a dictonnary for the input images paths
d= {}
# ---
# FILL THE GAP 1 : Complete your dataset_folder with the
# WorkshopDataset app-python/images path
#
# Exemple:
# d["dataset_folder"] = "/home/WorkshopData/app-python/images"
#
d["dataset_folder"] = "images"
#
# END OF GAP
# ---
d["image_name"] = "SENTINEL2A_20170407-154054-255_L2A_T17MNP_D_V1-4"
d["input_path"] = d["dataset_folder"] + "/" + d["image_name"] +"/"
d["B3_image"] = d["image_name"] + "_FRE_B3.tif"
d["B4_image"] = d["image_name"] + "_FRE_B4.tif"
d["B8A_image"] = d["image_name"] + "_FRE_B8A.tif"
d["B11_image"] = d["image_name"] + "_FRE_B11.tif"
##########################################
# Exercise 2 : Chaining applications #
##########################################
########### Application 1 : Resampling
#
# The following line creates an instance of the Superimpose application
application1 = otbApplication.Registry.CreateApplication("Superimpose")
# ---
# FILL THE GAP 2 : Complete the input and output parameters
#
# Exemple:
#
#application1.SetParameterString("inr",str( d["input_path"] + d["????"]))
#application1.SetParameterString("inm",str( d["input_path"] + d["????"]))
#application1.SetParameterString("out", "????.tif")
application1.SetParameterString("inr",str( d["input_path"] + d["B4_image"]))
application1.SetParameterString("inm",str( d["input_path"] + d["B8A_image"]))
application1.SetParameterString("out", "B8A_10.tif")
#
# END OF GAP
# ---
print "Launching... Resampling"
# The following line execute the application
application1.ExecuteAndWriteOutput()
print "End of Resampling \n"
########### Application 2 : NDVI Calculation
#
# Create the necessary OTB Applications
application2 = otbApplication.Registry.CreateApplication("BandMath")
# ---
# FILL THE GAP 3 : Complete the input and output parameters of the BandMath
#
# Example:
#application2.SetParameterStringList("il",["????.tif", str(d["input_path"] + d["?????"])])
#application2.SetParameterString("out", "????.tif")
#application2.SetParameterString("exp", "?????")
application2.SetParameterStringList("il",["B8A_10.tif", str(d["input_path"] + d["B4_image"])])
application2.SetParameterString("out", "ndvi.tif")
application2.SetParameterString("exp", "(im1b1-im2b1)/(im1b1+im2b1)")
#
#
# END OF GAP
# ---
# The following line execute the application
print "Launching... BandMath : NDVI"
application2.ExecuteAndWriteOutput()
print "End of BandMath NDVI \n"
########### Application 3: Mask Threshold
#
# Create the necessary OTB Applications
application3 = otbApplication.Registry.CreateApplication("BandMath")
# ---
# FILL THE GAP 4 : Complete the input and output parameters of the BandMath
#
# Exemple:
#application3.SetParameterStringList("il",["????.tif"])
#application3.SetParameterString("out", "????.tif")
#application3.SetParameterString("exp", "????")
application3.SetParameterStringList("il",["ndvi.tif"])
application3.SetParameterString("out", "water_mask.tif")
application3.SetParameterString("exp", "im1b1<0?1:0")
#
# END OF GAP
# ---
# The following line execute the application
print "Launching... BandMath : Threshold mask on NDVI"
application3.ExecuteAndWriteOutput()
print "End of BandMath : Threshold Mask \n"
#! /usr/bin/python
import otbApplication
if __name__ == "__main__":
# Initialization of a dictonnary for the input data paths
d= {}
# ---
# FILL THE GAP 1 : Complete your dataset_folder with the
# WorkshopDataset app-python/images path
#
# Exemple:
# d["dataset_folder"] = "/home/WorkshopData/app-python/images"
#
d["dataset_folder"] = "images"
#
# END OF GAP
# ---
d["image_name"] = "SENTINEL2A_20170407-154054-255_L2A_T17MNP_D_V1-4"
d["input_path"] = d["dataset_folder"] + "/" + d["image_name"] +"/"
d["B3_image"] = d["image_name"] + "_FRE_B3.tif"
d["B4_image"] = d["image_name"] + "_FRE_B4.tif"
d["B8A_image"] = d["image_name"] + "_FRE_B8A.tif"
d["B11_image"] = d["image_name"] + "_FRE_B11.tif"
##########################################
# Exercise 2 : Chaining applications #
##########################################
########### Application 1 : Resampling
#
# The following line creates an instance of the Superimpose application
application1 = otbApplication.Registry.CreateApplication("Superimpose")
application1.SetParameterString("inr",str( d["input_path"] + d["B4_image"]))
application1.SetParameterString("inm",str( d["input_path"] + d["B8A_image"]))
application1.SetParameterString("out", "B8A_10m.tif")
print "Launching... Resampling"
# The following line execute the application
application1.Execute()
print "End of Resampling \n"
########### Application 2 : NDVI Calculation
#
# Create the necessary OTB Applications
application2 = otbApplication.Registry.CreateApplication("BandMath")
# ---
# FILL THE GAP 2 : In-memory connection:
# declare the application1 output as the application2
# input (input name "il", StringList type)
#
# Exemple:
# application?.AddImageToParameterInputImageList("il",application?.GetParameterOutputImage("???"))
application2.AddImageToParameterInputImageList("il",application1.GetParameterOutputImage("out"))
#
# END OF GAP
# ---
# Declare the input list : the first element is declared alone (im1 = Red-B4)
application2.AddParameterStringList("il",str(d["input_path"] + d["B4_image"]))
application2.SetParameterString("out", "ndvi_mask.tif")
# ---
# FILL THE GAP 3 : Complete the BandMath expression
#
# Example:
# application2.SetParameterString("exp", "?????")
application2.SetParameterString("exp", "(im1b1-im2b1)/(im1b1+im2b1)<0?1:0")
#
#
# END OF GAP
# ---
# The following line execute the application
print "Launching... BandMath : Water Mask by NDVI"
application2.ExecuteAndWriteOutput()
print "End of BandMath NDVI \n"
#! /usr/bin/python
from sys import argv
import otbApplication
if __name__ == "__main__":
# Initialization of a dictonnary for the input data paths
d= {}
# ---
# FILL THE GAP 1 : Complete your dataset_folder with the
# WorkshopDataset app-python/images path
#
# Example:
# d["dataset_folder"] = "/home/WorkshopData/app-python/images"
#
d["dataset_folder"] = "images"
#
# END OF GAP
# ---
if len(argv) > 1:
d["image_name"] = argv[1]
else:
d["image_name"] = "SENTINEL2A_20170407-154054-255_L2A_T17MNP_D_V1-4"
d["input_path"] = d["dataset_folder"] + "/" + d["image_name"] +"/"
d["B3_image"] = d["image_name"] + "_FRE_B3.tif"
d["B4_image"] = d["image_name"] + "_FRE_B4.tif"
d["B5_image"] = d["image_name"] + "_FRE_B5.tif"
d["B8A_image"] = d["image_name"] + "_FRE_B8A.tif"
d["B11_image"] = d["image_name"] + "_FRE_B11.tif"
d["clouds_image"] = "MASKS/" + d["image_name"] + "_CLM_R1.tif"
###################################
# App 1 : Resampling B8A (NIR) #
###################################
# The following line creates an instance of the Superimpose application
app1 = otbApplication.Registry.CreateApplication("Superimpose")
# The following lines set all the application parameters:
app1.SetParameterString("inr",str( d["input_path"] + d["B4_image"]))
app1.SetParameterString("inm",str( d["input_path"] + d["B8A_image"]))
app1.SetParameterFloat("fv",-10000)
app1.SetParameterString("out", "B8A_10m.tif")
print "Launching... Resampling"
# The following line execute the application
app1.Execute()
print "End of Resampling \n"
###################################
# App 2 : NDVI Mask #
###################################
# Create the necessary OTB Applications
app2 = otbApplication.Registry.CreateApplication("BandMath")
# The following lines set all the application parameters:
# Define Input im1: Band NIR (B8A Resampled)
app2.AddImageToParameterInputImageList("il",app1.GetParameterOutputImage("out"))
# Define Input im2: Band Red (B4)
app2.AddParameterStringList("il",str(d["input_path"] + d["B4_image"]))
app2.SetParameterString("out", "ndvi_mask.tif")
app2.SetParameterOutputImagePixelType("out", otbApplication.ImagePixelType_uint8)
app2.SetParameterString("exp", "(im1b1-im2b1)/(im1b1+im2b1)<0?1:0")
# The following line execute the application
print "Launching... BandMath : Mask ndvi "
app2.Execute()
print "End of BandMath \n"
###################################
# App 3 : Final Land/Water Mask #
###################################
# Create the necessary OTB Applications
app3 = otbApplication.Registry.CreateApplication("BandMath")
# The following lines set all the application parameters:
# Define Input im1: pre-final mask
app3.AddImageToParameterInputImageList("il",app2.GetParameterOutputImage("out"))
# Define Input im2 : No data mask
app3.AddParameterStringList("il",str(d["input_path"] + d["clouds_image"]))
app3.SetParameterString("out", "ndvi_mask_clouds.tif")
app3.SetParameterOutputImagePixelType("out", otbApplication.ImagePixelType_uint8)
# ---
# FILL THE GAP 2 : Complete the bandmath expression to apply the 255 value to
# the pixels where clouds image is different from 0 and keep
# the NDVI Mask values where clouds image is equal to zero
#
# Remember the inputs:
# - im1b1 = water mask from app2;
# - im2b1 = clouds image
#
# Exemple:
# app3.SetParameterString("exp", "??????")
app3.SetParameterString("exp", "im2b1!=0?255:im1b1")
#
# END OF GAP
# ---
# The following line execute the application
print "Launching... BandMath : Land/Water mask"
app3.Execute()
print "End of BandMath \n"
###############################
# Check: Apply NODATA => 255 value
##############################
print "Apply NODATA value = 255 \n"
###################################
# App 4 : Final Land/Water Mask #
###################################
# Create the necessary OTB Applications
app4 = otbApplication.Registry.CreateApplication("ManageNoData")
# The following lines set all the application parameters:
# Define Input im1: water mask with wrong NODATA
app4.SetParameterInputImage("in",app3.GetParameterOutputImage("out"))
# Define Input im2 : No data mask
app4.SetParameterString("mode","changevalue")
app4.SetParameterString("mode.changevalue.newv","255")
app4.SetParameterString("out", "water_mask_"+d["image_name"]+".tif")
# The following line execute the application
print "Launching... Manage No DATA : Land/Water mask"
app4.ExecuteAndWriteOutput()
print "End of BandMath \n"
#! /usr/bin/python
from sys import argv
import otbApplication
if __name__ == "__main__":
''' Resampling and thresholding of Pekel Image
- arg1 : occurrence level (0-100 values)
'''
# INPUTS
d={}
d["occurrence_name"] = "ref/occurrence_90W_0N.tif"
d["B3_image"] = "ref/B3_ref.tif"
########################################
# App 1 : Resampling Occurrence Tile #
###################################
# The following line creates an instance of the Superimpose application
app1 = otbApplication.Registry.CreateApplication("Superimpose")
# The following lines set all the application parameters:
app1.SetParameterString("inm",d["occurrence_name"])
app1.SetParameterString("inr",d["B3_image"])
app1.SetParameterString("out", "GSW_resampled.tif")
app1.SetParameterFloat("fv",255)
app1.SetParameterOutputImagePixelType("out", otbApplication.ImagePixelType_uint8)
print "Launching... Resampling"
# The following line execute the application
app1.ExecuteAndWriteOutput()
print "End of Resampling \n"
########################################
# App 2 : Thresholding Occurrence Tile #
###################################
# The following line creates an instance of the Superimpose application
app2 = otbApplication.Registry.CreateApplication("BandMath")
# The following lines set all the application parameters:
# Define Input im1: pre-final mask
app2.AddImageToParameterInputImageList("il",app1.GetParameterOutputImage("out"))
app2.SetParameterString("exp","im1b1>" + argv[1]+"?1:0")
app2.SetParameterString("out", "GSW_"+argv[1]+".tif")
app2.SetParameterOutputImagePixelType("out", otbApplication.ImagePixelType_uint8)
print "Launching... Threshold "
# The following line execute the application
app2.ExecuteAndWriteOutput()
print "End of Threshold \n"
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