globalvar.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. """!
  2. @package global.py
  3. @brief Global variables
  4. This module provide the space for global variables
  5. used in the code.
  6. (C) 2007-2010 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Martin Landa <landa.martin gmail.com>
  10. """
  11. import os
  12. import sys
  13. import locale
  14. ### i18N
  15. import gettext
  16. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  17. grassPath = os.path.join(globalvar.ETCDIR, "python")
  18. sys.path.append(grassPath)
  19. import grass.script as grass
  20. # wxversion.select() called once at the beginning
  21. check = True
  22. def CheckForWx():
  23. """!Try to import wx module and check its version"""
  24. global check
  25. if not check:
  26. return
  27. minVersion = [2, 8, 1, 1]
  28. try:
  29. try:
  30. import wxversion
  31. except ImportError, e:
  32. raise ImportError(e)
  33. wxversion.select(str(minVersion[0]) + '.' + str(minVersion[1]))
  34. import wx
  35. version = wx.version().split(' ')[0]
  36. if map(int, version.split('.')) < minVersion:
  37. raise ValueError('Your wxPython version is %s.%s.%s.%s' % tuple(version.split('.')))
  38. except ImportError, e:
  39. print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
  40. sys.exit(1)
  41. except (ValueError, wxversion.VersionError), e:
  42. print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(minVersion) + \
  43. '%s.' % (str(e))
  44. sys.exit(1)
  45. except locale.Error, e:
  46. print >> sys.stderr, "Unable to set locale:", e
  47. os.environ['LC_ALL'] = ''
  48. check = False
  49. if not os.getenv("GRASS_WXBUNDLED"):
  50. CheckForWx()
  51. import wx
  52. import wx.lib.flatnotebook as FN
  53. try:
  54. import subprocess
  55. except:
  56. compatPath = os.path.join(globalvar.ETCWXDIR, "compat")
  57. sys.path.append(compatPath)
  58. import subprocess
  59. """
  60. Query layer (generated for example by selecting item in the Attribute Table Manager)
  61. Deleted automatically on re-render action
  62. """
  63. # temporal query layer (removed on re-render action)
  64. QUERYLAYER = 'qlayer'
  65. # path to python scripts
  66. ETCDIR = os.path.join(os.getenv("GISBASE"), "etc")
  67. ETCICONDIR = os.path.join(os.getenv("GISBASE"), "etc", "gui", "icons")
  68. ETCWXDIR = os.path.join(ETCDIR, "gui", "wxpython")
  69. """!Style definition for FlatNotebook pages"""
  70. FNPageStyle = FN.FNB_VC8 | \
  71. FN.FNB_BACKGROUND_GRADIENT | \
  72. FN.FNB_NODRAG | \
  73. FN.FNB_TABS_BORDER_SIMPLE
  74. FNPageColor = wx.Colour(125,200,175)
  75. """!Dialog widget dimension"""
  76. DIALOG_SPIN_SIZE = (150, -1)
  77. DIALOG_COMBOBOX_SIZE = (300, -1)
  78. DIALOG_GSELECT_SIZE = (400, -1)
  79. DIALOG_TEXTCTRL_SIZE = (400, -1)
  80. DIALOG_LAYER_SIZE = (100, -1)
  81. DIALOG_COLOR_SIZE = (30, 30)
  82. MAP_WINDOW_SIZE = (770, 570)
  83. HIST_WINDOW_SIZE = (500, 350)
  84. MAP_DISPLAY_STATUSBAR_MODE = [_("Coordinates"),
  85. _("Extent"),
  86. _("Comp. region"),
  87. _("Show comp. extent"),
  88. _("Display mode"),
  89. _("Display geometry"),
  90. _("Map scale"),
  91. _("Go to"),
  92. _("Projection"),]
  93. """!File name extension binaries/scripts"""
  94. if subprocess.mswindows:
  95. EXT_BIN = '.exe'
  96. EXT_SCT = '.py'
  97. else:
  98. EXT_BIN = ''
  99. EXT_SCT = ''
  100. def GetGRASSCmds(bin=True, scripts=True, gui_scripts=True):
  101. """!Create list of all available GRASS commands to use when
  102. parsing string from the command line
  103. """
  104. gisbase = os.environ['GISBASE']
  105. cmd = list()
  106. if bin is True:
  107. for file in os.listdir(os.path.join(gisbase, 'bin')):
  108. if not EXT_BIN or file[-4:] == EXT_BIN:
  109. cmd.append(file)
  110. # add special call for setting vector colors
  111. cmd.append('vcolors')
  112. if scripts is True:
  113. cmd = cmd + os.listdir(os.path.join(gisbase, 'scripts'))
  114. if gui_scripts is True:
  115. os.environ["PATH"] = os.getenv("PATH") + os.pathsep + os.path.join(gisbase, 'etc', 'gui', 'scripts')
  116. cmd = cmd + os.listdir(os.path.join(gisbase, 'etc', 'gui', 'scripts'))
  117. if subprocess.mswindows:
  118. for idx in range(len(cmd)):
  119. cmd[idx] = cmd[idx].replace(EXT_BIN, '')
  120. cmd[idx] = cmd[idx].replace(EXT_SCT, '')
  121. return cmd
  122. """@brief Collected GRASS-relared binaries/scripts"""
  123. grassCmd = {}
  124. grassCmd['all'] = GetGRASSCmds()
  125. grassCmd['script'] = GetGRASSCmds(bin=False)
  126. """@Toolbar icon size"""
  127. toolbarSize = (24, 24)
  128. """@Is g.mlist available?"""
  129. if 'g.mlist' in grassCmd['all']:
  130. have_mlist = True
  131. else:
  132. have_mlist = False
  133. def _getGDALFormats():
  134. """!Get dictionary of avaialble GDAL drivers"""
  135. ret = grass.read_command('r.in.gdal',
  136. quiet = True,
  137. flags = 'f')
  138. return _parseFormats(ret)
  139. def _getOGRFormats():
  140. """!Get dictionary of avaialble OGR drivers"""
  141. ret = grass.read_command('v.in.ogr',
  142. quiet = True,
  143. flags = 'f')
  144. return _parseFormats(ret)
  145. def _parseFormats(output):
  146. """!Parse r.in.gdal/v.in.ogr -f output"""
  147. formats = { 'file' : list(),
  148. 'database' : list(),
  149. 'protocol' : list()
  150. }
  151. if not output:
  152. return formats
  153. for line in output.splitlines():
  154. format = line.strip().rsplit(':', -1)[1].strip()
  155. if format in ('Memory', 'Virtual Raster', 'In Memory Raster'):
  156. continue
  157. if format in ('PostgreSQL', 'SQLite',
  158. 'ODBC', 'ESRI Personal GeoDatabase',
  159. 'Rasterlite',
  160. 'PostGIS WKT Raster driver'):
  161. formats['database'].append(format)
  162. elif format in ('GeoJSON',
  163. 'OGC Web Coverage Service',
  164. 'OGC Web Map Service',
  165. 'HTTP Fetching Wrapper'):
  166. formats['protocol'].append(format)
  167. else:
  168. formats['file'].append(format)
  169. return formats
  170. formats = {
  171. 'gdal' : _getGDALFormats(),
  172. 'ogr' : _getOGRFormats()
  173. }