g.gui.example.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 app 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. # i18n is taken care of in the grass library code.
  33. # So we need to import it before any of the GUI code.
  34. import grass.script.core as gcore
  35. if __name__ == "__main__":
  36. wxbase = os.path.join(os.getenv("GISBASE"), "etc", "gui", "wxpython")
  37. if wxbase not in sys.path:
  38. sys.path.append(wxbase)
  39. def main():
  40. options, flags = gcore.parser()
  41. import wx
  42. from grass.script.setup import set_gui_path
  43. set_gui_path()
  44. from core.globalvar import CheckWxVersion, MAP_WINDOW_SIZE
  45. from core.giface import StandaloneGrassInterface
  46. from core.settings import UserSettings
  47. from example.frame import ExampleMapDisplay
  48. if options["input"]:
  49. map_name = gcore.find_file(name=options["input"], element="cell")["fullname"]
  50. if not map_name:
  51. gcore.fatal(
  52. _("Raster map <{raster}> not found").format(raster=options["input"])
  53. )
  54. # define display driver (avoid 'no graphics device selected' error at start up)
  55. driver = UserSettings.Get(group="display", key="driver", subkey="type")
  56. if driver == "png":
  57. os.environ["GRASS_RENDER_IMMEDIATE"] = "png"
  58. else:
  59. os.environ["GRASS_RENDER_IMMEDIATE"] = "cairo"
  60. # launch application
  61. app = wx.App()
  62. if not CheckWxVersion([2, 9]):
  63. wx.InitAllImageHandlers()
  64. # show main frame
  65. frame = wx.Frame(
  66. parent=None, size=MAP_WINDOW_SIZE, title=_("Example Tool - GRASSGIS")
  67. )
  68. frame = ExampleMapDisplay(
  69. parent=frame,
  70. giface=StandaloneGrassInterface(),
  71. )
  72. if options["input"]:
  73. frame.giface.WriteLog(
  74. _("Loading raster map <{raster}>...").format(raster=map_name)
  75. )
  76. frame.SetLayer(map_name)
  77. frame.Show()
  78. app.MainLoop()
  79. if __name__ == "__main__":
  80. main()