-
Notifications
You must be signed in to change notification settings - Fork 36
/
GDAL_RS_Indices_Derivation.py
313 lines (255 loc) · 10.8 KB
/
GDAL_RS_Indices_Derivation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
""""
Calculate spectral indices using satellite remote sensing data.
Author: Ryan S. Anderson
Email: [email protected]
The MIT License (MIT)
Copyright (c) 2015 Ryan S. Anderson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
1. You MAY NOT sell any application that you derive from the code in this repository
without specific written permission from Ryan S. Anderson.
2. The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import time
import numpy
import ConfigParser
import Tkinter
from gdal_calculations import *
from Tkinter import *
from ttk import Button
from tkFileDialog import askdirectory, askopenfilename
startTime = time.time()
Env.overwrite = True
# Read .ini file
Config = ConfigParser.ConfigParser()
Config.read("Sensors_Formulas_RSIDT.ini")
# Set input stacked image and output directory if desired
inPath = r""
outPath = r""
# Get values from .ini file
indices = eval(Config.get("Parameters", "indices")) # List of indices
sensors = eval(Config.get("Parameters", "sensors")) # List of sensors
indicesSensor = eval(Config.get("Parameters", "indicesSensor")) # Dictionary of indices with compatible sensors
# Set indices to disable based on sensor compatibility
MSS_Disable = []
TM_Disable = []
ETM_Disable = []
OLI_Disable = []
MODIS_Disable = []
WV_Disable = []
for key, value in indicesSensor.iteritems():
if "Landsat 1-5 MSS" not in value:
MSS_Disable.append(key)
if "Landsat 4-5 TM" not in value:
TM_Disable.append(key)
if "Landsat 7 ETM+" not in value:
ETM_Disable.append(key)
if "Landsat 8 OLI" not in value:
OLI_Disable.append(key)
if "MODIS" not in value:
MODIS_Disable.append(key)
if "Worldview 02" not in value:
WV_Disable.append(key)
# --------------Begin GUI----------------
top = Tk()
top.title("Remote Sensing Indices Derivation Tool")
def cbChecked():
label['text'] = ''
# Set text to require reflectance or digital number based on sensor
def tcapshowstate(*args):
if cbVar[indices.index("Brightness")].get() or cbVar[indices.index("Greenness")].get() or cbVar[indices.index("Wetness")].get():
if vSensor.get() == "MODIS" or vSensor.get() == "Landsat 4-5 TM" or vSensor.get() == "Worldview 02" or vSensor.get() == "Landsat 7 ETM+" or vSensor.get() == "Landsat 8 OLI":
form["text"] = "(Requires Reflectance)"
if vSensor.get() == "Landsat 1-5 MSS":
form["text"] = "(Requires Digital Number (DN))"
else:
form["text"] = ""
def sectionBreak(index, title):
if text == index:
global rowpos, colpos
colpos = 2
rowpos += 1
Label(top, text=title, font="Times 10").grid(row=rowpos,column=colpos, columnspan=3, sticky=W)
rowpos += 1
exVar = IntVar()
ex = Checkbutton(top, text="Export Individual Bands", variable=exVar).grid(row=1, column=4, sticky=W, columnspan=2, padx=4, pady=4)
rowpos = 1
colpos = 2
cb = list(range(len(indices)))
cbVar = list(range(len(indices)))
for i, text in enumerate(indices):
if colpos % 6 == 0:
colpos = 2
rowpos += 1
sectionBreak("NDVI", "Vegetation Indices")
sectionBreak("NDWI", "Hydrologic Indices")
sectionBreak("Clay", "Geologic Indices")
sectionBreak("NBR", "Burn Indices")
sectionBreak("NDBI", "Miscellaneous Indices")
if text == 'Brightness':
colpos = 2
rowpos += 1
Label(top, text="Tasseled Cap Transformation", font="Times 10").grid(row=rowpos,column=colpos, columnspan=3, sticky=W)
form = Label(top, text="", font="Times 10")
form.grid(row=rowpos, column=4, columnspan=2, sticky=W)
rowpos += 1
cbVar[i] = IntVar()
cb[i] = Checkbutton(top, text=text, variable=cbVar[i], command=cbChecked)
cb[i].grid(row=rowpos, column=colpos, sticky=W, padx=4, pady=4)
colpos += 1
label = Label(top, width=5)
label.grid(row=rowpos, column=colpos)
cbChecked()
rowpos += 1
# Set Labels
Label(top, text="Sensor", font="Times 12 bold").grid(row=1,column=1, sticky=W)
Label(top, text="Indices", font="Times 12 bold").grid(row=1,column=2, sticky=W)
# Set Radiobuttons
def rbChecked():
label['text'] = ''
sensrowpos = 2
rb = list(range(len(sensors)))
vSensor = StringVar()
for i, sensor in enumerate(sensors):
rb[i] = Radiobutton(top, text=sensor, variable=vSensor, value=sensor)
rb[i].grid(row=sensrowpos, column=1, pady=4, padx=7, sticky=W)
sensrowpos += 1
rbChecked()
def disable(sensor, disableList):
if vSensor.get() == sensor:
for i in indices:
cbVar[indices.index(i)].set(1)
cb[indices.index(i)].configure(state='normal')
for i in disableList:
cbVar[indices.index(i)].set(0)
cb[indices.index(i)].configure(state='disabled')
# Disable or enable checkbuttons based on sensor selection
def showstate(*args):
disable("Worldview 02", WV_Disable)
disable("Landsat 4-5 TM", TM_Disable)
disable("Landsat 7 ETM+", ETM_Disable)
disable("Landsat 8 OLI", OLI_Disable)
disable("MODIS", MODIS_Disable)
disable("Landsat 1-5 MSS", MSS_Disable)
vSensor.trace_variable("w", showstate)
cbVar[indices.index("Brightness")].trace_variable("w", tcapshowstate)
cbVar[indices.index("Greenness")].trace_variable("w", tcapshowstate)
cbVar[indices.index("Wetness")].trace_variable("w", tcapshowstate)
vSensor.trace_variable("w", tcapshowstate)
vSensor.set("Landsat 4-5 TM")
top.resizable(0,0) # Disable window resizing
Label(top, text="Input Stacked Image").grid(row=rowpos,column=1, sticky=W)
rowpos += 1
indirVar = StringVar()
indirEntry = Entry(top, textvariable = indirVar, width=50)
indirEntry.delete(0, END)
indirEntry.insert(INSERT, inPath)
indirEntry.grid(row=rowpos,column=0, sticky=W, columnspan=8, padx=5)
def indirbrowser():
indir = askopenfilename(parent=top, title="Select Stacked Input Image")
if len(indir) > 0:
indirEntry.delete(0, END)
indirEntry.insert(INSERT, indir)
indirButton = Button(top, text = 'Browse', command=indirbrowser).grid(row=rowpos, column=4, pady=2, padx=2)
rowpos += 1
Label(top, text="Output Directory").grid(row=rowpos,column=1, sticky=W)
rowpos += 1
outdirVar = StringVar()
outdirEntry = Entry(top, textvariable = outdirVar, width=50)
outdirEntry.delete(0, END)
outdirEntry.insert(INSERT, outPath)
outdirEntry.grid(row=rowpos, column=1, sticky=W, columnspan=8, padx=5)
def outdirbrowser():
outdir = askdirectory(parent=top, title="Select Root Output Directory", mustexist=1)
if len(outdir) > 0:
outdirEntry.delete(0, END)
outdirEntry.insert(INSERT, outdir)
outdirButton = Button(top, text = 'Browse', command=outdirbrowser).grid(row=rowpos, column=4, pady=2, padx=2)
rowpos += 1
# Run Button
def callback():
top.destroy()
Button(top, text = 'Run', command=callback).grid(row=rowpos,column=3, pady=4, padx=4)
# Quit Button
def quitbutton():
print "Processing cancelled"
top.destroy()
raise SystemExit(0)
Button(top, text='Quit', command=quitbutton).grid(row=rowpos, column=2, pady=4, padx=4)
top.mainloop()
# --------------End GUI----------------
# Set variables selected from GUI
Sensor = vSensor.get()
inPath = indirVar.get()
outPath = outdirVar.get()
if not (outPath and os.path.exists(outPath)): os.makedirs(outPath) # Create output directory if it doesn't exist
pathRoot, inRasterStr = os.path.split(inPath)
print "Processing", inRasterStr
inRaster = Dataset(inPath)
# Set bands based on sensor
if Sensor == "Landsat 1-5 MSS":
Green = Float32(inRaster.get_raster_band(1))
Red = Float32(inRaster.get_raster_band(2))
NIR1 = Float32(inRaster.get_raster_band(3))
NIR2 = Float32(inRaster.get_raster_band(4))
if Sensor == "Landsat 4-5 TM" or Sensor == "Landsat 7 ETM+":
Blue = Float32(inRaster.get_raster_band(1))
Green = Float32(inRaster.get_raster_band(2))
Red = Float32(inRaster.get_raster_band(3))
NIR1 = Float32(inRaster.get_raster_band(4))
SWIR1 = Float32(inRaster.get_raster_band(5))
SWIR2 = Float32(inRaster.get_raster_band(6))
if Sensor == "Landsat 8 OLI":
Coastal = Float32(inRaster.get_raster_band(1))
Blue = Float32(inRaster.get_raster_band(2))
Green = Float32(inRaster.get_raster_band(3))
Red = Float32(inRaster.get_raster_band(4))
NIR1 = Float32(inRaster.get_raster_band(5))
SWIR1 = Float32(inRaster.get_raster_band(6))
SWIR2 = Float32(inRaster.get_raster_band(7))
if Sensor == "MODIS":
Red = Float32(inRaster.get_raster_band(1))
NIR1 = Float32(inRaster.get_raster_band(2))
Blue = Float32(inRaster.get_raster_band(3))
Green = Float32(inRaster.get_raster_band(4))
NIR2 = Float32(inRaster.get_raster_band(5))
SWIR1 = Float32(inRaster.get_raster_band(6))
SWIR2 = Float32(inRaster.get_raster_band(7))
if Sensor == "Worldview 02":
Coastal = Float32(inRaster.get_raster_band(1))
Blue = Float32(inRaster.get_raster_band(2))
Green = Float32(inRaster.get_raster_band(3))
Yellow = Float32(inRaster.get_raster_band(4))
Red = Float32(inRaster.get_raster_band(5))
RedEdge = Float32(inRaster.get_raster_band(6))
NIR1 = Float32(inRaster.get_raster_band(7))
NIR2 = Float32(inRaster.get_raster_band(8))
# Export individual Bands if selected
if exVar.get():
print "Exporting Bands"
for bandNo in range(inRaster._nbands):
outBand = inRaster.get_raster_band(bandNo + 1) * 1.0
outBand.save(outPath + "/" + inRasterStr[:-4] + "_B" + str(bandNo + 1) + ".tif")
# Calculate and save indices
for key, value in indicesSensor.iteritems():
if Sensor in value:
if cbVar[indices.index(key)].get(): # Determine if indice was selected from GUI
if key == 'Brightness' or key == 'Greenness' or key == 'Wetness' or key == 'Yellowness': # Check if tasseled cap index
formula = Config.get(Sensor, key) # Get sensor specific tasseled cap coefficients
else:
formula = Config.get("Formulas", key)
print key
eval(formula).save(outPath + "/" + inRasterStr[:-4] + "_" + key + ".tif") # Save index raster
endTime = time.time() - startTime
print "Completed in", ("%.2f" % endTime), 'seconds.'