GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis_set.py
Go to the documentation of this file.
1 """!
2 @package gis_set
3 
4 GRASS start-up screen.
5 
6 Initialization module for wxPython GRASS GUI.
7 Location/mapset management (selection, creation, etc.).
8 
9 Classes:
10  - gis_set::GRASSStartup
11  - gis_set::GListBox
12  - gis_set::StartUp
13 
14 (C) 2006-2012 by the GRASS Development Team
15 
16 This program is free software under the GNU General Public License
17 (>=v2). Read the file COPYING that comes with GRASS for details.
18 
19 @author Michael Barton and Jachym Cepicky (original author)
20 @author Martin Landa <landa.martin gmail.com> (various updates)
21 """
22 
23 import os
24 import sys
25 import shutil
26 import copy
27 import platform
28 import codecs
29 import getpass
30 
31 ### i18N
32 import gettext
33 gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
34 
35 if __name__ == "__main__":
36  sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython'))
37 from core import globalvar
38 import wx
39 import wx.lib.mixins.listctrl as listmix
40 import wx.lib.scrolledpanel as scrolled
41 
42 from grass.script import core as grass
43 
44 from gui_core.ghelp import HelpFrame
45 from core.gcmd import GMessage, GError, DecodeString, RunCommand, GWarning
46 from core.utils import GetListOfLocations, GetListOfMapsets
47 from location_wizard.dialogs import RegionDef
48 from gui_core.dialogs import TextEntryDialog
49 from gui_core.widgets import GenericValidator
50 
51 from grass.script import core as grass
52 
53 sys.stderr = codecs.getwriter('utf8')(sys.stderr)
54 
55 class GRASSStartup(wx.Frame):
56  """!GRASS start-up screen"""
57  def __init__(self, parent = None, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE):
58 
59  #
60  # GRASS variables
61  #
62  self.gisbase = os.getenv("GISBASE")
63  self.grassrc = self._readGisRC()
64  self.gisdbase = self.GetRCValue("GISDBASE")
65 
66  #
67  # list of locations/mapsets
68  #
69  self.listOfLocations = []
70  self.listOfMapsets = []
72 
73  wx.Frame.__init__(self, parent = parent, id = id, style = style)
74 
75  self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
76 
77  self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
78 
79  # i18N
80  import gettext
81  gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
82 
83  #
84  # graphical elements
85  #
86  # image
87  try:
88  name = os.path.join(globalvar.ETCIMGDIR, "startup_banner.gif")
89  self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
90  wx.Bitmap(name = name,
91  type = wx.BITMAP_TYPE_GIF))
92  except:
93  self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
94 
95  # labels
96  ### crashes when LOCATION doesn't exist
97  versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
98  grassVersion = versionFile.readline().split(' ')[0].rstrip('\n')
99  versionFile.close()
100 
101  self.select_box = wx.StaticBox (parent = self.panel, id = wx.ID_ANY,
102  label = " %s " % _("Choose project location and mapset"))
103 
104  self.manage_box = wx.StaticBox (parent = self.panel, id = wx.ID_ANY,
105  label = " %s " % _("Manage"))
106  self.lwelcome = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
107  label = _("Welcome to GRASS GIS %s\n"
108  "The world's leading open source GIS") % grassVersion,
109  style = wx.ALIGN_CENTRE)
110  self.ltitle = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
111  label = _("Select an existing project location and mapset\n"
112  "or define a new location"),
113  style = wx.ALIGN_CENTRE)
114  self.ldbase = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
115  label = _("GIS Data Directory:"))
116  self.llocation = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
117  label = _("Project location\n(projection/coordinate system)"),
118  style = wx.ALIGN_CENTRE)
119  self.lmapset = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
120  label = _("Accessible mapsets\n(directories of GIS files)"),
121  style = wx.ALIGN_CENTRE)
122  self.lcreate = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
123  label = _("Create new mapset\nin selected location"),
124  style = wx.ALIGN_CENTRE)
125  self.ldefine = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
126  label = _("Define new location"),
127  style = wx.ALIGN_CENTRE)
128  self.lmanageloc = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
129  label = _("Rename/delete selected\nmapset or location"),
130  style = wx.ALIGN_CENTRE)
131 
132  # buttons
133  self.bstart = wx.Button(parent = self.panel, id = wx.ID_ANY,
134  label = _("Start &GRASS"))
135  self.bstart.SetDefault()
136  self.bexit = wx.Button(parent = self.panel, id = wx.ID_EXIT)
137  self.bstart.SetMinSize((180, self.bexit.GetSize()[1]))
138  self.bhelp = wx.Button(parent = self.panel, id = wx.ID_HELP)
139  self.bbrowse = wx.Button(parent = self.panel, id = wx.ID_ANY,
140  label = _("&Browse"))
141  self.bmapset = wx.Button(parent = self.panel, id = wx.ID_ANY,
142  label = _("&Create mapset"))
143  self.bwizard = wx.Button(parent = self.panel, id = wx.ID_ANY,
144  label = _("&Location wizard"))
145  self.bwizard.SetToolTipString(_("Start location wizard."
146  " After location is created successfully,"
147  " GRASS session is started."))
148  self.manageloc = wx.Choice(parent = self.panel, id = wx.ID_ANY,
149  choices = [_('Rename mapset'), _('Rename location'),
150  _('Delete mapset'), _('Delete location')])
151  self.manageloc.SetSelection(0)
152 
153  # textinputs
154  self.tgisdbase = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY, value = "", size = (300, -1),
155  style = wx.TE_PROCESS_ENTER)
156 
157  # Locations
158  self.lblocations = GListBox(parent = self.panel,
159  id = wx.ID_ANY, size = (180, 200),
160  choices = self.listOfLocations)
161 
162  self.lblocations.SetColumnWidth(0, 180)
163 
164  # TODO: sort; but keep PERMANENT on top of list
165  # Mapsets
166  self.lbmapsets = GListBox(parent = self.panel,
167  id = wx.ID_ANY, size = (180, 200),
168  choices = self.listOfMapsets)
169 
170  self.lbmapsets.SetColumnWidth(0, 180)
171 
172  # layout & properties
173  self._set_properties()
174  self._do_layout()
175 
176  # events
177  self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
178  self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
179  self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
180  self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
181  self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
182  self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
183  self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
184  self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
185  self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
186  self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
187  self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
188  self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
189 
190  def _set_properties(self):
191  """!Set frame properties"""
192  self.SetTitle(_("Welcome to GRASS GIS"))
193  self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
194  wx.BITMAP_TYPE_ICO))
195 
196  self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
197  self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
198 
199  self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
200  self.bstart.SetToolTipString(_("Enter GRASS session"))
201  self.bstart.Enable(False)
202  self.bmapset.Enable(False)
203  self.manageloc.Enable(False)
204 
205  # set database
206  if not self.gisdbase:
207  # sets an initial path for gisdbase if nothing in GISRC
208  if os.path.isdir(os.getenv("HOME")):
209  self.gisdbase = os.getenv("HOME")
210  else:
211  self.gisdbase = os.getcwd()
212  try:
213  self.tgisdbase.SetValue(self.gisdbase)
214  except UnicodeDecodeError:
215  wx.MessageBox(parent = self, caption = _("Error"),
216  message = _("Unable to set GRASS database. "
217  "Check your locale settings."),
218  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
219 
220  self.OnSetDatabase(None)
221  location = self.GetRCValue("LOCATION_NAME")
222  if location == "<UNKNOWN>":
223  return
224  if not os.path.isdir(os.path.join(self.gisdbase, location)):
225  location = None
226 
227  # list of locations
228  self.UpdateLocations(self.gisdbase)
229  try:
230  self.lblocations.SetSelection(self.listOfLocations.index(location),
231  force = True)
232  self.lblocations.EnsureVisible(self.listOfLocations.index(location))
233  except ValueError:
234  sys.stderr.write(_("ERROR: Location <%s> not found\n") % self.GetRCValue("LOCATION_NAME"))
235  if len(self.listOfLocations) > 0:
236  self.lblocations.SetSelection(0, force = True)
237  self.lblocations.EnsureVisible(0)
238  location = self.listOfLocations[0]
239  else:
240  return
241 
242  # list of mapsets
243  self.UpdateMapsets(os.path.join(self.gisdbase, location))
244  mapset = self.GetRCValue("MAPSET")
245  if mapset:
246  try:
247  self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
248  force = True)
249  self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
250  except ValueError:
251  sys.stderr.write(_("ERROR: Mapset <%s> not found\n") % mapset)
252  self.lbmapsets.SetSelection(0, force = True)
253  self.lbmapsets.EnsureVisible(0)
254 
255  def _do_layout(self):
256  sizer = wx.BoxSizer(wx.VERTICAL)
257  dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
258  location_sizer = wx.BoxSizer(wx.HORIZONTAL)
259  select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
260  select_sizer = wx.FlexGridSizer(rows = 2, cols = 2, vgap = 4, hgap = 4)
261  select_sizer.AddGrowableRow(1)
262  select_sizer.AddGrowableCol(0)
263  select_sizer.AddGrowableCol(1)
264  manage_sizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
265  btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
266 
267  # gis data directory
268  dbase_sizer.Add(item = self.ldbase, proportion = 0,
269  flag = wx.ALIGN_CENTER_VERTICAL |
270  wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
271  border = 3)
272  dbase_sizer.Add(item = self.tgisdbase, proportion = 1,
273  flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
274  border = 3)
275  dbase_sizer.Add(item = self.bbrowse, proportion = 0,
276  flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
277  border = 3)
278 
279  # select sizer
280  select_sizer.Add(item = self.llocation, proportion = 0,
281  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
282  border = 3)
283  select_sizer.Add(item = self.lmapset, proportion = 0,
284  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
285  border = 3)
286  select_sizer.Add(item = self.lblocations, proportion = 1,
287  flag = wx.EXPAND)
288  select_sizer.Add(item = self.lbmapsets, proportion = 1,
289  flag = wx.EXPAND)
290 
291  select_boxsizer.Add(item = select_sizer, proportion = 1,
292  flag = wx.EXPAND)
293 
294  # define new location and mapset
295  manage_sizer.Add(item = self.ldefine, proportion = 0,
296  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
297  border = 3)
298  manage_sizer.Add(item = self.bwizard, proportion = 0,
299  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
300  border = 5)
301  manage_sizer.Add(item = self.lcreate, proportion = 0,
302  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
303  border = 3)
304  manage_sizer.Add(item = self.bmapset, proportion = 0,
305  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
306  border = 5)
307  manage_sizer.Add(item = self.lmanageloc, proportion = 0,
308  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
309  border = 3)
310  manage_sizer.Add(item = self.manageloc, proportion = 0,
311  flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
312  border = 5)
313 
314  # location sizer
315  location_sizer.Add(item = select_boxsizer, proportion = 1,
316  flag = wx.LEFT | wx.RIGHT | wx.EXPAND,
317  border = 3)
318  location_sizer.Add(item = manage_sizer, proportion = 0,
319  flag = wx.RIGHT | wx.EXPAND,
320  border = 3)
321 
322  # buttons
323  btns_sizer.Add(item = self.bstart, proportion = 0,
324  flag = wx.ALIGN_CENTER_HORIZONTAL |
325  wx.ALIGN_CENTER_VERTICAL |
326  wx.ALL,
327  border = 5)
328  btns_sizer.Add(item = self.bexit, proportion = 0,
329  flag = wx.ALIGN_CENTER_HORIZONTAL |
330  wx.ALIGN_CENTER_VERTICAL |
331  wx.ALL,
332  border = 5)
333  btns_sizer.Add(item = self.bhelp, proportion = 0,
334  flag = wx.ALIGN_CENTER_HORIZONTAL |
335  wx.ALIGN_CENTER_VERTICAL |
336  wx.ALL,
337  border = 5)
338 
339  # main sizer
340  sizer.Add(item = self.hbitmap,
341  proportion = 0,
342  flag = wx.ALIGN_CENTER_VERTICAL |
343  wx.ALIGN_CENTER_HORIZONTAL |
344  wx.ALL,
345  border = 3) # image
346  sizer.Add(item = self.lwelcome, # welcome message
347  proportion = 0,
348  flag = wx.ALIGN_CENTER_VERTICAL |
349  wx.ALIGN_CENTER_HORIZONTAL |
350  wx.BOTTOM,
351  border=1)
352  sizer.Add(item = self.ltitle, # title
353  proportion = 0,
354  flag = wx.ALIGN_CENTER_VERTICAL |
355  wx.ALIGN_CENTER_HORIZONTAL)
356  sizer.Add(item = dbase_sizer, proportion = 0,
357  flag = wx.ALIGN_CENTER_HORIZONTAL |
358  wx.RIGHT | wx.LEFT | wx.EXPAND,
359  border = 20) # GISDBASE setting
360  sizer.Add(item = location_sizer, proportion = 1,
361  flag = wx.RIGHT | wx.LEFT | wx.EXPAND,
362  border = 1)
363  sizer.Add(item = btns_sizer, proportion = 0,
364  flag = wx.ALIGN_CENTER_VERTICAL |
365  wx.ALIGN_CENTER_HORIZONTAL |
366  wx.RIGHT | wx.LEFT,
367  border = 1)
368 
369  self.panel.SetAutoLayout(True)
370  self.panel.SetSizer(sizer)
371  sizer.Fit(self.panel)
372  sizer.SetSizeHints(self)
373 
374  self.Layout()
375 
376  def _readGisRC(self):
377  """
378  Read variables from $HOME/.grassrc6 file
379  """
380 
381  grassrc = {}
382 
383  gisrc = os.getenv("GISRC")
384 
385  if gisrc and os.path.isfile(gisrc):
386  try:
387  rc = open(gisrc, "r")
388  for line in rc.readlines():
389  try:
390  key, val = line.split(":", 1)
391  except ValueError, e:
392  sys.stderr.write(_('Invalid line in GISRC file (%s):%s\n' % \
393  (e, line)))
394  grassrc[key.strip()] = DecodeString(val.strip())
395  finally:
396  rc.close()
397 
398  return grassrc
399 
400  def GetRCValue(self, value):
401  """!Return GRASS variable (read from GISRC)
402  """
403  if self.grassrc.has_key(value):
404  return self.grassrc[value]
405  else:
406  return None
407 
408  def OnWizard(self, event):
409  """!Location wizard started"""
410  from location_wizard.wizard import LocationWizard
411  gWizard = LocationWizard(parent = self,
412  grassdatabase = self.tgisdbase.GetValue())
413  if gWizard.location != None:
414  self.tgisdbase.SetValue(gWizard.grassdatabase)
415  self.OnSetDatabase(None)
416  self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
417  self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
418  self.lbmapsets.SetSelection(0)
419  self.SetLocation(self.gisdbase, gWizard.location, 'PERMANENT')
420  if gWizard.georeffile:
421  message = _("Do you want to import <%(name)s> to the newly created location? "
422  "The location's default region will be set from this imported "
423  "map.") % {'name': gWizard.georeffile}
424  dlg = wx.MessageDialog(parent = self,
425  message = message,
426  caption = _("Import data?"),
427  style = wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
428  dlg.CenterOnScreen()
429  if dlg.ShowModal() == wx.ID_YES:
430  self.ImportFile(gWizard.georeffile)
431  else:
432  self.SetDefaultRegion(location = gWizard.location)
433  dlg.Destroy()
434  else:
435  self.SetDefaultRegion(location = gWizard.location)
436 
437  dlg = TextEntryDialog(parent=self,
438  message=_("Do you want to create new mapset?"),
439  caption=_("Create new mapset"),
440  defaultValue=self._getDefaultMapsetName(),
441  validator=GenericValidator(grass.legal_name, self._nameValidationFailed),
442  style=wx.OK | wx.CANCEL | wx.HELP)
443  help = dlg.FindWindowById(wx.ID_HELP)
444  help.Bind(wx.EVT_BUTTON, self.OnHelp)
445  if dlg.ShowModal() == wx.ID_OK:
446  mapsetName = dlg.GetValue()
447  self.CreateNewMapset(mapsetName)
448 
449  def SetDefaultRegion(self, location):
450  """!Asks to set default region."""
451  dlg = wx.MessageDialog(parent = self,
452  message = _("Do you want to set the default "
453  "region extents and resolution now?"),
454  caption = _("Location <%s> created") % location,
455  style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
456  dlg.CenterOnScreen()
457  if dlg.ShowModal() == wx.ID_YES:
458  dlg.Destroy()
459  defineRegion = RegionDef(self, location = location)
460  defineRegion.CenterOnScreen()
461  defineRegion.ShowModal()
462  defineRegion.Destroy()
463  else:
464  dlg.Destroy()
465 
466  def ImportFile(self, filePath):
467  """!Tries to import file as vector or raster.
468 
469  If successfull sets default region from imported map.
470  """
471  mapName = os.path.splitext(os.path.basename(filePath))[0]
472  vectors = RunCommand('v.in.ogr', dsn = filePath, flags = 'l',
473  read = True)
474 
475  wx.BeginBusyCursor()
476  wx.Yield()
477  if mapName in vectors:
478  # vector detected
479  returncode, error = RunCommand('v.in.ogr', dsn = filePath, output = mapName,
480  getErrorMsg = True)
481  else:
482  returncode, error = RunCommand('r.in.gdal', input = filePath, output = mapName,
483  getErrorMsg = True)
484  wx.EndBusyCursor()
485 
486  if returncode != 0:
487  GError(parent = self,
488  message = _("Import of <%(name)s> failed.\n"
489  "Reason: %(msg)s") % ({'name': filePath, 'msg': error}))
490  else:
491  GMessage(message = _("Data file <%(name)s> imported successfully.") % {'name': filePath},
492  parent = self)
493  if not grass.find_file(element = 'cell', name = mapName)['fullname'] and \
494  not grass.find_file(element = 'vector', name = mapName)['fullname']:
495  GError(parent = self,
496  message = _("Map <%s> not found.") % mapName)
497  else:
498  if mapName in vectors:
499  args = {'vect' : mapName}
500  else:
501  args = {'rast' : mapName}
502  RunCommand('g.region', flags = 's', parent = self, **args)
503 
504  def OnManageLoc(self, event):
505  """!Location management choice control handler
506  """
507  sel = event.GetSelection()
508  if sel == 0:
509  self.RenameMapset()
510  elif sel == 1:
511  self.RenameLocation()
512  elif sel == 2:
513  self.DeleteMapset()
514  elif sel == 3:
515  self.DeleteLocation()
516 
517  event.Skip()
518 
519  def RenameMapset(self):
520  """!Rename selected mapset
521  """
522  location = self.listOfLocations[self.lblocations.GetSelection()]
523  mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
524  if mapset == 'PERMANENT':
525  GMessage(parent = self,
526  message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
527  'This mapset cannot be renamed.'))
528  return
529 
530  dlg = TextEntryDialog(parent = self,
531  message = _('Current name: %s\n\nEnter new name:') % mapset,
532  caption = _('Rename selected mapset'),
533  validator = GenericValidator(grass.legal_name, self._nameValidationFailed))
534 
535  if dlg.ShowModal() == wx.ID_OK:
536  newmapset = dlg.GetValue()
537  if newmapset == mapset:
538  dlg.Destroy()
539  return
540 
541  if newmapset in self.listOfMapsets:
542  wx.MessageBox(parent = self,
543  caption = _('Message'),
544  message = _('Unable to rename mapset.\n\n'
545  'Mapset <%s> already exists in location.') % newmapset,
546  style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
547  else:
548  try:
549  os.rename(os.path.join(self.gisdbase, location, mapset),
550  os.path.join(self.gisdbase, location, newmapset))
551  self.OnSelectLocation(None)
552  self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
553  except StandardError, e:
554  wx.MessageBox(parent = self,
555  caption = _('Error'),
556  message = _('Unable to rename mapset.\n\n%s') % e,
557  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
558 
559  dlg.Destroy()
560 
561  def RenameLocation(self):
562  """!Rename selected location
563  """
564  location = self.listOfLocations[self.lblocations.GetSelection()]
565 
566  dlg = TextEntryDialog(parent = self,
567  message = _('Current name: %s\n\nEnter new name:') % location,
568  caption = _('Rename selected location'),
569  validator = GenericValidator(grass.legal_name, self._nameValidationFailed))
570 
571  if dlg.ShowModal() == wx.ID_OK:
572  newlocation = dlg.GetValue()
573  if newlocation == location:
574  dlg.Destroy()
575  return
576 
577  if newlocation in self.listOfLocations:
578  wx.MessageBox(parent = self,
579  caption = _('Message'),
580  message = _('Unable to rename location.\n\n'
581  'Location <%s> already exists in GRASS database.') % newlocation,
582  style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
583  else:
584  try:
585  os.rename(os.path.join(self.gisdbase, location),
586  os.path.join(self.gisdbase, newlocation))
587  self.UpdateLocations(self.gisdbase)
588  self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
589  self.UpdateMapsets(newlocation)
590  except StandardError, e:
591  wx.MessageBox(parent = self,
592  caption = _('Error'),
593  message = _('Unable to rename location.\n\n%s') % e,
594  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
595 
596  dlg.Destroy()
597 
598  def DeleteMapset(self):
599  """!Delete selected mapset
600  """
601  location = self.listOfLocations[self.lblocations.GetSelection()]
602  mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
603  if mapset == 'PERMANENT':
604  GMessage(parent = self,
605  message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
606  'This mapset cannot be deleted.'))
607  return
608 
609  dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting mapset <%(mapset)s> "
610  "from location <%(location)s>?\n\n"
611  "ALL MAPS included in this mapset will be "
612  "PERMANENTLY DELETED!") % {'mapset' : mapset,
613  'location' : location},
614  caption = _("Delete selected mapset"),
615  style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
616 
617  if dlg.ShowModal() == wx.ID_YES:
618  try:
619  shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
620  self.OnSelectLocation(None)
621  self.lbmapsets.SetSelection(0)
622  except:
623  wx.MessageBox(message = _('Unable to delete mapset'))
624 
625  dlg.Destroy()
626 
627  def DeleteLocation(self):
628  """
629  Delete selected location
630  """
631 
632  location = self.listOfLocations[self.lblocations.GetSelection()]
633 
634  dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting "
635  "location <%s>?\n\n"
636  "ALL MAPS included in this location will be "
637  "PERMANENTLY DELETED!") % (location),
638  caption = _("Delete selected location"),
639  style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
640 
641  if dlg.ShowModal() == wx.ID_YES:
642  try:
643  shutil.rmtree(os.path.join(self.gisdbase, location))
644  self.UpdateLocations(self.gisdbase)
645  self.lblocations.SetSelection(0)
646  self.OnSelectLocation(None)
647  self.lbmapsets.SetSelection(0)
648  except:
649  wx.MessageBox(message = _('Unable to delete location'))
650 
651  dlg.Destroy()
652 
653  def UpdateLocations(self, dbase):
654  """!Update list of locations"""
655  try:
656  self.listOfLocations = GetListOfLocations(dbase)
657  except UnicodeEncodeError:
658  wx.MessageBox(parent = self, caption = _("Error"),
659  message = _("Unable to set GRASS database. "
660  "Check your locale settings."),
661  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
662 
663  self.lblocations.Clear()
664  self.lblocations.InsertItems(self.listOfLocations, 0)
665 
666  if len(self.listOfLocations) > 0:
667  self.lblocations.SetSelection(0)
668  else:
669  self.lblocations.SetSelection(wx.NOT_FOUND)
670 
671  return self.listOfLocations
672 
673  def UpdateMapsets(self, location):
674  """!Update list of mapsets"""
675  self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
676 
677  self.listOfMapsetsSelectable = list()
678  self.listOfMapsets = GetListOfMapsets(self.gisdbase, location)
679 
680  self.lbmapsets.Clear()
681 
682  # disable mapset with denied permission
683  locationName = os.path.basename(location)
684 
685  ret = RunCommand('g.mapset',
686  read = True,
687  flags = 'l',
688  location = locationName,
689  gisdbase = self.gisdbase)
690 
691  if ret:
692  for line in ret.splitlines():
693  self.listOfMapsetsSelectable += line.split(' ')
694  else:
695  RunCommand("g.gisenv",
696  set = "GISDBASE=%s" % self.gisdbase)
697  RunCommand("g.gisenv",
698  set = "LOCATION_NAME=%s" % locationName)
699  RunCommand("g.gisenv",
700  set = "MAPSET=PERMANENT")
701  # first run only
702  self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
703 
704  disabled = []
705  idx = 0
706  for mapset in self.listOfMapsets:
707  if mapset not in self.listOfMapsetsSelectable or \
708  os.path.isfile(os.path.join(self.gisdbase,
709  locationName,
710  mapset, ".gislock")):
711  disabled.append(idx)
712  idx += 1
713 
714  self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
715 
716  return self.listOfMapsets
717 
718  def OnSelectLocation(self, event):
719  """!Location selected"""
720  if event:
721  self.lblocations.SetSelection(event.GetIndex())
722 
723  if self.lblocations.GetSelection() != wx.NOT_FOUND:
724  self.UpdateMapsets(os.path.join(self.gisdbase,
725  self.listOfLocations[self.lblocations.GetSelection()]))
726  else:
727  self.listOfMapsets = []
728 
729  disabled = []
730  idx = 0
731  try:
732  locationName = self.listOfLocations[self.lblocations.GetSelection()]
733  except IndexError:
734  locationName = ''
735 
736  for mapset in self.listOfMapsets:
737  if mapset not in self.listOfMapsetsSelectable or \
738  os.path.isfile(os.path.join(self.gisdbase,
739  locationName,
740  mapset, ".gislock")):
741  disabled.append(idx)
742  idx += 1
743 
744  self.lbmapsets.Clear()
745  self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
746 
747  if len(self.listOfMapsets) > 0:
748  self.lbmapsets.SetSelection(0)
749  if locationName:
750  # enable start button when location and mapset is selected
751  self.bstart.Enable()
752  self.bmapset.Enable()
753  self.manageloc.Enable()
754  else:
755  self.lbmapsets.SetSelection(wx.NOT_FOUND)
756  self.bstart.Enable(False)
757  self.bmapset.Enable(False)
758  self.manageloc.Enable(False)
759 
760  def OnSelectMapset(self, event):
761  """!Mapset selected"""
762  self.lbmapsets.SetSelection(event.GetIndex())
763 
764  if event.GetText() not in self.listOfMapsetsSelectable:
765  self.lbmapsets.SetSelection(self.FormerMapsetSelection)
766  else:
767  self.FormerMapsetSelection = event.GetIndex()
768  event.Skip()
769 
770  def OnSetDatabase(self, event):
771  """!Database set"""
772  self.gisdbase = self.tgisdbase.GetValue()
773 
774  self.UpdateLocations(self.gisdbase)
775 
776  self.OnSelectLocation(None)
777 
778  def OnBrowse(self, event):
779  """'Browse' button clicked"""
780  if not event:
781  defaultPath = os.getenv('HOME')
782  else:
783  defaultPath = ""
784 
785  dlg = wx.DirDialog(parent = self, message = _("Choose GIS Data Directory"),
786  defaultPath = defaultPath, style = wx.DD_DEFAULT_STYLE)
787 
788  if dlg.ShowModal() == wx.ID_OK:
789  self.gisdbase = dlg.GetPath()
790  self.tgisdbase.SetValue(self.gisdbase)
791  self.OnSetDatabase(event)
792 
793  dlg.Destroy()
794 
795  def OnCreateMapset(self, event):
796  """!Create new mapset"""
797 
798  dlg = TextEntryDialog(parent = self,
799  message = _('Enter name for new mapset:'),
800  caption = _('Create new mapset'),
801  defaultValue = self._getDefaultMapsetName(),
802  validator = GenericValidator(grass.legal_name, self._nameValidationFailed))
803 
804  if dlg.ShowModal() == wx.ID_OK:
805  mapset = dlg.GetValue()
806  return self.CreateNewMapset(mapset = mapset)
807  else:
808  return False
809 
810  def CreateNewMapset(self, mapset):
811  if mapset in self.listOfMapsets:
812  GMessage(parent = self,
813  message = _("Mapset <%s> already exists.") % mapset)
814  return False
815 
816  try:
817  self.gisdbase = self.tgisdbase.GetValue()
818  location = self.listOfLocations[self.lblocations.GetSelection()]
819  os.mkdir(os.path.join(self.gisdbase, location, mapset))
820  # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
821  shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
822  os.path.join(self.gisdbase, location, mapset))
823  # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
824  self.OnSelectLocation(None)
825  self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
826  self.bstart.SetFocus()
827  return True
828  except StandardError, e:
829  GError(parent = self,
830  message = _("Unable to create new mapset: %s") % e,
831  showTraceback = False)
832  return False
833 
834  def OnStart(self, event):
835  """'Start GRASS' button clicked"""
836  dbase = self.tgisdbase.GetValue()
837  location = self.listOfLocations[self.lblocations.GetSelection()]
838  mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
839 
840  lockfile = os.path.join(dbase, location, mapset, '.gislock')
841  if os.path.isfile(lockfile):
842  dlg = wx.MessageDialog(parent = self,
843  message = _("GRASS is already running in selected mapset <%(mapset)s>\n"
844  "(file %(lock)s found).\n\n"
845  "Concurrent use not allowed.\n\n"
846  "Do you want to try to remove .gislock (note that you "
847  "need permission for this operation) and continue?") %
848  { 'mapset' : mapset, 'lock' : lockfile },
849  caption = _("Lock file found"),
850  style = wx.YES_NO | wx.NO_DEFAULT |
851  wx.ICON_QUESTION | wx.CENTRE)
852 
853  ret = dlg.ShowModal()
854  dlg.Destroy()
855  if ret == wx.ID_YES:
856  dlg1 = wx.MessageDialog(parent = self,
857  message = _("ARE YOU REALLY SURE?\n\n"
858  "If you really are running another GRASS session doing this "
859  "could corrupt your data. Have another look in the processor "
860  "manager just to be sure..."),
861  caption = _("Lock file found"),
862  style = wx.YES_NO | wx.NO_DEFAULT |
863  wx.ICON_QUESTION | wx.CENTRE)
864 
865  ret = dlg1.ShowModal()
866  dlg1.Destroy()
867 
868  if ret == wx.ID_YES:
869  try:
870  os.remove(lockfile)
871  except IOError, e:
872  GError(_("Unable to remove '%(lock)s'.\n\n"
873  "Details: %(reason)s") % { 'lock' : lockfile, 'reason' : e})
874  else:
875  return
876  else:
877  return
878  self.SetLocation(dbase, location, mapset)
879  self.ExitSuccessfully()
880 
881  def SetLocation(self, dbase, location, mapset):
882  RunCommand("g.gisenv",
883  set = "GISDBASE=%s" % dbase)
884  RunCommand("g.gisenv",
885  set = "LOCATION_NAME=%s" % location)
886  RunCommand("g.gisenv",
887  set = "MAPSET=%s" % mapset)
888 
889 
890  def _getDefaultMapsetName(self):
891  """!Returns default name for mapset."""
892  try:
893  defaultName = getpass.getuser()
894  defaultName.encode('ascii') # raise error if not ascii (not valid mapset name)
895  except: # whatever might go wrong
896  defaultName = 'user'
897 
898  return defaultName
899 
900  def ExitSuccessfully(self):
901  self.Destroy()
902  sys.exit(0)
903 
904  def OnExit(self, event):
905  """'Exit' button clicked"""
906  self.Destroy()
907  sys.exit (2)
908 
909  def OnHelp(self, event):
910  """'Help' button clicked"""
911  # help text in lib/init/helptext.html
912  filePath = os.path.join(self.gisbase, "docs", "html", "helptext.html")
913  import webbrowser
914  webbrowser.open(filePath)
915 
916  def OnCloseWindow(self, event):
917  """!Close window event"""
918  event.Skip()
919  sys.exit(2)
920 
921  def _nameValidationFailed(self, ctrl):
922  message = _("Name <%(name)s> is not a valid name for location or mapset. "
923  "Please use only ASCII characters excluding %(chars)s "
924  "and space.") % {'name': ctrl.GetValue(), 'chars': '/"\'@,=*~'}
925  GError(parent=self, message=message, caption=_("Invalid name"))
926 
927 class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
928  """!Use wx.ListCtrl instead of wx.ListBox, different style for
929  non-selectable items (e.g. mapsets with denied permission)"""
930  def __init__(self, parent, id, size,
931  choices, disabled = []):
932  wx.ListCtrl.__init__(self, parent, id, size = size,
933  style = wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
934  wx.BORDER_SUNKEN)
935 
936  listmix.ListCtrlAutoWidthMixin.__init__(self)
937 
938  self.InsertColumn(0, '')
939 
940  self.selected = wx.NOT_FOUND
941 
942  self._LoadData(choices, disabled)
943 
944  def _LoadData(self, choices, disabled = []):
945  """!Load data into list
946 
947  @param choices list of item
948  @param disabled list of indeces of non-selectable items
949  """
950  idx = 0
951  for item in choices:
952  index = self.InsertStringItem(sys.maxint, item)
953  self.SetStringItem(index, 0, item)
954 
955  if idx in disabled:
956  self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
957  idx += 1
958 
959  def Clear(self):
960  self.DeleteAllItems()
961 
962  def InsertItems(self, choices, pos, disabled = []):
963  self._LoadData(choices, disabled)
964 
965  def SetSelection(self, item, force = False):
966  if item != wx.NOT_FOUND and \
967  (platform.system() != 'Windows' or force):
968  ### Windows -> FIXME
969  self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
970 
971  self.selected = item
972 
973  def GetSelection(self):
974  return self.selected
975 
976 class StartUp(wx.App):
977  """!Start-up application"""
978 
979  def OnInit(self):
980  if not globalvar.CheckWxVersion([2, 9]):
981  wx.InitAllImageHandlers()
982  StartUp = GRASSStartup()
983  StartUp.CenterOnScreen()
984  self.SetTopWindow(StartUp)
985  StartUp.Show()
986 
987  if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
988  wx.MessageBox(parent = StartUp,
989  caption = _('Starting GRASS for the first time'),
990  message = _('GRASS needs a directory in which to store its data. '
991  'Create one now if you have not already done so. '
992  'A popular choice is "grassdata", located in '
993  'your home directory.'),
994  style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
995 
996  StartUp.OnBrowse(None)
997 
998  return 1
999 
1000 if __name__ == "__main__":
1001  if os.getenv("GISBASE") is None:
1002  sys.exit("Failed to start GUI, GRASS GIS is not running.")
1003 
1004  import gettext
1005  gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
1006 
1007  GRASSStartUp = StartUp(0)
1008  GRASSStartUp.MainLoop()
def GetListOfLocations
Get list of GRASS locations in given dbase.
Definition: core/utils.py:639
def DecodeString
Decode string using system encoding.
Definition: gcmd.py:69
wxGUI command interface
def CheckWxVersion
Check wx version.
Definition: globalvar.py:36
Location wizard - dialogs.
GRASS start-up screen.
Definition: gis_set.py:55
Location wizard - creates a new GRASS Location.
def OnSelectLocation
Location selected.
Definition: gis_set.py:718
def OnSelectMapset
Mapset selected.
Definition: gis_set.py:760
Start-up application.
Definition: gis_set.py:976
def GetListOfMapsets
Get list of mapsets in given GRASS location.
Definition: core/utils.py:662
Core GUI widgets.
def _set_properties
Set frame properties.
Definition: gis_set.py:190
def OnManageLoc
Location management choice control handler.
Definition: gis_set.py:504
def RenameMapset
Rename selected mapset.
Definition: gis_set.py:519
def OnCreateMapset
Create new mapset.
Definition: gis_set.py:795
select_box
crashes when LOCATION doesn&#39;t exist
Definition: gis_set.py:101
def OnWizard
Location wizard started.
Definition: gis_set.py:408
def OnSetDatabase
Database set.
Definition: gis_set.py:770
Various dialogs used in wxGUI.
def UpdateMapsets
Update list of mapsets.
Definition: gis_set.py:673
def split
Platform spefic shlex.split.
Definition: core/utils.py:37
def ImportFile
Tries to import file as vector or raster.
Definition: gis_set.py:466
def _LoadData
Load data into list.
Definition: gis_set.py:944
selected
Windows -&gt; FIXME.
Definition: gis_set.py:940
def GetRCValue
Return GRASS variable (read from GISRC)
Definition: gis_set.py:400
def DeleteMapset
Delete selected mapset.
Definition: gis_set.py:598
Help window.
def SetDefaultRegion
Asks to set default region.
Definition: gis_set.py:449
Misc utilities for wxGUI.
Use wx.ListCtrl instead of wx.ListBox, different style for non-selectable items (e.g.
Definition: gis_set.py:927
def UpdateLocations
Update list of locations.
Definition: gis_set.py:653
def OnCloseWindow
Close window event.
Definition: gis_set.py:916
def RenameLocation
Rename selected location.
Definition: gis_set.py:561
def RunCommand
Run GRASS command.
Definition: gcmd.py:625
def _getDefaultMapsetName
Returns default name for mapset.
Definition: gis_set.py:890