gis_set.py 43 KB

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