g.gui.rdigit.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: g.gui.rdigit
  5. # AUTHOR(S): Anna Petrasova <kratochanna gmail.com>,
  6. # Tomas Zigo <tomas.zigo slovanet.sk> (standalone module)
  7. # PURPOSE: wxGUI Raster Digitizer
  8. # COPYRIGHT: (C) 2014-2020 by Anna Petrasova, and the GRASS Development Team
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License as
  12. # published by the Free Software Foundation; either version 2 of the
  13. # License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful, but
  16. # WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. # General Public License for more details.
  19. #
  20. ############################################################################
  21. #%module
  22. #% description: Interactive editing and digitizing of raster maps.
  23. #% keyword: general
  24. #% keyword: GUI
  25. #% keyword: raster
  26. #% keyword: editing
  27. #% keyword: digitizer
  28. #%end
  29. #%option G_OPT_R_OUTPUT
  30. #% key: create
  31. #% label: Name of new raster map to create
  32. #% required: no
  33. #% guisection: Create
  34. #%end
  35. #%option G_OPT_R_BASE
  36. #% required: no
  37. #% guisection: Create
  38. #%end
  39. #%option G_OPT_R_TYPE
  40. #% answer: CELL
  41. #% required: no
  42. #% guisection: Create
  43. #%end
  44. #%option G_OPT_R_INPUT
  45. #% key: edit
  46. #% required: no
  47. #% label: Name of existing raster map to edit
  48. #% guisection: Edit
  49. #%end
  50. #%rules
  51. #% exclusive: create, edit
  52. #% required: create, edit
  53. #% requires: base, create
  54. #%end
  55. import os
  56. import grass.script as gs
  57. def main():
  58. gs.set_raise_on_error(False)
  59. options, flags = gs.parser()
  60. # import wx only after running parser
  61. # to avoid issues with complex imports when only interface is needed
  62. import wx
  63. from grass.script.setup import set_gui_path
  64. set_gui_path()
  65. from core.render import Map
  66. from mapdisp.frame import MapFrame
  67. from mapdisp.main import DMonGrassInterface
  68. from core.settings import UserSettings
  69. from grass.exceptions import CalledModuleError
  70. # define classes which needs imports as local
  71. # for longer definitions, a separate file would be a better option
  72. class RDigitMapFrame(MapFrame):
  73. def __init__(
  74. self, new_map=None, base_map=None, edit_map=None,
  75. map_type=None,
  76. ):
  77. MapFrame.__init__(
  78. self, parent=None, Map=Map(),
  79. giface=DMonGrassInterface(None),
  80. title=_("Raster Digitizer - GRASS GIS"),
  81. size=(850, 600),
  82. )
  83. # this giface issue not solved yet, we must set mapframe afterwards
  84. self._giface._mapframe = self
  85. self._giface.mapCreated.connect(self.OnMapCreated)
  86. self._mapObj = self.GetMap()
  87. # load raster map
  88. self._addLayer(name=new_map if new_map else edit_map)
  89. # switch toolbar
  90. self.AddToolbar('rdigit', fixed=True)
  91. rdigit = self.toolbars['rdigit']
  92. if new_map:
  93. rdigit._mapSelectionCombo.Unbind(wx.EVT_COMBOBOX)
  94. self.rdigit.SelectNewMap(
  95. standalone=True, mapName=new_map, bgMap=base_map,
  96. mapType=map_type,
  97. )
  98. rdigit._mapSelectionCombo.Bind(
  99. wx.EVT_COMBOBOX, rdigit.OnMapSelection,
  100. )
  101. else:
  102. rdigit._mapSelectionCombo.SetSelection(n=1)
  103. rdigit.OnMapSelection()
  104. def _addLayer(self, name, ltype='raster'):
  105. """Add layer into map
  106. :param str name: map name
  107. :param str ltype: layer type
  108. """
  109. mapLayer = self._mapObj.AddLayer(
  110. ltype=ltype, name=name,
  111. command=['d.rast', "map={}".format(name)],
  112. active=True, hidden=False, opacity=1.0, render=True,
  113. )
  114. def OnMapCreated(self, name, ltype):
  115. """Add new created raster layer into map
  116. :param str name: map name
  117. :param str ltype: layer type
  118. """
  119. self._mapObj.Clean()
  120. self._addLayer(name=name, ltype=ltype)
  121. self.GetMapWindow().UpdateMap()
  122. kwargs = {
  123. 'new_map': options['create'],
  124. 'base_map': options['base'],
  125. 'edit_map': options['edit'],
  126. 'map_type': options['type'],
  127. }
  128. mapset = gs.gisenv()['MAPSET']
  129. if kwargs['edit_map']:
  130. edit_map = gs.find_file(
  131. name=kwargs['edit_map'], element='raster',
  132. mapset=mapset,
  133. )['fullname']
  134. if not edit_map:
  135. gs.fatal(
  136. _(
  137. "Raster map <{}> not found in current mapset.".format(
  138. options['edit'],
  139. ),
  140. ),
  141. )
  142. else:
  143. kwargs['edit_map'] = edit_map
  144. else:
  145. if kwargs['base_map']:
  146. base_map = gs.find_file(
  147. name=kwargs['base_map'], element='raster',
  148. mapset=mapset,
  149. )['fullname']
  150. if not base_map:
  151. gs.fatal(
  152. _(
  153. "Base raster map <{}> not found in "
  154. "current mapset."
  155. .format(
  156. options['base'],
  157. ),
  158. ),
  159. )
  160. kwargs['base_map'] = base_map
  161. # allow immediate rendering
  162. driver = UserSettings.Get(
  163. group='display', key='driver', subkey='type',
  164. )
  165. if driver == 'png':
  166. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  167. else:
  168. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  169. app = wx.App()
  170. frame = RDigitMapFrame(**kwargs)
  171. frame.Show()
  172. app.MainLoop()
  173. if __name__ == "__main__":
  174. main()