我正在尝试从图书"Remote Sensing Raster Programming"
开始计算NDVI from raster dataset
。程序使用NIR
读取两个单独的栅格波段RED
和GDAL
,并执行以下等式NDVI = (NIR-RED)/(NIR+RED)
。
但是它产生错误并且GUI没有打开。我正在努力学习wxPython,我将非常感谢您解决这个问题。
错误 -
Traceback (most recent call last):
File "C:\Users\User\Desktop\ndvi.py", line 189, in <module>
frame = MyFrame(None)
File "C:\Users\User\Desktop\ndvi.py", line 48, in __init__
self.make_fb()
File "C:\Users\User\Desktop\ndvi.py", line 112, in make_fb
fileMode=wx.OPEN,
AttributeError: 'module' object has no attribute 'OPEN'
程序代码
#!/usr/bin/python
# -*- coding: cp1252 -*-
import wx
import wx.lib.filebrowsebutton as filebrowse
import os
# For Image Processing
import numpy
from osgeo import gdalnumeric
from osgeo import gdal
from osgeo import gdal_array
from osgeo.gdalconst import *
# Define satellite bands
redchan = ''
nirchan = ''
# Define output file name
output = ''
# Define Info Message
overview = """Vegetation Index Processing.
Calculates vegetation indices based on biophysical parameters.
NDVI: Normalized Difference Vegetation Index
NDVI = (band 2 - band 1) / (band 2 + band 1)
NDVI = (NIR - Red) / (NIR + Red)
Rouse J., Haas R., Schell J. and Deering D. (1974).
Monitoring vegetation systems in the Great Plains
with ERTS. In Proceedings of the Third Earth
Resources Technology Satellite-1 Symposium, Greenbelt."""
class MyFrame(wx.Frame):
def __init__(self,parent, id=-1, title='Normalized Difference Vegetation Index Processing',
pos=(0,0),
size=(400,400),
style=wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self.lognull = wx.LogNull()
# Input Filenames
self.redchan = redchan
self.nirchan = nirchan
self.output = output
# Construct Interface
self.make_text()
self.make_buttons()
self.make_fb()
self.mbox = wx.BoxSizer(wx.VERTICAL)
self.mbox.Add((10,10))
self.mbox.Add(self.text, 1, wx.EXPAND|wx.CENTER, 10)
self.mbox.Add((10,10))
self.mbox.Add((10,10))
self.mbox.Add(self.cc2, 1, wx.EXPAND, 10)
self.mbox.Add(self.cc3, 1, wx.EXPAND, 10)
self.mbox.Add(self.cc6, 1, wx.EXPAND, 10)
self.mbox.Add((10,10))
self.mbox.Add((10,10))
self.mbox.Add(self.bbox, 1, wx.CENTER, 10)
self.mbox.Add((10,10))
self.SetSizer(self.mbox)
self.bindEvents()
# Process Equations, Handling and saving of output
def OnOK(self,event):
print "red: ", self.redchan, " nir:",self.nirchan, " out:", self.output
if(self.redchan==''):
self.OnFileInError()
elif(self.nirchan==''):
self.OnFileInError()
else:
self.redband = gdal_array.LoadFile(self.redchan)
self.nirband = gdal_array.LoadFile(self.nirchan)
# NDVI
self.result=self.ndvi(self.redband, self.nirband)
# prepare/create output file
tmp = gdal.Open(str(self.redchan))
geoT = tmp.GetGeoTransform()
proJ = tmp.GetProjection()
tmp = None
out = gdal_array.OpenArray(self.result )
out.SetGeoTransform( geoT )
out.SetProjection( proJ )
driver = gdal.GetDriverByName( 'GTiff' )
driver.CreateCopy( self.output, out )
self.Destroy()
def ndvi( self, redchan, nirchan ):
"""
Normalized Difference Vegetation Index
ndvi( redchan, nirchan )
"""
result = 1.0*( nirchan - redchan )
result /= 1.0*( nirchan + redchan )
return result
def OnFileInError(self):
dlg = wx.MessageDialog(self,
'Minimum files to add:\n\n Input files => Red and NIR \n One Output file',
'Error',wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
# Path+filename seek and set
def make_fb(self):
# get current working directory
self.dirnm = os.getcwd()
self.cc2 = filebrowse.FileBrowseButton(
self, -1, size=(50, -1), labelText='RED:',
startDirectory = self.dirnm,
fileMode=wx.OPEN,
changeCallback = self.fbbCallback2,
)
self.cc3 = filebrowse.FileBrowseButton(
self, -1, size=(50, -1), labelText='NIR:',
startDirectory = self.dirnm,
fileMode=wx.OPEN,
changeCallback = self.fbbCallback3
)
self.cc6 = filebrowse.FileBrowseButton(
self, -1, size=(50, -1), labelText='OUT File:',
startDirectory = self.dirnm,
fileMask='*.tif',
fileMode=wx.SAVE,
changeCallback = self.fbbCallback6
)
# Collect path+filenames
def fbbCallback2(self, evt):
self.redchan = str(evt.GetString())
def fbbCallback3(self, evt):
self.nirchan = str(evt.GetString())
def fbbCallback6(self, evt):
self.output = str(evt.GetString())
# Front text
def make_text(self):
self.text = wx.StaticText(self, -1, '''\n\tThis is a full Python +
WxPython application,\n\tprocessing NDVI through the use
of \n\tGDAL Python bindings and numpy''')
# Bottom buttons
def make_buttons(self):
self.bbox = wx.BoxSizer(wx.HORIZONTAL)
# OnOK
bmp0 = wx.ArtProvider.GetBitmap(wx.ART_TICK_MARK, wx.ART_TOOLBAR, (32,32))
self.b0 = wx.BitmapButton(self, 20, bmp0, (20, 20),
(bmp0.GetWidth()+50, bmp0.GetHeight()+10), style=wx.NO_BORDER)
self.b0.SetToolTip("Process")
self.bbox.Add(self.b0,1,wx.CENTER,10)
# OnCancel
bmp1 = wx.ArtProvider.GetBitmap(wx.ART_CROSS_MARK, wx.ART_TOOLBAR, (32,32))
self.b1 = wx.BitmapButton(self, 30, bmp1, (20, 20),
(bmp1.GetWidth()+50, bmp1.GetHeight()+10), style=wx.NO_BORDER)
self.b1.SetToolTip("Abort")
self.bbox.Add(self.b1,1,wx.CENTER,10)
# OnInfo
bmp2 = wx.ArtProvider.GetBitmap(wx.ART_HELP, wx.ART_TOOLBAR, (32,32))
self.b2 = wx.BitmapButton(self, 40, bmp2, (20, 20),
(bmp2.GetWidth()+50, bmp2.GetHeight()+10), style=wx.NO_BORDER)
self.b2.SetToolTip("Help/Info.")
self.bbox.Add(self.b2,1,wx.CENTER,10)
def bindEvents(self):
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.Bind(wx.EVT_BUTTON, self.OnOK, self.b0)
self.Bind(wx.EVT_BUTTON, self.OnCancel, self.b1)
self.Bind(wx.EVT_BUTTON, self.OnInfo, self.b2)
def OnCloseWindow(self, event):
self.Destroy()
def OnCancel(self, event):
self.Destroy()
def OnInfo(self,event):
dlg = wx.MessageDialog(self, overview,'Help', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
class MainApp(wx.App):
def OnInit(self):
frame = MainFrame(None)
frame.Show(True)
self.SetTopWindow(frame)
return True
if __name__ == '__main__':
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
答案 0 :(得分:3)
如果您使用的是wxPython 4(Phoenix),那么您希望使用wx.FD_OPEN
而不是wx.OPEN
。我不确定为什么它不在Migration Guide或Classic Vs. Phoenix页面中