guiutils.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. """
  2. @package startup.guiutils
  3. @brief General GUI-dependent utilities for GUI startup of GRASS GIS
  4. (C) 2018 by Vaclav Petras the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Vaclav Petras <wenzeslaus gmail com>
  8. @author Linda Kladivova <l.kladivova@seznam.cz>
  9. This is for code which depend on something from GUI (wx or wxGUI).
  10. """
  11. import os
  12. import sys
  13. import wx
  14. from grass.grassdb.checks import (
  15. is_mapset_locked,
  16. get_mapset_lock_info,
  17. is_mapset_name_valid,
  18. is_location_name_valid,
  19. get_mapset_name_invalid_reason,
  20. get_location_name_invalid_reason,
  21. get_reason_mapset_not_removable,
  22. get_reasons_mapsets_not_removable,
  23. get_reasons_location_not_removable,
  24. get_reasons_locations_not_removable,
  25. get_reasons_grassdb_not_removable
  26. )
  27. from grass.grassdb.create import create_mapset, get_default_mapset_name
  28. from grass.grassdb.manage import (
  29. delete_mapset,
  30. delete_location,
  31. delete_grassdb,
  32. rename_mapset,
  33. rename_location,
  34. )
  35. from core import globalvar
  36. from core.gcmd import GError, GMessage, DecodeString, RunCommand
  37. from gui_core.dialogs import TextEntryDialog
  38. from location_wizard.dialogs import RegionDef
  39. from gui_core.widgets import GenericValidator
  40. def SetSessionMapset(database, location, mapset):
  41. """Sets database, location and mapset for the current session"""
  42. RunCommand("g.gisenv", set="GISDBASE=%s" % database)
  43. RunCommand("g.gisenv", set="LOCATION_NAME=%s" % location)
  44. RunCommand("g.gisenv", set="MAPSET=%s" % mapset)
  45. class MapsetDialog(TextEntryDialog):
  46. def __init__(self, parent=None, default=None, message=None, caption=None,
  47. database=None, location=None):
  48. self.database = database
  49. self.location = location
  50. validator = GenericValidator(self._isMapsetNameValid,
  51. self._showMapsetNameInvalidReason)
  52. TextEntryDialog.__init__(
  53. self, parent=parent,
  54. message=message,
  55. caption=caption,
  56. defaultValue=default,
  57. validator=validator,
  58. )
  59. def _showMapsetNameInvalidReason(self, ctrl):
  60. message = get_mapset_name_invalid_reason(self.database,
  61. self.location,
  62. ctrl.GetValue())
  63. GError(parent=self, message=message, caption=_("Invalid mapset name"))
  64. def _isMapsetNameValid(self, text):
  65. """Check whether user's input location is valid or not."""
  66. return is_mapset_name_valid(self.database, self.location, text)
  67. class LocationDialog(TextEntryDialog):
  68. def __init__(self, parent=None, default=None, message=None, caption=None,
  69. database=None):
  70. self.database = database
  71. validator = GenericValidator(self._isLocationNameValid,
  72. self._showLocationNameInvalidReason)
  73. TextEntryDialog.__init__(
  74. self, parent=parent,
  75. message=message,
  76. caption=caption,
  77. defaultValue=default,
  78. validator=validator,
  79. )
  80. def _showLocationNameInvalidReason(self, ctrl):
  81. message = get_location_name_invalid_reason(self.database,
  82. ctrl.GetValue())
  83. GError(parent=self, message=message, caption=_("Invalid location name"))
  84. def _isLocationNameValid(self, text):
  85. """Check whether user's input location is valid or not."""
  86. return is_location_name_valid(self.database, text)
  87. # TODO: similar to (but not the same as) read_gisrc function in grass.py
  88. def read_gisrc():
  89. """Read variables from a current GISRC file
  90. Returns a dictionary representation of the file content.
  91. """
  92. grassrc = {}
  93. gisrc = os.getenv("GISRC")
  94. if gisrc and os.path.isfile(gisrc):
  95. try:
  96. rc = open(gisrc, "r")
  97. for line in rc.readlines():
  98. try:
  99. key, val = line.split(":", 1)
  100. except ValueError as e:
  101. sys.stderr.write(
  102. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  103. grassrc[key.strip()] = DecodeString(val.strip())
  104. finally:
  105. rc.close()
  106. return grassrc
  107. def GetVersion():
  108. """Gets version and revision
  109. Returns tuple `(version, revision)`. For standard releases revision
  110. is an empty string.
  111. Revision string is currently wrapped in parentheses with added
  112. leading space. This is an implementation detail and legacy and may
  113. change anytime.
  114. """
  115. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  116. versionLine = versionFile.readline().rstrip('\n')
  117. versionFile.close()
  118. try:
  119. grassVersion, grassRevision = versionLine.split(' ', 1)
  120. if grassVersion.endswith('dev'):
  121. grassRevisionStr = ' (%s)' % grassRevision
  122. else:
  123. grassRevisionStr = ''
  124. except ValueError:
  125. grassVersion = versionLine
  126. grassRevisionStr = ''
  127. return (grassVersion, grassRevisionStr)
  128. def create_mapset_interactively(guiparent, grassdb, location):
  129. """
  130. Create new mapset
  131. """
  132. dlg = MapsetDialog(
  133. parent=guiparent,
  134. default=get_default_mapset_name(),
  135. message=_("Name for the new mapset:"),
  136. caption=_("Create new mapset"),
  137. database=grassdb,
  138. location=location,
  139. )
  140. mapset = None
  141. if dlg.ShowModal() == wx.ID_OK:
  142. mapset = dlg.GetValue()
  143. try:
  144. create_mapset(grassdb, location, mapset)
  145. except OSError as err:
  146. mapset = None
  147. GError(
  148. parent=guiparent,
  149. message=_("Unable to create new mapset: {}").format(err),
  150. showTraceback=False,
  151. )
  152. dlg.Destroy()
  153. return mapset
  154. def create_location_interactively(guiparent, grassdb):
  155. """
  156. Create new location using Location Wizard.
  157. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  158. by default or another mapset a user created and may want to switch to.
  159. """
  160. from location_wizard.wizard import LocationWizard
  161. gWizard = LocationWizard(parent=guiparent,
  162. grassdatabase=grassdb)
  163. if gWizard.location is None:
  164. gWizard_output = (None, None, None)
  165. # Returns Nones after Cancel
  166. return gWizard_output
  167. if gWizard.georeffile:
  168. message = _(
  169. "Do you want to import {}"
  170. "to the newly created location?"
  171. ).format(gWizard.georeffile)
  172. dlg = wx.MessageDialog(parent=guiparent,
  173. message=message,
  174. caption=_("Import data?"),
  175. style=wx.YES_NO | wx.YES_DEFAULT |
  176. wx.ICON_QUESTION)
  177. dlg.CenterOnParent()
  178. if dlg.ShowModal() == wx.ID_YES:
  179. import_file(guiparent, gWizard.georeffile)
  180. dlg.Destroy()
  181. if gWizard.default_region:
  182. defineRegion = RegionDef(guiparent, location=gWizard.location)
  183. defineRegion.CenterOnParent()
  184. defineRegion.ShowModal()
  185. defineRegion.Destroy()
  186. if gWizard.user_mapset:
  187. mapset = create_mapset_interactively(guiparent,
  188. gWizard.grassdatabase,
  189. gWizard.location)
  190. # Returns database and location created by user
  191. # and a mapset user may want to switch to
  192. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  193. mapset)
  194. else:
  195. # Returns PERMANENT mapset when user mapset not defined
  196. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  197. "PERMANENT")
  198. return gWizard_output
  199. def rename_mapset_interactively(guiparent, grassdb, location, mapset):
  200. """Rename mapset with user interaction.
  201. Exceptions during renaming are handled in get_reason_mapset_not_removable
  202. function.
  203. Returns newmapset if there was a change or None if the mapset cannot be
  204. renamed (see reasons given by get_reason_mapset_not_removable
  205. function) or if another error was encountered.
  206. """
  207. newmapset = None
  208. # Check selected mapset
  209. message = get_reason_mapset_not_removable(grassdb, location, mapset,
  210. check_permanent=True)
  211. if message:
  212. dlg = wx.MessageDialog(
  213. parent=guiparent,
  214. message=_(
  215. "Cannot rename mapset <{mapset}> for the following reason:\n\n"
  216. "{reason}\n\n"
  217. "No mapset will be renamed."
  218. ).format(mapset=mapset, reason=message),
  219. caption=_("Unable to rename selected mapset"),
  220. style=wx.OK | wx.ICON_WARNING
  221. )
  222. dlg.ShowModal()
  223. dlg.Destroy()
  224. return newmapset
  225. # Display question dialog
  226. dlg = MapsetDialog(
  227. parent=guiparent,
  228. default=mapset,
  229. message=_("Current name: {}\n\nEnter new name:").format(mapset),
  230. caption=_("Rename selected mapset"),
  231. database=grassdb,
  232. location=location,
  233. )
  234. if dlg.ShowModal() == wx.ID_OK:
  235. newmapset = dlg.GetValue()
  236. try:
  237. rename_mapset(grassdb, location, mapset, newmapset)
  238. except OSError as err:
  239. newmapset = None
  240. wx.MessageBox(
  241. parent=guiparent,
  242. caption=_("Error"),
  243. message=_("Unable to rename mapset.\n\n{}").format(err),
  244. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  245. )
  246. dlg.Destroy()
  247. return newmapset
  248. def rename_location_interactively(guiparent, grassdb, location):
  249. """Rename location with user interaction.
  250. Exceptions during renaming are handled in get_reasons_location_not_removable
  251. function.
  252. Returns newlocation if there was a change or None if the location cannot be
  253. renamed (see reasons given by get_reasons_location_not_removable
  254. function) or if another error was encountered.
  255. """
  256. newlocation = None
  257. # Check selected location
  258. messages = get_reasons_location_not_removable(grassdb, location)
  259. if messages:
  260. dlg = wx.MessageDialog(
  261. parent=guiparent,
  262. message=_(
  263. "Cannot rename location <{location}> for the following reasons:\n\n"
  264. "{reasons}\n\n"
  265. "No location will be renamed."
  266. ).format(location=location, reasons="\n".join(messages)),
  267. caption=_("Unable to rename selected location"),
  268. style=wx.OK | wx.ICON_WARNING
  269. )
  270. dlg.ShowModal()
  271. dlg.Destroy()
  272. return newlocation
  273. # Display question dialog
  274. dlg = LocationDialog(
  275. parent=guiparent,
  276. default=location,
  277. message=_("Current name: {}\n\nEnter new name:").format(location),
  278. caption=_("Rename selected location"),
  279. database=grassdb,
  280. )
  281. if dlg.ShowModal() == wx.ID_OK:
  282. newlocation = dlg.GetValue()
  283. try:
  284. rename_location(grassdb, location, newlocation)
  285. except OSError as err:
  286. newlocation = None
  287. wx.MessageBox(
  288. parent=guiparent,
  289. caption=_("Error"),
  290. message=_("Unable to rename location.\n\n{}").format(err),
  291. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  292. )
  293. dlg.Destroy()
  294. return newlocation
  295. def download_location_interactively(guiparent, grassdb):
  296. """
  297. Download new location using Location Wizard.
  298. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  299. by default or in future it could be the mapset the user may want to
  300. switch to.
  301. """
  302. from startup.locdownload import LocationDownloadDialog
  303. result = (None, None, None)
  304. loc_download = LocationDownloadDialog(parent=guiparent,
  305. database=grassdb)
  306. loc_download.Centre()
  307. loc_download.ShowModal()
  308. if loc_download.GetLocation() is not None:
  309. # Returns database and location created by user
  310. # and a mapset user may want to switch to
  311. result = (grassdb, loc_download.GetLocation(), "PERMANENT")
  312. loc_download.Destroy()
  313. return result
  314. def delete_mapset_interactively(guiparent, grassdb, location, mapset):
  315. """Delete one mapset with user interaction.
  316. This is currently just a convenience wrapper for delete_mapsets_interactively().
  317. """
  318. mapsets = [(grassdb, location, mapset)]
  319. return delete_mapsets_interactively(guiparent, mapsets)
  320. def delete_mapsets_interactively(guiparent, mapsets):
  321. """Delete multiple mapsets with user interaction.
  322. Parameter *mapsets* is a list of tuples (database, location, mapset).
  323. Exceptions during deletation are handled in get_reasons_mapsets_not_removable
  324. function.
  325. Returns True if there was a change, i.e., all mapsets were successfuly
  326. deleted or at least one mapset was deleted.
  327. Returns False if one or more mapsets cannot be deleted (see reasons given
  328. by get_reasons_mapsets_not_removable function) or if an error was
  329. encountered when deleting the first mapset in the list.
  330. """
  331. deletes = []
  332. modified = False
  333. # Check selected mapsets
  334. messages = get_reasons_mapsets_not_removable(mapsets, check_permanent=True)
  335. if messages:
  336. dlg = wx.MessageDialog(
  337. parent=guiparent,
  338. message=_(
  339. "Cannot delete one or more mapsets for the following reasons:\n\n"
  340. "{reasons}\n\n"
  341. "No mapsets will be deleted."
  342. ).format(reasons="\n".join(messages)),
  343. caption=_("Unable to delete selected mapsets"),
  344. style=wx.OK | wx.ICON_WARNING
  345. )
  346. dlg.ShowModal()
  347. dlg.Destroy()
  348. return modified
  349. # No error occurs, create list of mapsets for deleting
  350. for grassdb, location, mapset in mapsets:
  351. mapset_path = os.path.join(grassdb, location, mapset)
  352. deletes.append(mapset_path)
  353. # Display question dialog
  354. dlg = wx.MessageDialog(
  355. parent=guiparent,
  356. message=_(
  357. "Do you want to continue with deleting"
  358. " one or more of the following mapsets?\n\n"
  359. "{deletes}\n\n"
  360. "All maps included in these mapsets will be permanently deleted!"
  361. ).format(deletes="\n".join(deletes)),
  362. caption=_("Delete selected mapsets"),
  363. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  364. )
  365. if dlg.ShowModal() == wx.ID_YES:
  366. try:
  367. for grassdb, location, mapset in mapsets:
  368. delete_mapset(grassdb, location, mapset)
  369. modified = True
  370. dlg.Destroy()
  371. return modified
  372. except OSError as error:
  373. wx.MessageBox(
  374. parent=guiparent,
  375. caption=_("Error when deleting mapsets"),
  376. message=_(
  377. "The following error occured when deleting mapset <{path}>:"
  378. "\n\n{error}\n\n"
  379. "Deleting of mapsets was interrupted."
  380. ).format(
  381. path=os.path.join(grassdb, location, mapset),
  382. error=error,
  383. ),
  384. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  385. )
  386. dlg.Destroy()
  387. return modified
  388. def delete_location_interactively(guiparent, grassdb, location):
  389. """Delete one location with user interaction.
  390. This is currently just a convenience wrapper for delete_locations_interactively().
  391. """
  392. locations = [(grassdb, location)]
  393. return delete_locations_interactively(guiparent, locations)
  394. def delete_locations_interactively(guiparent, locations):
  395. """Delete multiple locations with user interaction.
  396. Parameter *locations* is a list of tuples (database, location).
  397. Exceptions during deletation are handled in get_reasons_locations_not_removable
  398. function.
  399. Returns True if there was a change, i.e., all locations were successfuly
  400. deleted or at least one location was deleted.
  401. Returns False if one or more locations cannot be deleted (see reasons given
  402. by get_reasons_locations_not_removable function) or if an error was
  403. encountered when deleting the first location in the list.
  404. """
  405. deletes = []
  406. modified = False
  407. # Check selected locations
  408. messages = get_reasons_locations_not_removable(locations)
  409. if messages:
  410. dlg = wx.MessageDialog(
  411. parent=guiparent,
  412. message=_(
  413. "Cannot delete one or more locations for the following reasons:\n\n"
  414. "{reasons}\n\n"
  415. "No locations will be deleted."
  416. ).format(reasons="\n".join(messages)),
  417. caption=_("Unable to delete selected locations"),
  418. style=wx.OK | wx.ICON_WARNING
  419. )
  420. dlg.ShowModal()
  421. dlg.Destroy()
  422. return modified
  423. # No error occurs, create list of locations for deleting
  424. for grassdb, location in locations:
  425. location_path = os.path.join(grassdb, location)
  426. deletes.append(location_path)
  427. # Display question dialog
  428. dlg = wx.MessageDialog(
  429. parent=guiparent,
  430. message=_(
  431. "Do you want to continue with deleting"
  432. " one or more of the following locations?\n\n"
  433. "{deletes}\n\n"
  434. "All mapsets included in these locations will be permanently deleted!"
  435. ).format(deletes="\n".join(deletes)),
  436. caption=_("Delete selected locations"),
  437. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  438. )
  439. if dlg.ShowModal() == wx.ID_YES:
  440. try:
  441. for grassdb, location in locations:
  442. delete_location(grassdb, location)
  443. modified = True
  444. dlg.Destroy()
  445. return modified
  446. except OSError as error:
  447. wx.MessageBox(
  448. parent=guiparent,
  449. caption=_("Error when deleting locations"),
  450. message=_(
  451. "The following error occured when deleting location <{path}>:"
  452. "\n\n{error}\n\n"
  453. "Deleting of locations was interrupted."
  454. ).format(
  455. path=os.path.join(grassdb, location),
  456. error=error,
  457. ),
  458. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  459. )
  460. dlg.Destroy()
  461. return modified
  462. def delete_grassdb_interactively(guiparent, grassdb):
  463. """
  464. Delete grass database if could be deleted.
  465. If current grass database found, desired operation cannot be performed.
  466. Exceptions during deleting are handled in this function.
  467. Returns True if grass database is deleted from the disk. Returns None if
  468. cannot be deleted (see above the possible reasons).
  469. """
  470. deleted = False
  471. # Check selected grassdb
  472. messages = get_reasons_grassdb_not_removable(grassdb)
  473. if messages:
  474. dlg = wx.MessageDialog(
  475. parent=guiparent,
  476. message=_(
  477. "Cannot delete GRASS database from disk for the following reason:\n\n"
  478. "{reasons}\n\n"
  479. "GRASS database will not be deleted."
  480. ).format(reasons="\n".join(messages)),
  481. caption=_("Unable to delete selected GRASS database"),
  482. style=wx.OK | wx.ICON_WARNING
  483. )
  484. dlg.ShowModal()
  485. else:
  486. dlg = wx.MessageDialog(
  487. parent=guiparent,
  488. message=_(
  489. "Do you want to delete"
  490. " the following GRASS database from disk?\n\n"
  491. "{grassdb}\n\n"
  492. "The directory will be permanently deleted!"
  493. ).format(grassdb=grassdb),
  494. caption=_("Delete selected GRASS database"),
  495. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  496. )
  497. if dlg.ShowModal() == wx.ID_YES:
  498. try:
  499. delete_grassdb(grassdb)
  500. deleted = True
  501. dlg.Destroy()
  502. return deleted
  503. except OSError as error:
  504. wx.MessageBox(
  505. parent=guiparent,
  506. caption=_("Error when deleting GRASS database"),
  507. message=_(
  508. "The following error occured when deleting database <{path}>:"
  509. "\n\n{error}\n\n"
  510. "Deleting of GRASS database was interrupted."
  511. ).format(
  512. path=grassdb,
  513. error=error,
  514. ),
  515. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  516. )
  517. dlg.Destroy()
  518. return deleted
  519. def can_switch_mapset_interactive(guiparent, grassdb, location, mapset):
  520. """
  521. Checks if mapset is locked and offers to remove the lock file.
  522. Returns True if user wants to switch to the selected mapset in spite of
  523. removing lock. Returns False if a user wants to stay in the current
  524. mapset or if an error was encountered.
  525. """
  526. can_switch = True
  527. mapset_path = os.path.join(grassdb, location, mapset)
  528. if is_mapset_locked(mapset_path):
  529. info = get_mapset_lock_info(mapset_path)
  530. user = info['owner'] if info['owner'] else _('unknown')
  531. lockpath = info['lockpath']
  532. timestamp = info['timestamp']
  533. dlg = wx.MessageDialog(
  534. parent=guiparent,
  535. message=_("User {user} is already running GRASS in selected mapset "
  536. "<{mapset}>\n (file {lockpath} created {timestamp} "
  537. "found).\n\n"
  538. "Concurrent use not allowed.\n\n"
  539. "Do you want to stay in the current mapset or remove "
  540. ".gislock and switch to selected mapset?"
  541. ).format(user=user,
  542. mapset=mapset,
  543. lockpath=lockpath,
  544. timestamp=timestamp),
  545. caption=_("Mapset is in use"),
  546. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  547. )
  548. dlg.SetYesNoLabels("S&witch to selected mapset",
  549. "S&tay in current mapset")
  550. if dlg.ShowModal() == wx.ID_YES:
  551. # Remove lockfile
  552. try:
  553. os.remove(lockpath)
  554. except IOError as e:
  555. wx.MessageBox(
  556. parent=guiparent,
  557. caption=_("Error when removing lock file"),
  558. message=_("Unable to remove {lockpath}.\n\n Details: {error}."
  559. ).format(lockpath=lockpath,
  560. error=e),
  561. style=wx.OK | wx.ICON_ERROR | wx.CENTRE
  562. )
  563. can_switch = False
  564. else:
  565. can_switch = False
  566. dlg.Destroy()
  567. return can_switch
  568. def import_file(guiparent, filePath):
  569. """Tries to import file as vector or raster.
  570. If successfull sets default region from imported map.
  571. """
  572. RunCommand('db.connect', flags='c')
  573. mapName = os.path.splitext(os.path.basename(filePath))[0]
  574. vectors = RunCommand('v.in.ogr', input=filePath, flags='l',
  575. read=True)
  576. wx.BeginBusyCursor()
  577. wx.GetApp().Yield()
  578. if vectors:
  579. # vector detected
  580. returncode, error = RunCommand(
  581. 'v.in.ogr', input=filePath, output=mapName, flags='e',
  582. getErrorMsg=True)
  583. else:
  584. returncode, error = RunCommand(
  585. 'r.in.gdal', input=filePath, output=mapName, flags='e',
  586. getErrorMsg=True)
  587. wx.EndBusyCursor()
  588. if returncode != 0:
  589. GError(
  590. parent=guiparent,
  591. message=_(
  592. "Import of <%(name)s> failed.\n"
  593. "Reason: %(msg)s") % ({
  594. 'name': filePath,
  595. 'msg': error}))
  596. else:
  597. GMessage(
  598. message=_(
  599. "Data file <%(name)s> imported successfully. "
  600. "The location's default region was set from "
  601. "this imported map.") % {
  602. 'name': filePath},
  603. parent=guiparent)
  604. def switch_mapset_interactively(guiparent, giface, dbase, location, mapset):
  605. """Switch current mapset. Emits giface.currentMapsetChanged signal."""
  606. if dbase:
  607. if RunCommand('g.mapset', parent=guiparent,
  608. location=location,
  609. mapset=mapset,
  610. dbase=dbase) == 0:
  611. GMessage(parent=guiparent,
  612. message=_("Current GRASS database is <%(dbase)s>.\n"
  613. "Current location is <%(loc)s>.\n"
  614. "Current mapset is <%(mapset)s>."
  615. ) %
  616. {'dbase': dbase, 'loc': location, 'mapset': mapset})
  617. giface.currentMapsetChanged.emit(dbase=dbase,
  618. location=location,
  619. mapset=mapset)
  620. elif location:
  621. if RunCommand('g.mapset', parent=guiparent,
  622. location=location,
  623. mapset=mapset) == 0:
  624. GMessage(parent=guiparent,
  625. message=_("Current location is <%(loc)s>.\n"
  626. "Current mapset is <%(mapset)s>.") %
  627. {'loc': location, 'mapset': mapset})
  628. giface.currentMapsetChanged.emit(dbase=None,
  629. location=location,
  630. mapset=mapset)
  631. else:
  632. if RunCommand('g.mapset',
  633. parent=guiparent,
  634. mapset=mapset) == 0:
  635. GMessage(parent=guiparent,
  636. message=_("Current mapset is <%s>.") % mapset)
  637. giface.currentMapsetChanged.emit(dbase=None, location=None, mapset=mapset)