g.gui.example.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: g.gui.iclass
  5. # AUTHOR(S): Anna Petrasova
  6. # PURPOSE: Example GUI application
  7. # COPYRIGHT: (C) 2012-2014 by the GRASS Development Team
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. ############################################################################
  20. # %module
  21. # % description: Example GUI application which displays raster map and further information
  22. # % keyword: example
  23. # % keyword: GUI
  24. # % keyword: raster
  25. # %end
  26. # %option G_OPT_R_INPUT
  27. # % description: Name of raster map to load
  28. # % required: no
  29. # %end
  30. import os
  31. import sys
  32. import wx
  33. # i18n is taken care of in the grass library code.
  34. # So we need to import it before any of the GUI code.
  35. import grass.script.core as gcore
  36. if __name__ == "__main__":
  37. wxbase = os.path.join(os.getenv("GISBASE"), "etc", "gui", "wxpython")
  38. if wxbase not in sys.path:
  39. sys.path.append(wxbase)
  40. from core.globalvar import CheckWxVersion
  41. from core.giface import StandaloneGrassInterface
  42. from core.utils import GuiModuleMain
  43. from core.settings import UserSettings
  44. from example.frame import ExampleMapFrame
  45. def main():
  46. options, flags = gcore.parser()
  47. if options["input"]:
  48. map_name = gcore.find_file(name=options["input"], element="cell")["fullname"]
  49. if not map_name:
  50. gcore.fatal(
  51. _("Raster map <{raster}> not found").format(raster=options["input"])
  52. )
  53. # define display driver (avoid 'no graphics device selected' error at start up)
  54. driver = UserSettings.Get(group="display", key="driver", subkey="type")
  55. if driver == "png":
  56. os.environ["GRASS_RENDER_IMMEDIATE"] = "png"
  57. else:
  58. os.environ["GRASS_RENDER_IMMEDIATE"] = "cairo"
  59. # launch application
  60. app = wx.App()
  61. if not CheckWxVersion([2, 9]):
  62. wx.InitAllImageHandlers()
  63. # show main frame
  64. giface = StandaloneGrassInterface()
  65. frame = ExampleMapFrame(parent=None, giface=giface)
  66. if options["input"]:
  67. giface.WriteLog(_("Loading raster map <{raster}>...").format(raster=map_name))
  68. frame.SetLayer(map_name)
  69. frame.Show()
  70. app.MainLoop()
  71. if __name__ == "__main__":
  72. GuiModuleMain(main)