Professional Documents
Culture Documents
Python Mini Project
Python Mini Project
import time
import arcpy
import arcgisscripting
startTime = time.time()
arcpy.env.overwriteOutput = 1
arcpy.CheckOutExtension("spatial")
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"]
root = Tk()
root.title("INDICES CALCULATION")
#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()
sensorow += 1
#set radiobutton
sensorow = 2
radiobutton= list(range(len(sensors)))
rbVar = StringVar()
rbVar.set("Landsat 8 OLI")
sensorow += 1
#input directory
rowpos=10
indirVar = StringVar()
indirEntry.insert(INSERT, inPath)
def input_directory():
if len(inPath) > 0:
indirEntry.delete(0, END)
indirEntry.insert(INSERT, inPath)
#output directory
rowpos += 1
rowpos += 1
outdirVar = StringVar()
outdirEntry.insert(INSERT, outPath)
def output_directory():
if len(outPath) > 0:
outdirEntry.insert(INSERT, outPath)
rowpos += 1
# Run Button
def callback():
root.destroy()
# Quit Button
def quitbutton():
root.destroy()
raise SystemExit(0)
root.mainloop()
#end of 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()
arcpy.env.workspace = inPath
#os.makedirs() method will create all unavailable/missing directory in the specified path.
#Here, tail is the last path name component and head is everything leading up to that.
d = arcpy.Describe(inPath)
#Here the children method of Describe class returns the layer objects in an array for different
bands for different sensors.
Green = Raster(d.children[0].name)
Red = Raster(d.children[1].name)
NIR1 = Raster(d.children[2].name)
NIR2 = Raster(d.children[3].name)
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)
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)
# iteritems(): returns an iterator of the dictionary's list in the form of (key, value) tuple pairs.
#We have defined and stored the formulas for each indices.
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 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