gis_set.py 41 KB

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