gis_set.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  25. if __name__ == "__main__":
  26. gui_wx_path = os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython')
  27. if gui_wx_path not in sys.path:
  28. sys.path.append(gui_wx_path)
  29. from core import globalvar
  30. import wx
  31. import wx.lib.mixins.listctrl as listmix
  32. import wx.lib.scrolledpanel as scrolled
  33. from grass.script import core as grass
  34. from gui_core.ghelp import HelpFrame
  35. from core.gcmd import GMessage, GError, DecodeString, RunCommand, GWarning
  36. from core.utils import GetListOfLocations, GetListOfMapsets, _
  37. from location_wizard.dialogs import RegionDef
  38. from gui_core.dialogs import TextEntryDialog
  39. from gui_core.widgets import GenericValidator
  40. sys.stderr = codecs.getwriter('utf8')(sys.stderr)
  41. class GRASSStartup(wx.Frame):
  42. """!GRASS start-up screen"""
  43. def __init__(self, parent = None, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE):
  44. #
  45. # GRASS variables
  46. #
  47. self.gisbase = os.getenv("GISBASE")
  48. self.grassrc = self._readGisRC()
  49. self.gisdbase = self.GetRCValue("GISDBASE")
  50. #
  51. # list of locations/mapsets
  52. #
  53. self.listOfLocations = []
  54. self.listOfMapsets = []
  55. self.listOfMapsetsSelectable = []
  56. wx.Frame.__init__(self, parent = parent, id = id, style = style)
  57. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  58. self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  59. # i18N
  60. #
  61. # graphical elements
  62. #
  63. # image
  64. try:
  65. if os.getenv('ISISROOT'):
  66. name = os.path.join(globalvar.ETCDIR, "gui", "images", "startup_banner_isis.png")
  67. else:
  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.BitmapFromImage(wx.EmptyImage(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. GRASSStartUp = StartUp(0)
  875. GRASSStartUp.MainLoop()