4 AUTHOR(S): Anne Ghisla <a.ghisla AT gmail.com>
6 PURPOSE: Dedicated GUI for v.krige script.
8 DEPENDS: R 2.x, packages gstat, maptools and spgrass6, optional: automap
10 COPYRIGHT: (C) 2009 by the GRASS Development Team
12 This program is free software under the GNU General Public
13 License (>=v2). Read the file COPYING that comes with GRASS
21 from tempfile
import gettempdir
26 gettext.install(
'grasswxpy', os.path.join(os.getenv(
"GISBASE"),
'locale'), unicode =
True)
33 sys.exit(_(
"No GRASS-python library found."))
36 GUIModulesPath = os.path.join(os.getenv(
"GISBASE"),
"etc",
"wxpython",
"gui_modules")
37 sys.path.append(GUIModulesPath)
43 from preferences
import globalSettings
as UserSettings
47 import wx.lib.flatnotebook
as FN
57 """ Main panel. Contains all widgets except Menus and Statusbar. """
58 def __init__(self, parent, Rinstance, controller, *args, **kwargs):
59 wx.Panel.__init__(self, parent, *args, **kwargs)
65 InputBoxSizer = wx.StaticBoxSizer(wx.StaticBox(self, id = wx.ID_ANY, label = _(
"Input Data")),
66 orient = wx.HORIZONTAL)
68 flexSizer = wx.FlexGridSizer(cols = 3, hgap = 5, vgap = 5)
69 flexSizer.AddGrowableCol(1)
71 flexSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _(
"Point dataset:")),
72 flag = wx.ALIGN_CENTER_VERTICAL)
75 updateOnPopup =
False)
76 self.InputDataMap.SetFocus()
77 flexSizer.Add(item = self.
InputDataMap, flag = wx.ALIGN_CENTER_VERTICAL)
79 RefreshButton = wx.Button(self, id = wx.ID_REFRESH)
81 flexSizer.Add(item = RefreshButton, flag = wx.ALIGN_CENTER_VERTICAL)
83 flexSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _(
"Numeric column:")),
84 flag = wx.ALIGN_CENTER_VERTICAL)
86 self.InputDataColumn.SetSelection(0)
91 InputBoxSizer.Add(item = flexSizer)
94 KrigingSizer = wx.StaticBoxSizer(wx.StaticBox(self, id = wx.ID_ANY, label = _(
"Kriging")), wx.HORIZONTAL)
97 style = FN.FNB_BOTTOM |
98 FN.FNB_NO_NAV_BUTTONS |
99 FN.FNB_FANCY_TABS | FN.FNB_NO_X_BUTTON)
101 for Rpackage
in [
"gstat"]:
102 self.
CreatePage(package = Rpackage, Rinstance = Rinstance, controller = controller)
105 self.
goutput = goutput.GMConsole(parent = self, margin =
False,
108 self.
outpage = self.RPackagesBook.AddPage(self.
goutput, text = _(
"Command output"))
110 self.RPackagesBook.SetSelection(0)
111 KrigingSizer.Add(self.
RPackagesBook, proportion = 1, flag = wx.EXPAND)
114 OutputSizer = wx.StaticBoxSizer(wx.StaticBox(self, id = wx.ID_ANY, label = _(
"Output")), wx.HORIZONTAL)
116 OutputParameters = wx.GridBagSizer(hgap = 5, vgap = 5)
117 OutputParameters.AddGrowableCol(1)
118 OutputParameters.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _(
"Name for the output raster map:")),
119 flag = wx.ALIGN_CENTER_VERTICAL,
123 mapsets = [grass.gisenv()[
'MAPSET']])
124 OutputParameters.Add(item = self.
OutputMapName, flag = wx.EXPAND | wx.ALL,
127 self.VarianceRasterCheckbox.SetValue(state =
True)
129 flag = wx.ALIGN_CENTER_VERTICAL,
133 mapsets = [grass.gisenv()[
'MAPSET']])
139 label = _(
"Allow output files to overwrite existing files"))
140 self.OverwriteCheckBox.SetValue(UserSettings.Get(group=
'cmd', key=
'overwrite', subkey=
'enabled'))
142 pos = (2, 0), span = (1, 2))
144 OutputSizer.Add(OutputParameters, proportion = 0, flag = wx.EXPAND | wx.ALL, border = self.
border)
147 ButtonSizer = wx.BoxSizer(wx.HORIZONTAL)
148 HelpButton = wx.Button(self, id = wx.ID_HELP)
150 QuitButton = wx.Button(self, id = wx.ID_EXIT)
152 self.
RunButton = wx.Button(self, id = wx.ID_ANY, label = _(
"&Run"))
153 self.RunButton.Bind(wx.EVT_BUTTON, self.
OnRunButton)
154 self.RunButton.Enable(
False)
155 ButtonSizer.Add(HelpButton, proportion = 0, flag = wx.ALIGN_LEFT | wx.ALL, border = self.
border)
156 ButtonSizer.Add(QuitButton, proportion = 0, flag = wx.ALIGN_RIGHT | wx.ALL, border = self.
border)
157 ButtonSizer.Add(self.
RunButton, proportion = 0, flag = wx.ALIGN_RIGHT | wx.ALL, border = self.
border)
160 Sizer = wx.BoxSizer(wx.VERTICAL)
161 Sizer.Add(InputBoxSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = self.
border)
162 Sizer.Add(KrigingSizer, proportion = 1, flag = wx.EXPAND | wx.ALL, border = self.
border)
163 Sizer.Add(OutputSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = self.
border)
164 Sizer.Add(ButtonSizer, proportion = 0, flag = wx.ALIGN_RIGHT | wx.ALL, border = self.
border)
165 self.SetSizerAndFit(Sizer)
170 self.InputDataMap.GetElementList()
173 """ Creates the three notebook pages, one for each R package """
174 for package
in [
"gstat"]:
175 classobj = eval(
"RBook"+package+
"Panel")
176 setattr(self,
"RBook"+package+
"Panel", (classobj(self,
178 Rinstance = Rinstance,
179 controller = controller)))
180 self.RPackagesBook.AddPage(page = getattr(self,
"RBook"+package+
"Panel"), text = package)
183 """ Forces refresh of list of available layers. """
184 self.InputDataMap.GetElementList()
187 """ Cancel button pressed"""
201 grass.run_command(
'g.manual', entry =
'v.krige')
206 """ Refreshes list of columns and fills output map name TextCtrl """
207 MapName = event.GetString()
208 self.InputDataColumn.InsertColumns(vector = MapName,
209 layer = 1, excludeKey =
True,
210 type = [
'integer',
'double precision'])
211 self.InputDataColumn.SetSelection(0)
212 self.RunButton.Enable(self.InputDataColumn.GetSelection()
is not -1)
213 self.RBookgstatPanel.PlotButton.Enable(self.InputDataColumn.GetSelection()
is not -1)
215 if self.InputDataColumn.GetSelection()
is not -1:
216 self.OutputMapName.SetValue(MapName.split(
"@")[0]+
"_kriging")
217 self.OutputVarianceMapName.SetValue(MapName.split(
"@")[0]+
"_kriging_var")
219 self.OutputMapName.SetValue(
'')
220 self.OutputVarianceMapName.SetValue(
'')
223 """ Execute R analysis. """
228 SelectedPanel = self.RPackagesBook.GetCurrentPage()
230 if self.RPackagesBook.GetPageText(self.RPackagesBook.GetSelection()) ==
'Command output':
231 self.goutput.WriteError(
"No parameters for running. Please select \"gstat\" tab, check parameters and re-run.")
235 command = [
"v.krige",
"input=" + self.InputDataMap.GetValue(),
236 "column=" + self.InputDataColumn.GetValue(),
237 "output=" + self.OutputMapName.GetValue(),
238 "package=" +
'%s' % self.RPackagesBook.GetPageText(self.RPackagesBook.GetSelection())]
240 if not hasattr(SelectedPanel,
'VariogramCheckBox')
or not SelectedPanel.VariogramCheckBox.IsChecked():
241 command.append(
"model=" +
'%s' % SelectedPanel.ModelChoicebox.GetStringSelection().
split(
" ")[0])
243 for i
in [
'Sill',
'Nugget',
'Range']:
244 if getattr(SelectedPanel, i+
"ChextBox").IsChecked():
245 command.append(i.lower() +
"=" +
'%s' % getattr(SelectedPanel, i+
'Ctrl').GetValue())
247 if SelectedPanel.KrigingRadioBox.GetStringSelection() ==
"Block kriging":
248 command.append(
"block=" +
'%s' % SelectedPanel.BlockSpinBox.GetValue())
249 if self.OverwriteCheckBox.IsChecked():
250 command.append(
"--overwrite")
251 if self.VarianceRasterCheckbox.IsChecked():
252 command.append(
"output_var=" + self.OutputVarianceMapName.GetValue())
257 self.goutput.RunCmd(command, switchPage =
True)
260 self.OutputVarianceMapName.Enable(event.IsChecked())
263 """ Kriging module for GRASS GIS. Depends on R and its packages gstat and geoR. """
264 def __init__(self, parent, Rinstance, controller, *args, **kwargs):
265 wx.Frame.__init__(self, parent, *args, **kwargs)
267 self.SetTitle(_(
"Kriging Module"))
268 self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR,
'grass_dialog.ico'), wx.BITMAP_TYPE_ICO))
270 self.CreateStatusBar()
271 self.log.message(_(
"Ready."))
274 self.SetMinSize(self.GetBestSize())
275 self.SetSize(self.GetBestSize())
278 """ The log output is redirected to the status bar of the containing frame. """
283 """ Updates status bar """
284 self.parent.SetStatusText(text_string.strip())
287 """ Generic notebook page with shared widgets and empty kriging functions. """
289 wx.Panel.__init__(self, parent, *args, **kwargs)
295 label = _(
"Variogram fitting")),
301 self.VariogramSizer.Add(self.
LeftSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = parent.border)
302 self.VariogramSizer.Add(self.
RightSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = parent.border)
305 self.
PlotButton = wx.Button(self, id = wx.ID_ANY, label = _(
"Plot/refresh variogram"))
307 self.PlotButton.Enable(
False)
308 self.LeftSizer.Add(self.
PlotButton, proportion = 0, flag = wx.ALL, border = parent.border)
309 self.LeftSizer.Add(self.
ParametersSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = parent.border)
314 setattr(self, n+
"ChextBox", wx.CheckBox(self,
315 id = self.ParametersList.index(n),
317 setattr(self, n+
"Ctrl", (wx.SpinCtrl(self,
319 min = MinValues[self.ParametersList.index(n)],
321 getattr(self, n+
"ChextBox").Bind(wx.EVT_CHECKBOX,
323 id = self.ParametersList.index(n))
324 setattr(self, n+
"Sizer", (wx.BoxSizer(wx.HORIZONTAL)))
325 self.ParametersSizer.Add(getattr(self, n+
"ChextBox"),
326 flag = wx.ALIGN_CENTER_VERTICAL,
327 pos = (self.ParametersList.index(n),0))
328 self.ParametersSizer.Add(getattr(self, n+
"Ctrl"),
329 flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL,
330 pos = (self.ParametersList.index(n),1))
341 label = _(
"Kriging techniques")),
344 KrigingList = [
"Ordinary kriging",
"Block kriging"]
347 choices = KrigingList,
349 style = wx.RA_SPECIFY_COLS)
351 self.KrigingSizer.Add(self.
KrigingRadioBox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = parent.border)
354 BlockSizer = wx.BoxSizer(wx.HORIZONTAL)
355 BlockLabel = wx.StaticText(self, id = wx.ID_ANY, label = _(
"Block size:"))
356 self.
BlockSpinBox = wx.SpinCtrl(self, id = wx.ID_ANY, min = 1, max = maxint)
357 self.BlockSpinBox.Enable(
False)
358 BlockSizer.Add(BlockLabel, flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = parent.border)
359 BlockSizer.Add(self.
BlockSpinBox, flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = parent.border)
360 self.KrigingSizer.Add(BlockSizer, flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = parent.border)
362 self.
Sizer = wx.BoxSizer(wx.VERTICAL)
363 self.Sizer.Add(self.
VariogramSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = parent.border)
364 self.Sizer.Add(self.
KrigingSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = parent.border)
367 self.BlockSpinBox.Enable(event.GetInt() == 1)
370 """ Plots variogram with current options. """
374 """ Enables/Disables the SpinCtrl in respect of the checkbox. """
375 n = self.ParametersList[event.GetId()]
376 getattr(self, n+
"Ctrl").Enable(event.IsChecked())
379 """ Subclass of RBookPanel, with specific gstat options and kriging functions. """
380 def __init__(self, parent, Rinstance, controller, *args, **kwargs):
381 RBookPanel.__init__(self, parent, *args, **kwargs)
387 if robjects.r.require(
'automap')[0]:
389 self.LeftSizer.Insert(0,
392 flag = wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALL,
394 self.SetSizerAndFit(self.
Sizer)
395 self.VariogramCheckBox.Bind(wx.EVT_CHECKBOX, self.
HideOptions)
396 self.VariogramCheckBox.SetValue(state =
True)
398 ModelFactor = robjects.r.vgm().rx(
'long')
399 ModelList = robjects.r.levels(ModelFactor[0])
405 for n
in [
"Sill",
"Nugget",
"Range"]:
406 getattr(self, n+
"Ctrl").Enable(
False)
407 self.ModelChoicebox.Enable(
False)
409 VariogramSubSizer = wx.BoxSizer(wx.HORIZONTAL)
410 VariogramSubSizer.Add(item = wx.StaticText(self,
412 label = _(
"Model: ")),
413 flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
416 flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
419 self.LeftSizer.Insert(2, item = VariogramSubSizer)
421 self.SetSizerAndFit(self.
Sizer)
424 self.ModelChoicebox.Enable(
not event.IsChecked())
425 for n
in [
"Sill",
"Nugget",
"Range"]:
426 if not event.IsChecked():
427 getattr(self, n+
"Ctrl").Enable(
True)
428 getattr(self, n+
"ChextBox").SetValue(
True)
429 getattr(self, n+
"ChextBox").Enable(
False)
431 getattr(self, n+
"Ctrl").Enable(
False)
432 getattr(self, n+
"ChextBox").SetValue(
False)
433 getattr(self, n+
"ChextBox").Enable(
True)
437 """ Plots variogram with current options. """
441 map = self.parent.InputDataMap.GetValue()
442 column = self.parent.InputDataColumn.GetValue()
445 if globals()[
"InputData"]
is None:
446 globals()[
"InputData"] = controller.ImportMap(map = map,
449 Formula = controller.ComposeFormula(column = column,
450 isblock = self.KrigingRadioBox.GetStringSelection() ==
"Block kriging",
451 inputdata = globals()[
'InputData'])
453 if hasattr(self,
'VariogramCheckBox')
and self.VariogramCheckBox.IsChecked():
455 for each
in (
"Sill",
"Nugget",
"Range"):
456 if getattr(self, each+
'ChextBox').IsChecked():
457 setattr(self, each.lower(), getattr(self, each+
"Ctrl").GetValue())
459 setattr(self, each.lower(), robjects.r(
'''NA'''))
461 self.
model = self.ModelChoicebox.GetStringSelection().
split(
" ")[0]
462 for each
in (
"Sill",
"Nugget",
"Range"):
463 if getattr(self, each+
'ChextBox').IsChecked():
464 setattr(self, each.lower(), getattr(self, each+
"Ctrl").GetValue())
466 globals()[
"Variogram"] = controller.FitVariogram(Formula,
470 nugget = self.nugget,
474 thread.start_new_thread(self.
plot, ())
479 textplot = robjects.r.plot(Variogram[
'datavariogram'], Variogram[
'variogrammodel'])
486 rinterface.process_revents()
490 """ Subclass of RBookPanel, with specific geoR options and kriging functions. """
492 RBookPanel.__init__(self, parent, *args, **kwargs)
494 for n
in self.GetChildren():
496 self.Sizer.Add(wx.StaticText(self, id = wx.ID_ANY, label = _(
"Work in progress! No functionality provided.")))
497 self.SetSizerAndFit(self.
Sizer)