Processing.py 3.5 KB
Newer Older
Commandre Benjamin's avatar
Commandre Benjamin committed
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import configparser
import datetime
7
import glob
Commandre Benjamin's avatar
Commandre Benjamin committed
8
9
10
11
12
13
14
from osgeo import gdal

import otbApplication as otb

from app import Archive

def str2bool(v):
15
  return v.lower() in ("true")
Commandre Benjamin's avatar
Commandre Benjamin committed
16
17

class Processing(object):
18

Commandre Benjamin's avatar
Commandre Benjamin committed
19
20
	def __init__(self):
		pass
21

Commandre Benjamin's avatar
Commandre Benjamin committed
22
23
24
25
26
27
28
29
	def i_download(self):
		"""
			Méthode pour télécharger les images sur le site Theia Land.
		"""

		if not self.annee_fin :
			self.annee_fin = datetime.datetime.now().year

Commandre Benjamin's avatar
Commandre Benjamin committed
30
31
		self.check_download = Archive.Archive(self.capteur, self.bandes, self.niveau, self.emprise, self.zone_etude,\
											  self.resultats, self.annee_debut, int(self.annee_fin), self.seuil_nuage)
Commandre Benjamin's avatar
Commandre Benjamin committed
32
		self.check_download.listing()
33
		self.check_download.download_auto(self.id, self.mdp, self.proxy, extraction=self.extraction)
Commandre Benjamin's avatar
Commandre Benjamin committed
34
35
36
37

		self.liste_dossier = dict()

		for annee in os.listdir(self.resultats):
38
			with open("{}/{}/dates.txt".format(self.resultats, annee), "w") as fichier :
Commandre Benjamin's avatar
Commandre Benjamin committed
39
40
41
42
43
44
				for image in sorted(glob.glob("{}/{}/Images/*".format(self.resultats, annee))) :
					fichier.write("{}\n".format(os.path.basename(image)[:-4]))

			self.liste_dossier[annee] = sorted([x for x in glob.glob("{}/{}/Images/*".format(self.resultats, annee)) if x.endswith(".tif")])

	def otbPhenologie(self):
45
46
47
		"""
			Appel le module OTB : 'SigmoFitting' sur l'image composée de l'empilement des NDVI
		"""
Commandre Benjamin's avatar
Commandre Benjamin committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

		otb_Phenologie = otb.Registry.CreateApplication("SigmoFitting")
		otb_Phenologie.SetParameterString("mode", "metrics")

		for annee in self.liste_dossier :
			otb_Phenologie.SetParameterString("in", "{}/{}/NDVI/stack_ndvi.tif".format(self.resultats, annee))
			otb_Phenologie.SetParameterString("dates", "{}/{}/dates.txt".format(self.resultats, annee))
			otb_Phenologie.SetParameterString("out", "{}/{}/metrics.tif".format(self.resultats, annee))

			otb_Phenologie.ExecuteAndWriteOutput()

	def calcul_ndvi(self):
		"""
			Méthode effectuant le calcul du NDVI via le module OTB : 'RadiometricIndices'
		"""

64
		options_vrt	= gdal.ParseCommandLine('-resolution highest -separate')
65

Commandre Benjamin's avatar
Commandre Benjamin committed
66
67
68
69
70
71
72
73
		otb_NDVI = otb.Registry.CreateApplication("RadiometricIndices")
		otb_NDVI.SetParameterInt("channels.blue", 1)
		otb_NDVI.SetParameterInt("channels.green", 2)
		otb_NDVI.SetParameterInt("channels.red", 3)
		otb_NDVI.SetParameterInt("channels.nir", 4)
		otb_NDVI.SetParameterStringList("list", ["Vegetation:NDVI"])

		sauvegarde = configparser.ConfigParser()
74

Commandre Benjamin's avatar
Commandre Benjamin committed
75
76
77
78
79
80
81
82
83
84
85
86
		for annee in self.liste_dossier :

			sauvegarde.read("{}/{}/sauvegarde.ini".format(self.resultats, annee))
			dossier_NDVI = "{}/{}/NDVI".format(self.resultats, annee)

			if not os.path.exists(dossier_NDVI) :
				os.makedirs(dossier_NDVI)

			for img in self.liste_dossier[annee] :

				chemin_ndvi = "{}/ndvi_{}".format(dossier_NDVI, os.path.basename(img))

87
				if not str2bool(sauvegarde[os.path.basename(img)[:-4]]["NDVI"]):
Commandre Benjamin's avatar
Commandre Benjamin committed
88
89
90
91
92
					otb_NDVI.SetParameterString("in", img)
					otb_NDVI.SetParameterString("out", chemin_ndvi)
					otb_NDVI.ExecuteAndWriteOutput()
					sauvegarde[os.path.basename(img)[:-4]]["NDVI"] = "True"

93
94
			liste_ndvi = sorted([x for x in glob.glob("{}/*".format(dossier_NDVI)) if x.endswith(".tif") and "stack" not in x])
			vrt = gdal.BuildVRT("", liste_ndvi, options=options_vrt)
Commandre Benjamin's avatar
Commandre Benjamin committed
95
96
97
98
99
			gdal.Translate("{}/{}/NDVI/stack_ndvi.tif".format(self.resultats, annee), vrt)

			with open("{}/{}/sauvegarde.ini".format(self.resultats, annee), 'w') as configfile:
				sauvegarde.write(configfile)

100
	def i_images_processing(self):
Commandre Benjamin's avatar
Commandre Benjamin committed
101
		"""
102
			Calul le ndvi, fusionnne en une seule image puis lance le module OTBPhenology
Commandre Benjamin's avatar
Commandre Benjamin committed
103
		"""
104
		self.calcul_ndvi()
105
		self.otbPhenologie()