gis_set.py 45 KB

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