joo gustavo a amorim email joaogustavoamorimgmail com coding date jan 2019 pythonblack true imports class implemented to calculus the index class summary this algorithm consists in calculating vegetation indices these indices can be used for precision agriculture for example or remote sensing there are functions to define the data and to calculate the implemented indices vegetation index https en wikipedia orgwikivegetationindex a vegetation index vi is a spectral transformation of two or more bands designed to enhance the contribution of vegetation properties and allow reliable spatial and temporal intercomparisons of terrestrial photosynthetic activity and canopy structural variations information about channels wavelength range for each nir nearinfrared https www malvernpanalytical combrproductstechnologynearinfraredspectroscopy wavelength range 700 nm to 2500 nm red edge https en wikipedia orgwikirededge wavelength range 680 nm to 730 nm red https en wikipedia orgwikicolor wavelength range 635 nm to 700 nm blue https en wikipedia orgwikicolor wavelength range 450 nm to 490 nm green https en wikipedia orgwikicolor wavelength range 520 nm to 560 nm implemented index list abbreviationofindexname list of channels used arvi2 red nir ccci red rededge nir cvi red green nir gli red green blue ndvi red nir bndvi blue nir rededgendvi red rededge gndvi green nir gbndvi green blue nir grndvi red green nir rbndvi red blue nir pndvi red green blue nir atsavi red nir bwdrvi blue nir cigreen green nir cirededge rededge nir ci red blue ctvi red nir gdvi green nir evi red blue nir gemi red nir gosavi green nir gsavi green nir hue red green blue ivi red nir ipvi red nir i red green blue rvi red nir mrvi red nir msavi red nir normg red green nir normnir red green nir normr red green nir ngrdi red green ri red green s red green blue if red green blue dvi red nir tvi red nir ndre rededge nir list of all index implemented allindex arvi2 ccci cvi gli ndvi bndvi rededgendvi gndvi gbndvi grndvi rbndvi pndvi atsavi bwdrvi cigreen cirededge ci ctvi gdvi evi gemi gosavi gsavi hue ivi ipvi i rvi mrvi msavi normg normnir normr ngrdi ri s if dvi tvi ndre list of index with not blue channel notblueindex arvi2 ccci cvi ndvi rededgendvi gndvi grndvi atsavi cigreen cirededge ctvi gdvi gemi gosavi gsavi ivi ipvi rvi mrvi msavi normg normnir normr ngrdi ri dvi tvi ndre list of index just with rgb channels rgbindex gli ci hue i ngrdi ri s if performs the calculation of the index with the values instantiated in the class str index abbreviation of index name to perform atmospherically resistant vegetation index 2 https www indexdatabase dedbisingle php id396 return index 0 181 17self nirself redself nirself red canopy chlorophyll content index https www indexdatabase dedbisingle php id224 return index chlorophyll vegetation index https www indexdatabase dedbisingle php id391 return index self green leaf index https www indexdatabase dedbisingle php id375 return index normalized difference self nirself red normalized difference vegetation index calibrated ndvi cdvi https www indexdatabase dedbisingle php id58 return index normalized difference self nirself blue self bluenormalized difference vegetation index https www indexdatabase dedbisingle php id135 return index normalized difference self rededgeself red https www indexdatabase dedbisingle php id235 return index normalized difference self nirself green self green ndvi https www indexdatabase dedbisingle php id401 return index self greenself blue ndvi https www indexdatabase dedbisingle php id186 return index self greenself red ndvi https www indexdatabase dedbisingle php id185 return index self redself blue ndvi https www indexdatabase dedbisingle php id187 return index pan ndvi https www indexdatabase dedbisingle php id188 return index adjusted transformed soiladjusted vi https www indexdatabase dedbisingle php id209 return index self bluewide dynamic range vegetation index https www indexdatabase dedbisingle php id136 return index chlorophyll index self green https www indexdatabase dedbisingle php id128 return index chlorophyll index self rededge https www indexdatabase dedbisingle php id131 return index coloration index https www indexdatabase dedbisingle php id11 return index corrected transformed vegetation index https www indexdatabase dedbisingle php id244 return index difference self nirself green self green difference vegetation index https www indexdatabase dedbisingle php id27 return index enhanced vegetation index https www indexdatabase dedbisingle php id16 return index global environment monitoring index https www indexdatabase dedbisingle php id25 return index self green optimized soil adjusted vegetation index https www indexdatabase dedbisingle php id29 mit y 0 16 return index self green soil adjusted vegetation index https www indexdatabase dedbisingle php id31 mit n 0 5 return index hue https www indexdatabase dedbisingle php id34 return index ideal vegetation index https www indexdatabase dedbisingle php id276 bintercept of vegetation line asoil line slope return index infraself red percentage vegetation index https www indexdatabase dedbisingle php id35 return index intensity https www indexdatabase dedbisingle php id36 return index ratiovegetationindex http www seosproject eumodulesremotesensingremotesensingc03s01p01 html return index modified normalized difference vegetation index rvi https www indexdatabase dedbisingle php id275 return index modified soil adjusted vegetation index https www indexdatabase dedbisingle php id44 return index norm g https www indexdatabase dedbisingle php id50 return index norm self nir https www indexdatabase dedbisingle php id51 return index norm r https www indexdatabase dedbisingle php id52 return index normalized difference self greenself red normalized self green self red difference index visible atmospherically resistant indices self green viself green https www indexdatabase dedbisingle php id390 return index normalized difference self redself green self redness index https www indexdatabase dedbisingle php id74 return index saturation https www indexdatabase dedbisingle php id77 return index shape index https www indexdatabase dedbisingle php id79 return index simple ratio self nirself red difference vegetation index vegetation index number vin https www indexdatabase dedbisingle php id12 return index transformed vegetation index https www indexdatabase dedbisingle php id98 return index genering a random matrices to test this class red np ones1000 1000 1 dtypefloat64 46787 green np ones1000 1000 1 dtypefloat64 23487 blue np ones1000 1000 1 dtypefloat64 14578 rededge np ones1000 1000 1 dtypefloat64 51045 nir np ones1000 1000 1 dtypefloat64 52200 examples of how to use the class instantiating the class cl indexcalculation instantiating the class with the values cl indexcalculationredred greengreen blueblue rededgerededge nirnir how set the values after instantiate the class cl for update the data or when don t instantiating the class with the values cl setmatricesredred greengreen blueblue rededgerededge nirnir calculating the indices for the instantiated values in the class note the ccci index can be changed to any index implemented in the class indexvalueform1 cl calculationccci redred greengreen blueblue rededgerededge nirnir astypenp float64 indexvalueform2 cl ccci calculating the index with the values directly you can set just the values preferred note the calculation function performs the function setmatrices indexvalueform3 cl calculationccci redred greengreen blueblue rededgerededge nirnir astypenp float64 printform 1 np array2stringindexvalueform1 precision20 separator floatmode maxprecequal printform 2 np array2stringindexvalueform2 precision20 separator floatmode maxprecequal printform 3 np array2stringindexvalueform3 precision20 separator floatmode maxprecequal a list of examples results for different type of data at ndvi float16 0 31567383 ndvi red 50 nir 100 float32 0 31578946 ndvi red 50 nir 100 float64 0 3157894736842105 ndvi red 50 nir 100 longdouble 0 3157894736842105 ndvi red 50 nir 100 joão gustavo a amorim email joaogustavoamorim gmail com coding date jan 2019 python black true imports class implemented to calculus the index class summary this algorithm consists in calculating vegetation indices these indices can be used for precision agriculture for example or remote sensing there are functions to define the data and to calculate the implemented indices vegetation index https en wikipedia org wiki vegetation_index a vegetation index vi is a spectral transformation of two or more bands designed to enhance the contribution of vegetation properties and allow reliable spatial and temporal inter comparisons of terrestrial photosynthetic activity and canopy structural variations information about channels wavelength range for each nir near infrared https www malvernpanalytical com br products technology near infrared spectroscopy wavelength range 700 nm to 2500 nm red edge https en wikipedia org wiki red_edge wavelength range 680 nm to 730 nm red https en wikipedia org wiki color wavelength range 635 nm to 700 nm blue https en wikipedia org wiki color wavelength range 450 nm to 490 nm green https en wikipedia org wiki color wavelength range 520 nm to 560 nm implemented index list abbreviationofindexname list of channels used arvi2 red nir ccci red rededge nir cvi red green nir gli red green blue ndvi red nir bndvi blue nir rededgendvi red rededge gndvi green nir gbndvi green blue nir grndvi red green nir rbndvi red blue nir pndvi red green blue nir atsavi red nir bwdrvi blue nir cigreen green nir cirededge rededge nir ci red blue ctvi red nir gdvi green nir evi red blue nir gemi red nir gosavi green nir gsavi green nir hue red green blue ivi red nir ipvi red nir i red green blue rvi red nir mrvi red nir msavi red nir normg red green nir normnir red green nir normr red green nir ngrdi red green ri red green s red green blue if red green blue dvi red nir tvi red nir ndre rededge nir list of all index implemented allindex arvi2 ccci cvi gli ndvi bndvi rededgendvi gndvi gbndvi grndvi rbndvi pndvi atsavi bwdrvi cigreen cirededge ci ctvi gdvi evi gemi gosavi gsavi hue ivi ipvi i rvi mrvi msavi normg normnir normr ngrdi ri s if dvi tvi ndre list of index with not blue channel notblueindex arvi2 ccci cvi ndvi rededgendvi gndvi grndvi atsavi cigreen cirededge ctvi gdvi gemi gosavi gsavi ivi ipvi rvi mrvi msavi normg normnir normr ngrdi ri dvi tvi ndre list of index just with rgb channels rgbindex gli ci hue i ngrdi ri s if performs the calculation of the index with the values instantiated in the class str index abbreviation of index name to perform atmospherically resistant vegetation index 2 https www indexdatabase de db i single php id 396 return index 0 18 1 17 self nir self red self nir self red canopy chlorophyll content index https www indexdatabase de db i single php id 224 return index chlorophyll vegetation index https www indexdatabase de db i single php id 391 return index self green leaf index https www indexdatabase de db i single php id 375 return index normalized difference self nir self red normalized difference vegetation index calibrated ndvi cdvi https www indexdatabase de db i single php id 58 return index normalized difference self nir self blue self blue normalized difference vegetation index https www indexdatabase de db i single php id 135 return index normalized difference self rededge self red https www indexdatabase de db i single php id 235 return index normalized difference self nir self green self green ndvi https www indexdatabase de db i single php id 401 return index self green self blue ndvi https www indexdatabase de db i single php id 186 return index self green self red ndvi https www indexdatabase de db i single php id 185 return index self red self blue ndvi https www indexdatabase de db i single php id 187 return index pan ndvi https www indexdatabase de db i single php id 188 return index adjusted transformed soil adjusted vi https www indexdatabase de db i single php id 209 return index self blue wide dynamic range vegetation index https www indexdatabase de db i single php id 136 return index chlorophyll index self green https www indexdatabase de db i single php id 128 return index chlorophyll index self rededge https www indexdatabase de db i single php id 131 return index coloration index https www indexdatabase de db i single php id 11 return index corrected transformed vegetation index https www indexdatabase de db i single php id 244 return index difference self nir self green self green difference vegetation index https www indexdatabase de db i single php id 27 return index enhanced vegetation index https www indexdatabase de db i single php id 16 return index global environment monitoring index https www indexdatabase de db i single php id 25 return index self green optimized soil adjusted vegetation index https www indexdatabase de db i single php id 29 mit y 0 16 return index self green soil adjusted vegetation index https www indexdatabase de db i single php id 31 mit n 0 5 return index hue https www indexdatabase de db i single php id 34 return index ideal vegetation index https www indexdatabase de db i single php id 276 b intercept of vegetation line a soil line slope return index infraself red percentage vegetation index https www indexdatabase de db i single php id 35 return index intensity https www indexdatabase de db i single php id 36 return index ratio vegetation index http www seos project eu modules remotesensing remotesensing c03 s01 p01 html return index modified normalized difference vegetation index rvi https www indexdatabase de db i single php id 275 return index modified soil adjusted vegetation index https www indexdatabase de db i single php id 44 return index norm g https www indexdatabase de db i single php id 50 return index norm self nir https www indexdatabase de db i single php id 51 return index norm r https www indexdatabase de db i single php id 52 return index normalized difference self green self red normalized self green self red difference index visible atmospherically resistant indices self green viself green https www indexdatabase de db i single php id 390 return index normalized difference self red self green self redness index https www indexdatabase de db i single php id 74 return index saturation https www indexdatabase de db i single php id 77 return index shape index https www indexdatabase de db i single php id 79 return index simple ratio self nir self red difference vegetation index vegetation index number vin https www indexdatabase de db i single php id 12 return index transformed vegetation index https www indexdatabase de db i single php id 98 return index genering a random matrices to test this class red np ones 1000 1000 1 dtype float64 46787 green np ones 1000 1000 1 dtype float64 23487 blue np ones 1000 1000 1 dtype float64 14578 rededge np ones 1000 1000 1 dtype float64 51045 nir np ones 1000 1000 1 dtype float64 52200 examples of how to use the class instantiating the class cl indexcalculation instantiating the class with the values cl indexcalculation red red green green blue blue rededge rededge nir nir how set the values after instantiate the class cl for update the data or when don t instantiating the class with the values cl setmatrices red red green green blue blue rededge rededge nir nir calculating the indices for the instantiated values in the class note the ccci index can be changed to any index implemented in the class indexvalue_form1 cl calculation ccci red red green green blue blue rededge rededge nir nir astype np float64 indexvalue_form2 cl ccci calculating the index with the values directly you can set just the values preferred note the calculation function performs the function setmatrices indexvalue_form3 cl calculation ccci red red green green blue blue rededge rededge nir nir astype np float64 print form 1 np array2string indexvalue_form1 precision 20 separator floatmode maxprec_equal print form 2 np array2string indexvalue_form2 precision 20 separator floatmode maxprec_equal print form 3 np array2string indexvalue_form3 precision 20 separator floatmode maxprec_equal a list of examples results for different type of data at ndvi float16 0 31567383 ndvi red 50 nir 100 float32 0 31578946 ndvi red 50 nir 100 float64 0 3157894736842105 ndvi red 50 nir 100 longdouble 0 3157894736842105 ndvi red 50 nir 100
import numpy as np



