Hello,
I am trying to use various otb applications within a python class that I made.
Most of it works fine except the following: ManagenoData.
For some reason it tries to write my file to the followin location
“RuntimeError: Cannot open image /home/jovyan/data_agata/1966/Images/corona_30061966/corona_30061966. The file does not exist.”
However the output file it should write it to is
index=“/home/jovyan/data_agata/1966/Indices/corona_30061966_haralick_21.tif” fed into
app.SetParameterString(“out”,index)
I have cheked the value of index many times, and is indeed what i want it to be.
Note that the compute_index() and haralick() functions of the following code work well. My Haralick indices are correctly computed. However it is on when, whthin the Haralick() method, i call for change_ndv() that i have that unexpected output path.
-----Error Message
2023-04-28 10:18:14 (INFO) ManageNoData: OTB will use at most 32 threads
Traceback (most recent call last):
File "textural_indices_calculations.py", line 236, in <module>
calcul.compute_index(fname)
File "textural_indices_calculations.py", line 229, in compute_index
self.haralick(filename,1,'',fen) #2 min avec hlick_rad = 11
File "textural_indices_calculations.py", line 125, in haralick
self.ndv_change(index=sortie)
File "textural_indices_calculations.py", line 50, in ndv_change
app.ExecuteAndWriteOutput()
File "/opt/anaconda/envs/env_otb/conda-otb/lib/python/otbApplication.py", line 2801, in ExecuteAndWriteOutput
return _otbApplication.Application_ExecuteAndWriteOutput(self)
-------------- Full code
I have commented the computing part of the haralick function beaucase it takes time, it is running without issues.
class texture:
def __init__(self,filename):
year=filename[-4:]
self.path_corona="/home/jovyan/data_agata/"+str(year)+"/Images/"+str(filename)+"/"
self.path_mask="/home/jovyan/data_agata/"+str(year)+"/Images/"+str(filename)+"/" +str(filename)
self.path_mnt='/home/jovyan/data_agata/MNT'
self.path_indices = "/home/jovyan/data_agata/"+str(year)+"/Indices/"
self.raster_fname=str(filename)
self.corrected_raster=self.path_corona+str(filename)+'.tif'
#------------------ Masking
def ndv_change(self,index):
app=None
app = otb.Registry.CreateApplication("ManageNoData")
mask = self.path_mask
#Je n'explique pas pq cette variable n'est pas prise en compte dans la suite sur write output
app.SetParameterString("in",index)
app.SetParameterString("mode","apply")
app.SetParameterString("mode.apply.mask",mask)
app.SetParameterFloat("mode.apply.ndval", 0)
app.SetParameterString("out",index)
print (index)
app.ExecuteAndWriteOutput()
app=None
app = otb.Registry.CreateApplication("ManageNoData")
#fixe ndv à -9999
app.SetParameterString("in",index)
app.SetParameterString("out",index)
app.SetParameterString("mode","changevalue")
app.SetParameterFloat("mode.changevalue.newv",-9999)
app.ExecuteAndWriteOutput()
return
#------------------ Calculs des différents indices sur l'image CORONA (composée et clippé) ------------------
def bin_val(self,fenetre):
nb_bin=int(184-8*fenetre)
if nb_bin>=8:
return nb_bin
else:
return int(8)
#Bande: renseigner 1, 2 ,3 ou 4 pour les bandes 4,5,6, ou è respectivement
#fenetre: int
def haralick(self,filename,bande,nom_bande,fenetre):
entree= self.corrected_raster
#sortie A changer pour aboutir dans dossier INDICES + ajouter taille radius
sortie=self.path_indices + str(filename)+'_haralick_{}.tif'.format(fenetre)
# app=None
# app = otb.Registry.CreateApplication("HaralickTextureExtraction")
#
# #Entrée
# app.SetParameterString("in", entree)
#
# #Bande sur laquelle c'est calculé
# app.SetParameterInt("channel",bande)
#
# #Borner les indices d'Haralick (extraire max er min)
# bande_objet=rasterio.open(entree).read(bande)
#
# app.SetParameterInt("parameters.nbbin",self.bin_val(fenetre=fenetre))
# app.SetParameterFloat("parameters.min",float(bande_objet.mean()-2*bande_objet.std()))
# app.SetParameterFloat("parameters.max",float(bande_objet.mean()+2*bande_objet.std()))
#
# #Taille de la fenêtre
# app.SetParameterInt("parameters.xrad", fenetre)
# app.SetParameterInt("parameters.yrad", fenetre)
# #Offset, c'est quoi ?
#
# #Choix des indices ---> Lequel choisir
# app.SetParameterString("texture","simple")
#
# app.SetParameterString("out", sortie)
#
# app.ExecuteAndWriteOutput()
#On réaligne les étendues si décalage
ds_ref=gdal.Open(entree)
gt_ref=ds_ref.GetGeoTransform
ulx, xres, xskew, uly, yskew, yres = ds_ref.GetGeoTransform()
lrx = ulx + (ds_ref.RasterXSize * xres)
lry = uly + (ds_ref.RasterYSize * yres)
commande="/opt/anaconda/envs/env_otb/bin/gdal_edit.py -a_srs epsg:32718 -a_ullr {ulx} {uly} {lrx} {lry} {sortie}".format(ulx=ulx,uly=uly,lrx=lrx,lry=lry,sortie=str(sortie))
os.system(commande)
ds_ref=None
self.ndv_change(index=sortie)
return
###############Execution
#deja fait pour 1,5,11,21,35,
# fototex en block, pas assez de mémoire pour moving window à 35 px-> On LAISSE tomber
# fototex ne marfche pas pour 1 et 5
def compute_index(self,filename):
win_sizes=[21]
print("haralick pour",fen)
self.haralick(filename,1,'',fen) #2 min avec hlick_rad = 11
calcul=texture(fname)
calcul.compute_index(fname)