You are on page 1of 7

import os

import time

import arcpy

from arcpy.sa import *

import arcgisscripting

from Tkinter import *

from ttk import Button

from tkFileDialog import*

startTime = time.time()

arcpy.env.overwriteOutput = 1

arcpy.CheckOutExtension("spatial")

# Set input stacked image and output directory if desired

inPath = r""

outPath = r""

sensors = ["Landsat 1-5 MSS", "Landsat 4-5 TM", "Landsat 7 ETM+", "Landsat 8 OLI"]

indices = ["NDVI","SAVI","EVI","NDWI","NDBI","NDSI"]

indicesSensor = {'NDVI':["Landsat 1-5 MSS","Landsat 4-5 TM","Landsat 7


ETM+","Landsat 8 OLI"],

'SAVI':["Landsat 1-5 MSS","Landsat 4-5 TM","Landsat 7 ETM+","Landsat 8


OLI"],

'EVI':["Landsat 4-5 TM","Landsat 7 ETM+","Landsat 8 OLI"],

'NDWI':["Landsat 1-5 MSS","Landsat 4-5 TM","Landsat 7 ETM+","Landsat 8


OLI"],

'NDBI':["Landsat 4-5 TM","Landsat 7 ETM+","Landsat 8 OLI"],

'NDSI':["Landsat 1-5 MSS","Landsat 4-5 TM","Landsat 7 ETM+","Landsat 8


OLI"]}

root = Tk()
root.title("INDICES CALCULATION")

Label(root, text="Sensor", font="Times 14 bold").grid(row=1,column=1, sticky=W)

Label(root, text="Indices", font="Times 14 bold").grid(row=1,column=10, sticky=W)

#set checkbutton

checkbutton = list(range(len(indices)))

cbVar = list(range(len(indices)))

sensorow=2

for i, indice in enumerate(indices): #enumerate is builtin function for each element in cursor
, a tuple is produced with (counter, element)

cbVar[i] = IntVar()

checkbutton[i] = Checkbutton(root, text=indice, variable=cbVar[i])

checkbutton[i].grid(row=sensorow, column=10, pady=4, padx=4, sticky=W)

sensorow += 1

#set radiobutton

sensorow = 2

radiobutton= list(range(len(sensors)))

rbVar = StringVar()

for i, sensor in enumerate(sensors):

radiobutton[i] = Radiobutton(root, text=sensor, variable= rbVar, value=sensor)

radiobutton[i].grid(row=sensorow, column=1, pady=4, padx=7, sticky=W)

rbVar.set("Landsat 8 OLI")

sensorow += 1

#input directory

rowpos=10

Label(root, text="Input Stacked Image").grid(row=rowpos,column=1, sticky=W)


rowpos += 1

indirVar = StringVar()

indirEntry = Entry(root, textvariable = indirVar, width=50)

indirEntry.insert(INSERT, inPath)

indirEntry.grid(row=rowpos,column=0, sticky=W, columnspan=8, padx=5)

def input_directory():

inPath = askopenfilename(parent=root, title="Select Stacked Input Image")

if len(inPath) > 0:

indirEntry.delete(0, END)

indirEntry.insert(INSERT, inPath)

directoryButton1 = Button(root, text = 'Browse',


command=input_directory).grid(row=rowpos, column=55, pady=2, padx=2)

#output directory

rowpos += 1

Label(root, text="Output Directory").grid(row=rowpos,column=1, sticky=W)

rowpos += 1

outdirVar = StringVar()

outdirEntry = Entry(root, textvariable = outdirVar, width=50)

outdirEntry.insert(INSERT, outPath)

outdirEntry.grid(row=rowpos, column=1, sticky=W, columnspan=8, padx=5)

def output_directory():

outPath= askdirectory(parent=root, title="Select Root Output Directory")

if len(outPath) > 0:
outdirEntry.insert(INSERT, outPath)

directoryButton2 = Button(root, text = 'Browse',


command=output_directory).grid(row=rowpos, column=55, pady=2, padx=2)

rowpos += 1

# Run Button

def callback():

root.destroy()

Button(root, text = 'Run', command=callback).grid(row=rowpos, column=3, pady=4, padx=4)

# Quit Button

def quitbutton():

print "Processing cancelled"

root.destroy()

raise SystemExit(0)

Button(root, text='Quit', command=quitbutton).grid(row=rowpos, column=2,


pady=4,padx=4)

root.mainloop()

#end of GUI..............

# Set variables selected from GUI

#the radio button variable is called using get method which is defined in variable sensor.
similaly in path and outpath variable are defined.

Sensor = rbVar.get()

inPath = indirVar.get()

outPath = outdirVar.get()

#Set the workspace environment setting

arcpy.env.workspace = inPath

#os.makedirs() method will create all unavailable/missing directory in the specified path.

if not (outPath and os.path.exists(outPath)): os.makedirs(outPath) # Create output directory if


it doesn't exist
#os.path.split() method in Python is used to Split the path name into a pair head and tail.

#Here, tail is the last path name component and head is everything leading up to that.

pathRoot, inRaster = os.path.split(inPath)

# the processing is printed once path name is given.

print "Processing", inRaster

# For different bands based on sensor

#We have Created a Describe object

d = arcpy.Describe(inPath)

#Here the children method of Describe class returns the layer objects in an array for different
bands for different sensors.

#if sensor is lansat 1-5 mss it returns following four bands.

if Sensor == "Landsat 1-5 MSS":

Green = Raster(d.children[0].name)

Red = Raster(d.children[1].name)

NIR1 = Raster(d.children[2].name)

NIR2 = Raster(d.children[3].name)

if Sensor == "Landsat 4-5 TM" or Sensor == "Landsat 7 ETM+":

Blue = Raster(d.children[0].name)

Green = Raster(d.children[1].name)

Red = Raster(d.children[2].name)

NIR1 = Raster(d.children[3].name)

SWIR1 = Raster(d.children[4].name)

SWIR2 = Raster(d.children[5].name)

if Sensor == "Landsat 8 OLI":


Coastal = Raster(d.children[0].name)

Blue = Raster(d.children[1].name)

Green = Raster(d.children[2].name)

Red = Raster(d.children[3].name)

NIR1 = Raster(d.children[4].name)

SWIR1 = Raster(d.children[5].name)

SWIR2 = Raster(d.children[6].name)

# Calculate and save indices

# iteritems(): returns an iterator of the dictionary's list in the form of (key, value) tuple pairs.

for key, value in indicesSensor.iteritems():

#We have defined and stored the formulas for each indices.

NDVI = (NIR1 - Red)*100/(NIR1 + Red)

EVI = 2.5*((NIR1 - Red)/(NIR1 + 6 * Red - 7.5 * Blue + 1))

NDWI = (Green - NIR1)*100/(Green + NIR1)

SAVI = ((NIR1 - Red)/(NIR1 + Red + 0.5)) * (1 + 0.5)

NDSI =(Green - NIR1)*100/(Green + NIR1)

NDBI = (SWIR1 - NIR1)*100/(SWIR1 + NIR1)

if Sensor in value:

#cb var is called using get method which gives key as output

if cbVar[indices.index(key)].get():

formula=key

print key

#The formula is evaluated and index raster is saved.

eval(formula).save(outPath + "/" + inRaster[:-4] + "_" + key + ".tif") # Save index


raster

# the total execution time is calculated and printed by subtracting the start from the end and
completion time is calculated.

#initialize the start variable that contains the starting time using the time()
endTime = time.time() - startTime

print "Completed in", ("%.2f" % endTime), 'seconds.'

You might also like