g.gui.rdigit.py 6.0 KB

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