gis_set.py 45 KB

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