class IndexCalculation:
    

    def __init__(self, red=None, green=None, blue=None, red_edge=None, nir=None):
        self.set_matricies(red=red, green=green, blue=blue, red_edge=red_edge, nir=nir)

    def set_matricies(self, red=None, green=None, blue=None, red_edge=None, nir=None):
        if red is not None:
            self.red = red
        if green is not None:
            self.green = green
        if blue is not None:
            self.blue = blue
        if red_edge is not None:
            self.redEdge = red_edge
        if nir is not None:
            self.nir = nir
        return True

    def calculation(
        self, index="", red=None, green=None, blue=None, red_edge=None, nir=None
    ):
        
        self.set_matricies(red=red, green=green, blue=blue, red_edge=red_edge, nir=nir)
        funcs = {
            "ARVI2": self.arv12,
            "CCCI": self.ccci,
            "CVI": self.cvi,
            "GLI": self.gli,
            "NDVI": self.ndvi,
            "BNDVI": self.bndvi,
            "redEdgeNDVI": self.red_edge_ndvi,
            "GNDVI": self.gndvi,
            "GBNDVI": self.gbndvi,
            "GRNDVI": self.grndvi,
            "RBNDVI": self.rbndvi,
            "PNDVI": self.pndvi,
            "ATSAVI": self.atsavi,
            "BWDRVI": self.bwdrvi,
            "CIgreen": self.ci_green,
            "CIrededge": self.ci_rededge,
            "CI": self.ci,
            "CTVI": self.ctvi,
            "GDVI": self.gdvi,
            "EVI": self.evi,
            "GEMI": self.gemi,
            "GOSAVI": self.gosavi,
            "GSAVI": self.gsavi,
            "Hue": self.hue,
            "IVI": self.ivi,
            "IPVI": self.ipvi,
            "I": self.i,
            "RVI": self.rvi,
            "MRVI": self.mrvi,
            "MSAVI": self.m_savi,
            "NormG": self.norm_g,
            "NormNIR": self.norm_nir,
            "NormR": self.norm_r,
            "NGRDI": self.ngrdi,
            "RI": self.ri,
            "S": self.s,
            "IF": self._if,
            "DVI": self.dvi,
            "TVI": self.tvi,
            "NDRE": self.ndre,
        }

        try:
            return funcs[index]()
        except KeyError:
            print("Index not in the list!")
            return False

    def arv12(self):
        
        return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))

    def ccci(self):
        
        return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / (
            (self.nir - self.red) / (self.nir + self.red)
        )

    def cvi(self):
        
        return self.nir * (self.red / (self.green**2))

    def gli(self):
        
        return (2 * self.green - self.red - self.blue) / (
            2 * self.green + self.red + self.blue
        )

    def ndvi(self):
        
        return (self.nir - self.red) / (self.nir + self.red)

    def bndvi(self):
        
        return (self.nir - self.blue) / (self.nir + self.blue)

    def red_edge_ndvi(self):
        
        return (self.redEdge - self.red) / (self.redEdge + self.red)

    def gndvi(self):
        
        return (self.nir - self.green) / (self.nir + self.green)

    def gbndvi(self):
        
        return (self.nir - (self.green + self.blue)) / (
            self.nir + (self.green + self.blue)
        )

    def grndvi(self):
        
        return (self.nir - (self.green + self.red)) / (
            self.nir + (self.green + self.red)
        )

    def rbndvi(self):
        
        return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red))

    def pndvi(self):
        
        return (self.nir - (self.green + self.red + self.blue)) / (
            self.nir + (self.green + self.red + self.blue)
        )

    def atsavi(self, x=0.08, a=1.22, b=0.03):
        
        return a * (
            (self.nir - a * self.red - b)
            / (a * self.nir + self.red - a * b + x * (1 + a**2))
        )

    def bwdrvi(self):
        
        return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue)

    def ci_green(self):
        
        return (self.nir / self.green) - 1

    def ci_rededge(self):
        
        return (self.nir / self.redEdge) - 1

    def ci(self):
        
        return (self.red - self.blue) / self.red

    def ctvi(self):
        
        ndvi = self.ndvi()
        return ((ndvi + 0.5) / (abs(ndvi + 0.5))) * (abs(ndvi + 0.5) ** (1 / 2))

    def gdvi(self):
        
        return self.nir - self.green

    def evi(self):
        
        return 2.5 * (
            (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1)
        )

    def gemi(self):
        
        n = (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / (
            self.nir + self.red + 0.5
        )
        return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red)

    def gosavi(self, y=0.16):
        
        return (self.nir - self.green) / (self.nir + self.green + y)

    def gsavi(self, n=0.5):
        
        return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n)

    def hue(self):
        
        return np.arctan(
            ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue)
        )

    def ivi(self, a=None, b=None):
        
        return (self.nir - b) / (a * self.red)

    def ipvi(self):
        
        return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1)

    def i(self):
        
        return (self.red + self.green + self.blue) / 30.5

    def rvi(self):
        
        return self.nir / self.red

    def mrvi(self):
        
        return (self.rvi() - 1) / (self.rvi() + 1)

    def m_savi(self):
        
        return (
            (2 * self.nir + 1)
            - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2)
        ) / 2

    def norm_g(self):
        
        return self.green / (self.nir + self.red + self.green)

    def norm_nir(self):
        
        return self.nir / (self.nir + self.red + self.green)

    def norm_r(self):
        
        return self.red / (self.nir + self.red + self.green)

    def ngrdi(self):
        
        return (self.green - self.red) / (self.green + self.red)

    def ri(self):
        
        return (self.red - self.green) / (self.red + self.green)

    def s(self):
        
        max_value = np.max([np.max(self.red), np.max(self.green), np.max(self.blue)])
        min_value = np.min([np.min(self.red), np.min(self.green), np.min(self.blue)])
        return (max_value - min_value) / max_value

    def _if(self):
        
        return (2 * self.red - self.green - self.blue) / (self.green - self.blue)

    def dvi(self):
        
        return self.nir / self.red

    def tvi(self):
        
        return (self.ndvi() + 0.5) ** (1 / 2)

    def ndre(self):
        return (self.nir - self.redEdge) / (self.nir + self.redEdge)