g.gui.rdigit.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. # define classes which needs imports as local
  70. # for longer definitions, a separate file would be a better option
  71. class RDigitMapFrame(MapFrame):
  72. def __init__(
  73. self, new_map=None, base_map=None, edit_map=None,
  74. map_type=None,
  75. ):
  76. MapFrame.__init__(
  77. self, parent=None, Map=Map(),
  78. giface=DMonGrassInterface(None),
  79. title=_("Raster Digitizer - GRASS GIS"),
  80. size=(850, 600),
  81. )
  82. # this giface issue not solved yet, we must set mapframe afterwards
  83. self._giface._mapframe = self
  84. self._giface.mapCreated.connect(self.OnMapCreated)
  85. self._mapObj = self.GetMap()
  86. # load raster map
  87. self._addLayer(name=new_map if new_map else edit_map)
  88. # switch toolbar
  89. self.AddToolbar('rdigit', fixed=True)
  90. rdigit = self.toolbars['rdigit']
  91. if new_map:
  92. rdigit._mapSelectionCombo.Unbind(wx.EVT_COMBOBOX)
  93. self.rdigit.SelectNewMap(
  94. standalone=True, mapName=new_map, bgMap=base_map,
  95. mapType=map_type,
  96. )
  97. rdigit._mapSelectionCombo.Bind(
  98. wx.EVT_COMBOBOX, rdigit.OnMapSelection,
  99. )
  100. else:
  101. rdigit._mapSelectionCombo.SetSelection(n=1)
  102. rdigit.OnMapSelection()
  103. def _addLayer(self, name, ltype='raster'):
  104. """Add layer into map
  105. :param str name: map name
  106. :param str ltype: layer type
  107. """
  108. mapLayer = self._mapObj.AddLayer(
  109. ltype=ltype, name=name,
  110. command=['d.rast', "map={}".format(name)],
  111. active=True, hidden=False, opacity=1.0, render=True,
  112. )
  113. def OnMapCreated(self, name, ltype):
  114. """Add new created raster layer into map
  115. :param str name: map name
  116. :param str ltype: layer type
  117. """
  118. self._mapObj.Clean()
  119. self._addLayer(name=name, ltype=ltype)
  120. self.GetMapWindow().UpdateMap()
  121. kwargs = {
  122. 'new_map': options['create'],
  123. 'base_map': options['base'],
  124. 'edit_map': options['edit'],
  125. 'map_type': options['type'],
  126. }
  127. mapset = gs.gisenv()['MAPSET']
  128. if kwargs['edit_map']:
  129. edit_map = gs.find_file(
  130. name=kwargs['edit_map'], element='raster',
  131. mapset=mapset,
  132. )['fullname']
  133. if not edit_map:
  134. gs.fatal(
  135. _(
  136. "Raster map <{}> not found in current mapset.".format(
  137. options['edit'],
  138. ),
  139. ),
  140. )
  141. else:
  142. kwargs['edit_map'] = edit_map
  143. else:
  144. if kwargs['base_map']:
  145. base_map = gs.find_file(
  146. name=kwargs['base_map'], element='raster',
  147. mapset=mapset,
  148. )['fullname']
  149. if not base_map:
  150. gs.fatal(
  151. _(
  152. "Base raster map <{}> not found in "
  153. "current mapset."
  154. .format(
  155. options['base'],
  156. ),
  157. ),
  158. )
  159. kwargs['base_map'] = base_map
  160. # allow immediate rendering
  161. driver = UserSettings.Get(
  162. group='display', key='driver', subkey='type',
  163. )
  164. if driver == 'png':
  165. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  166. else:
  167. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  168. app = wx.App()
  169. frame = RDigitMapFrame(**kwargs)
  170. frame.Show()
  171. app.MainLoop()
  172. if __name__ == "__main__":
  173. main()