gis_set.py 43 KB

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