grass.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import os
  2. import sys
  3. import types
  4. import subprocess
  5. import re
  6. import atexit
  7. # subprocess wrapper that uses shell on Windows
  8. class Popen(subprocess.Popen):
  9. def __init__(self, args, bufsize=0, executable=None,
  10. stdin=None, stdout=None, stderr=None,
  11. preexec_fn=None, close_fds=False, shell=None,
  12. cwd=None, env=None, universal_newlines=False,
  13. startupinfo=None, creationflags=0):
  14. if shell == None:
  15. shell = (sys.platform == "win32")
  16. subprocess.Popen.__init__(self, args, bufsize, executable,
  17. stdin, stdout, stderr,
  18. preexec_fn, close_fds, shell,
  19. cwd, env, universal_newlines,
  20. startupinfo, creationflags)
  21. PIPE = subprocess.PIPE
  22. STDOUT = subprocess.STDOUT
  23. def call(*args, **kwargs):
  24. return Popen(*args, **kwargs).wait()
  25. # GRASS-oriented interface to subprocess module
  26. _popen_args = ["bufsize", "executable", "stdin", "stdout", "stderr",
  27. "preexec_fn", "close_fds", "cwd", "env",
  28. "universal_newlines", "startupinfo", "creationflags"]
  29. def _make_val(val):
  30. if isinstance(val, types.StringType):
  31. return val
  32. if isinstance(val, types.ListType):
  33. return ",".join(map(_make_val, val))
  34. if isinstance(val, types.TupleType):
  35. return _make_val(list(val))
  36. return str(val)
  37. def make_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **options):
  38. args = [prog]
  39. if overwrite:
  40. args.append("--o")
  41. if quiet:
  42. args.append("--q")
  43. if verbose:
  44. args.append("--v")
  45. if flags:
  46. args.append("-%s" % flags)
  47. for opt, val in options.iteritems():
  48. if val != None:
  49. if opt[0] == '_':
  50. opt = opt[1:]
  51. args.append("%s=%s" % (opt, _make_val(val)))
  52. return args
  53. def start_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **kwargs):
  54. options = {}
  55. popts = {}
  56. for opt, val in kwargs.iteritems():
  57. if opt in _popen_args:
  58. popts[opt] = val
  59. else:
  60. options[opt] = val
  61. args = make_command(prog, flags, overwrite, quiet, verbose, **options)
  62. return Popen(args, **popts)
  63. def run_command(*args, **kwargs):
  64. ps = start_command(*args, **kwargs)
  65. return ps.wait()
  66. def pipe_command(*args, **kwargs):
  67. kwargs['stdout'] = PIPE
  68. return start_command(*args, **kwargs)
  69. def feed_command(*args, **kwargs):
  70. kwargs['stdin'] = PIPE
  71. return start_command(*args, **kwargs)
  72. def read_command(*args, **kwargs):
  73. ps = pipe_command(*args, **kwargs)
  74. return ps.communicate()[0]
  75. def write_command(*args, **kwargs):
  76. stdin = kwargs['stdin']
  77. kwargs['stdin'] = PIPE
  78. p = start_command(*args, **kwargs)
  79. p.stdin.write(stdin)
  80. p.stdin.close()
  81. return p.wait()
  82. def exec_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, env = None, **kwargs):
  83. args = make_command(prog, flags, overwrite, quiet, verbose, **kwargs)
  84. if env == None:
  85. env = os.environ
  86. os.execvpe(prog, args, env)
  87. # interface to g.message
  88. def message(msg, flag = None):
  89. run_command("g.message", flags = flag, message = msg)
  90. def debug(msg):
  91. message(msg, flag = 'd')
  92. def verbose(msg):
  93. message(msg, flag = 'v')
  94. def info(msg):
  95. message(msg, flag = 'i')
  96. def warning(msg):
  97. message(msg, flag = 'w')
  98. def error(msg):
  99. message(msg, flag = 'e')
  100. def fatal(msg):
  101. error(msg)
  102. sys.exit(1)
  103. # interface to g.parser
  104. def _parse_env():
  105. options = {}
  106. flags = {}
  107. for var, val in os.environ.iteritems():
  108. if var.startswith("GIS_OPT_"):
  109. opt = var.replace("GIS_OPT_", "", 1).lower()
  110. options[opt] = val;
  111. if var.startswith("GIS_FLAG_"):
  112. flg = var.replace("GIS_FLAG_", "", 1).lower()
  113. flags[flg] = bool(int(val));
  114. return (options, flags)
  115. def parser():
  116. if not os.getenv("GISBASE"):
  117. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  118. sys.exit(1)
  119. if len(sys.argv) > 1 and sys.argv[1] == "@ARGS_PARSED@":
  120. return _parse_env()
  121. cmdline = [basename(sys.argv[0])]
  122. cmdline += ['"' + arg + '"' for arg in sys.argv[1:]]
  123. os.environ['CMDLINE'] = ' '.join(cmdline)
  124. argv = sys.argv[:]
  125. name = argv[0]
  126. if not os.path.isabs(name):
  127. if os.sep in name or (os.altsep and os.altsep in name):
  128. argv[0] = os.path.abspath(name)
  129. else:
  130. argv[0] = os.path.join(sys.path[0], name)
  131. os.execvp("g.parser", [name] + argv)
  132. raise OSError("error executing g.parser")
  133. # interface to g.tempfile
  134. def tempfile():
  135. return read_command("g.tempfile", pid = os.getpid()).strip()
  136. # key-value parsers
  137. def parse_key_val(s, sep = '=', dflt = None):
  138. result = {}
  139. for line in s.splitlines():
  140. kv = line.split(sep, 1)
  141. k = kv[0].strip()
  142. if len(kv) > 1:
  143. v = kv[1]
  144. else:
  145. v = dflt
  146. result[k] = v
  147. return result
  148. _kv_regex = None
  149. def parse_key_val2(s):
  150. global _kv_regex
  151. if _kv_regex == None:
  152. _kv_regex = re.compile("([^=]+)='(.*)';?")
  153. result = []
  154. for line in s.splitlines():
  155. m = _kv_regex.match(line)
  156. if m != None:
  157. result.append(m.groups())
  158. else:
  159. result.append(line.split('=', 1))
  160. return dict(result)
  161. # interface to g.gisenv
  162. def gisenv():
  163. return parse_key_val2(read_command("g.gisenv"))
  164. # interface to g.region
  165. def region():
  166. s = read_command("g.region", flags='g')
  167. return parse_key_val(s)
  168. def use_temp_region():
  169. name = "tmp.%s.%d" % (os.path.basename(sys.argv[0]), os.getpid())
  170. run_command("g.region", save = name)
  171. os.environ['WIND_OVERRIDE'] = name
  172. atexit.register(del_temp_region)
  173. def del_temp_region():
  174. try:
  175. name = os.environ.pop('WIND_OVERRIDE')
  176. run_command("g.remove", quiet = True, region = name)
  177. except:
  178. pass
  179. # interface to g.findfile
  180. def find_file(name, element = 'cell', mapset = None):
  181. s = read_command("g.findfile", element = element, file = name, mapset = mapset)
  182. return parse_key_val2(s)
  183. # interface to g.list
  184. def list_grouped(type):
  185. dashes_re = re.compile("^----+$")
  186. mapset_re = re.compile("<(.*)>:$")
  187. result = {}
  188. mapset = None
  189. for line in read_command("g.list", type = type).splitlines():
  190. if line == "":
  191. continue
  192. if dashes_re.match(line):
  193. continue
  194. m = mapset_re.search(line)
  195. if m:
  196. mapset = m.group(1)
  197. result[mapset] = []
  198. continue
  199. if mapset:
  200. result[mapset].extend(line.split())
  201. return result
  202. def _concat(xs):
  203. result = []
  204. for x in xs:
  205. result.extend(x)
  206. return result
  207. def list_pairs(type):
  208. return _concat([[(map, mapset) for map in maps]
  209. for mapset, maps in list_grouped(type).iteritems()])
  210. def list_strings(type):
  211. return ["%s@%s" % pair for pair in list_pairs(type)]
  212. # color parsing
  213. named_colors = {
  214. "white": (1.00, 1.00, 1.00),
  215. "black": (0.00, 0.00, 0.00),
  216. "red": (1.00, 0.00, 0.00),
  217. "green": (0.00, 1.00, 0.00),
  218. "blue": (0.00, 0.00, 1.00),
  219. "yellow": (1.00, 1.00, 0.00),
  220. "magenta": (1.00, 0.00, 1.00),
  221. "cyan": (0.00, 1.00, 1.00),
  222. "aqua": (0.00, 0.75, 0.75),
  223. "grey": (0.75, 0.75, 0.75),
  224. "gray": (0.75, 0.75, 0.75),
  225. "orange": (1.00, 0.50, 0.00),
  226. "brown": (0.75, 0.50, 0.25),
  227. "purple": (0.50, 0.00, 1.00),
  228. "violet": (0.50, 0.00, 1.00),
  229. "indigo": (0.00, 0.50, 1.00)}
  230. def parse_color(val, dflt = None):
  231. if val in named_colors:
  232. return named_colors[val]
  233. vals = val.split(':')
  234. if len(vals) == 3:
  235. return tuple(float(v) / 255 for v in vals)
  236. return dflt
  237. # check GRASS_OVERWRITE
  238. def overwrite():
  239. owstr = 'GRASS_OVERWRITE'
  240. return owstr in os.environ and os.environ[owstr] != '0'
  241. # check GRASS_VERBOSE
  242. def verbosity():
  243. vbstr = 'GRASS_VERBOSE'
  244. if vbstr:
  245. return int(vbstr)
  246. else:
  247. return 0
  248. ## various utilities, not specific to GRASS
  249. # basename inc. extension stripping
  250. def basename(path, ext = None):
  251. name = os.path.basename(path)
  252. if not ext:
  253. return name
  254. fs = name.rsplit('.', 1)
  255. if len(fs) > 1 and fs[1].lower() == ext:
  256. name = fs[0]
  257. return name
  258. # find a program (replacement for "which")
  259. def find_program(pgm, args = []):
  260. nuldev = file(os.devnull, 'w+')
  261. try:
  262. call([pgm] + args, stdin = nuldev, stdout = nuldev, stderr = nuldev)
  263. found = True
  264. except:
  265. found = False
  266. nuldev.close()
  267. return found
  268. # try to remove a file, without complaints
  269. def try_remove(path):
  270. try:
  271. os.remove(path)
  272. except:
  273. pass
  274. # try to remove a directory, without complaints
  275. def try_rmdir(path):
  276. try:
  277. os.rmdir(path)
  278. except:
  279. pass
  280. # run "v.db.connect -g ..." and parse output
  281. def vector_db(map, layer = None, **args):
  282. s = read_command('v.db.connect', flags = 'g', map = map, layer = layer, **args)
  283. result = []
  284. for l in s.splitlines():
  285. f = l.split(' ')
  286. if len(f) != 5:
  287. continue
  288. if layer and int(layer) == int(f[0]):
  289. return f
  290. result.append(f)
  291. if not layer:
  292. return result
  293. # run "db.describe -c ..." and parse output
  294. def db_describe(table, **args):
  295. s = read_command('db.describe', flags = 'c', table = table, **args)
  296. if not s:
  297. return None
  298. cols = []
  299. result = {}
  300. for l in s.splitlines():
  301. f = l.split(':')
  302. key = f[0]
  303. f[1] = f[1].lstrip(' ')
  304. if key.startswith('Column '):
  305. n = int(key.split(' ')[1])
  306. cols.insert(n, f[1:])
  307. elif key in ['ncols', 'nrows']:
  308. result[key] = int(f[1])
  309. else:
  310. result[key] = f[1:]
  311. result['cols'] = cols
  312. return result
  313. # run "db.connect -p" and parse output
  314. def db_connection():
  315. s = read_command('db.connect', flags = 'p')
  316. return parse_key_val(s, sep = ':')
  317. # run "v.info -c ..." and parse output
  318. def vector_columns(map, layer = None, **args):
  319. s = read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True, **args)
  320. result = []
  321. for line in s.splitlines():
  322. f = line.split('|')
  323. if len(f) == 2:
  324. result.append(f)
  325. return result
  326. # add vector history
  327. def vector_history(map):
  328. run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
  329. # add raster history
  330. def raster_history(map):
  331. run_command('r.support', map = map, history = os.environ['CMDLINE'])