globalvar.py 6.0 KB

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