utils.py 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. """
  2. @package core.utils
  3. @brief Misc utilities for wxGUI
  4. (C) 2007-2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Martin Landa <landa.martin gmail.com>
  8. @author Jachym Cepicky
  9. """
  10. import os
  11. import sys
  12. import platform
  13. import string
  14. import glob
  15. import shlex
  16. import re
  17. import inspect
  18. from grass.script import core as grass
  19. from grass.script import task as gtask
  20. from core import globalvar
  21. from core.gcmd import RunCommand
  22. from core.debug import Debug
  23. try:
  24. # intended to be used also outside this module
  25. import gettext
  26. _ = gettext.translation('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale')).ugettext
  27. except IOError:
  28. # using no translation silently
  29. def null_gettext(string):
  30. return string
  31. _ = null_gettext
  32. def normalize_whitespace(text):
  33. """Remove redundant whitespace from a string"""
  34. return string.join(string.split(text), ' ')
  35. def split(s):
  36. """Platform spefic shlex.split"""
  37. try:
  38. if sys.platform == "win32":
  39. return shlex.split(s.replace('\\', r'\\'))
  40. else:
  41. return shlex.split(s)
  42. except ValueError as e:
  43. sys.stderr.write(_("Syntax error: %s") % e)
  44. return []
  45. def GetTempfile(pref=None):
  46. """Creates GRASS temporary file using defined prefix.
  47. .. todo::
  48. Fix path on MS Windows/MSYS
  49. :param pref: prefer the given path
  50. :return: Path to file name (string) or None
  51. """
  52. ret = RunCommand('g.tempfile',
  53. read = True,
  54. pid = os.getpid())
  55. tempfile = ret.splitlines()[0].strip()
  56. # FIXME
  57. # ugly hack for MSYS (MS Windows)
  58. if platform.system() == 'Windows':
  59. tempfile = tempfile.replace("/", "\\")
  60. try:
  61. path, file = os.path.split(tempfile)
  62. if pref:
  63. return os.path.join(pref, file)
  64. else:
  65. return tempfile
  66. except:
  67. return None
  68. def GetLayerNameFromCmd(dcmd, fullyQualified = False, param = None,
  69. layerType = None):
  70. """Get map name from GRASS command
  71. Parameter dcmd can be modified when first parameter is not
  72. defined.
  73. :param dcmd: GRASS command (given as list)
  74. :param fullyQualified: change map name to be fully qualified
  75. :param param: params directory
  76. :param str layerType: check also layer type ('raster', 'vector',
  77. '3d-raster', ...)
  78. :return: tuple (name, found)
  79. """
  80. mapname = ''
  81. found = True
  82. if len(dcmd) < 1:
  83. return mapname, False
  84. if 'd.grid' == dcmd[0]:
  85. mapname = 'grid'
  86. elif 'd.geodesic' in dcmd[0]:
  87. mapname = 'geodesic'
  88. elif 'd.rhumbline' in dcmd[0]:
  89. mapname = 'rhumb'
  90. elif 'd.graph' in dcmd[0]:
  91. mapname = 'graph'
  92. else:
  93. params = list()
  94. for idx in range(len(dcmd)):
  95. try:
  96. p, v = dcmd[idx].split('=', 1)
  97. except ValueError:
  98. continue
  99. if p == param:
  100. params = [(idx, p, v)]
  101. break
  102. if p in ('map', 'input', 'layer',
  103. 'red', 'blue', 'green',
  104. 'h_map', 's_map', 'i_map',
  105. 'reliefmap', 'labels'):
  106. params.append((idx, p, v))
  107. if len(params) < 1:
  108. if len(dcmd) > 1:
  109. i = 1
  110. while i < len(dcmd):
  111. if '=' not in dcmd[i] and not dcmd[i].startswith('-'):
  112. task = gtask.parse_interface(dcmd[0])
  113. # this expects the first parameter to be the right one
  114. p = task.get_options()['params'][0].get('name', '')
  115. params.append((i, p, dcmd[i]))
  116. break
  117. i += 1
  118. else:
  119. return mapname, False
  120. if len(params) < 1:
  121. return mapname, False
  122. # need to add mapset for all maps
  123. mapsets = {}
  124. for i, p, v in params:
  125. if p == 'layer':
  126. continue
  127. mapname = v
  128. mapset = ''
  129. if fullyQualified and '@' not in mapname:
  130. if layerType in ('raster', 'vector', '3d-raster', 'rgb', 'his'):
  131. try:
  132. if layerType in ('raster', 'rgb', 'his'):
  133. findType = 'cell'
  134. else:
  135. findType = layerType
  136. mapset = grass.find_file(mapname, element = findType)['mapset']
  137. except AttributeError: # not found
  138. return '', False
  139. if not mapset:
  140. found = False
  141. else:
  142. mapset = '' # grass.gisenv()['MAPSET']
  143. mapsets[i] = mapset
  144. # update dcmd
  145. for i, p, v in params:
  146. if p == 'layer':
  147. continue
  148. dcmd[i] = p + '=' + v
  149. if i in mapsets and mapsets[i]:
  150. dcmd[i] += '@' + mapsets[i]
  151. maps = list()
  152. ogr = False
  153. for i, p, v in params:
  154. if v.lower().rfind('@ogr') > -1:
  155. ogr = True
  156. if p == 'layer' and not ogr:
  157. continue
  158. maps.append(dcmd[i].split('=', 1)[1])
  159. mapname = '\n'.join(maps)
  160. return mapname, found
  161. def GetValidLayerName(name):
  162. """Make layer name SQL compliant, based on G_str_to_sql()
  163. .. todo::
  164. Better use directly Ctypes to reuse venerable libgis C fns...
  165. """
  166. retName = str(name).strip()
  167. # check if name is fully qualified
  168. if '@' in retName:
  169. retName, mapset = retName.split('@')
  170. else:
  171. mapset = None
  172. cIdx = 0
  173. retNameList = list(retName)
  174. for c in retNameList:
  175. if not (c >= 'A' and c <= 'Z') and \
  176. not (c >= 'a' and c <= 'z') and \
  177. not (c >= '0' and c <= '9'):
  178. retNameList[cIdx] = '_'
  179. cIdx += 1
  180. retName = ''.join(retNameList)
  181. if not (retName[0] >= 'A' and retName[0] <= 'Z') and \
  182. not (retName[0] >= 'a' and retName[0] <= 'z'):
  183. retName = 'x' + retName[1:]
  184. if mapset:
  185. retName = retName + '@' + mapset
  186. return retName
  187. def ListOfCatsToRange(cats):
  188. """Convert list of category number to range(s)
  189. Used for example for d.vect cats=[range]
  190. :param cats: category list
  191. :return: category range string
  192. :return: '' on error
  193. """
  194. catstr = ''
  195. try:
  196. cats = map(int, cats)
  197. except:
  198. return catstr
  199. i = 0
  200. while i < len(cats):
  201. next = 0
  202. j = i + 1
  203. while j < len(cats):
  204. if cats[i + next] == cats[j] - 1:
  205. next += 1
  206. else:
  207. break
  208. j += 1
  209. if next > 1:
  210. catstr += '%d-%d,' % (cats[i], cats[i + next])
  211. i += next + 1
  212. else:
  213. catstr += '%d,' % (cats[i])
  214. i += 1
  215. return catstr.strip(',')
  216. def ListOfMapsets(get = 'ordered'):
  217. """Get list of available/accessible mapsets
  218. :param str get: method ('all', 'accessible', 'ordered')
  219. :return: list of mapsets
  220. :return: None on error
  221. """
  222. mapsets = []
  223. if get == 'all' or get == 'ordered':
  224. ret = RunCommand('g.mapsets',
  225. read = True,
  226. quiet = True,
  227. flags = 'l',
  228. sep = 'newline')
  229. if ret:
  230. mapsets = ret.splitlines()
  231. ListSortLower(mapsets)
  232. else:
  233. return None
  234. if get == 'accessible' or get == 'ordered':
  235. ret = RunCommand('g.mapsets',
  236. read = True,
  237. quiet = True,
  238. flags = 'p',
  239. sep = 'newline')
  240. if ret:
  241. if get == 'accessible':
  242. mapsets = ret.splitlines()
  243. else:
  244. mapsets_accessible = ret.splitlines()
  245. for mapset in mapsets_accessible:
  246. mapsets.remove(mapset)
  247. mapsets = mapsets_accessible + mapsets
  248. else:
  249. return None
  250. return mapsets
  251. def ListSortLower(list):
  252. """Sort list items (not case-sensitive)"""
  253. list.sort(cmp=lambda x, y: cmp(x.lower(), y.lower()))
  254. def GetVectorNumberOfLayers(vector):
  255. """Get list of all vector layers"""
  256. layers = list()
  257. if not vector:
  258. return layers
  259. fullname = grass.find_file(name = vector, element = 'vector')['fullname']
  260. if not fullname:
  261. Debug.msg(5, "utils.GetVectorNumberOfLayers(): vector map '%s' not found" % vector)
  262. return layers
  263. ret, out, msg = RunCommand('v.category',
  264. getErrorMsg = True,
  265. read = True,
  266. input = fullname,
  267. option = 'layers')
  268. if ret != 0:
  269. sys.stderr.write(_("Vector map <%(map)s>: %(msg)s\n") % { 'map' : fullname, 'msg' : msg })
  270. return layers
  271. else:
  272. Debug.msg(1, "GetVectorNumberOfLayers(): ret %s" % ret)
  273. for layer in out.splitlines():
  274. layers.append(layer)
  275. Debug.msg(3, "utils.GetVectorNumberOfLayers(): vector=%s -> %s" % \
  276. (fullname, ','.join(layers)))
  277. return layers
  278. def Deg2DMS(lon, lat, string = True, hemisphere = True, precision = 3):
  279. """Convert deg value to dms string
  280. :param lon: longitude (x)
  281. :param lat: latitude (y)
  282. :param string: True to return string otherwise tuple
  283. :param hemisphere: print hemisphere
  284. :param precision: seconds precision
  285. :return: DMS string or tuple of values
  286. :return: empty string on error
  287. """
  288. try:
  289. flat = float(lat)
  290. flon = float(lon)
  291. except ValueError:
  292. if string:
  293. return ''
  294. else:
  295. return None
  296. # fix longitude
  297. while flon > 180.0:
  298. flon -= 360.0
  299. while flon < -180.0:
  300. flon += 360.0
  301. # hemisphere
  302. if hemisphere:
  303. if flat < 0.0:
  304. flat = abs(flat)
  305. hlat = 'S'
  306. else:
  307. hlat = 'N'
  308. if flon < 0.0:
  309. hlon = 'W'
  310. flon = abs(flon)
  311. else:
  312. hlon = 'E'
  313. else:
  314. flat = abs(flat)
  315. flon = abs(flon)
  316. hlon = ''
  317. hlat = ''
  318. slat = __ll_parts(flat, precision = precision)
  319. slon = __ll_parts(flon, precision = precision)
  320. if string:
  321. return slon + hlon + '; ' + slat + hlat
  322. return (slon + hlon, slat + hlat)
  323. def DMS2Deg(lon, lat):
  324. """Convert dms value to deg
  325. :param lon: longitude (x)
  326. :param lat: latitude (y)
  327. :return: tuple of converted values
  328. :return: ValueError on error
  329. """
  330. x = __ll_parts(lon, reverse = True)
  331. y = __ll_parts(lat, reverse = True)
  332. return (x, y)
  333. def __ll_parts(value, reverse = False, precision = 3):
  334. """Converts deg to d:m:s string
  335. :param value: value to be converted
  336. :param reverse: True to convert from d:m:s to deg
  337. :param precision: seconds precision (ignored if reverse is True)
  338. :return: converted value (string/float)
  339. :return: ValueError on error (reverse == True)
  340. """
  341. if not reverse:
  342. if value == 0.0:
  343. return '%s%.*f' % ('00:00:0', precision, 0.0)
  344. d = int(int(value))
  345. m = int((value - d) * 60)
  346. s = ((value - d) * 60 - m) * 60
  347. if m < 0:
  348. m = '00'
  349. elif m < 10:
  350. m = '0' + str(m)
  351. else:
  352. m = str(m)
  353. if s < 0:
  354. s = '00.0000'
  355. elif s < 10.0:
  356. s = '0%.*f' % (precision, s)
  357. else:
  358. s = '%.*f' % (precision, s)
  359. return str(d) + ':' + m + ':' + s
  360. else: # -> reverse
  361. try:
  362. d, m, s = value.split(':')
  363. hs = s[-1]
  364. s = s[:-1]
  365. except ValueError:
  366. try:
  367. d, m = value.split(':')
  368. hs = m[-1]
  369. m = m[:-1]
  370. s = '0.0'
  371. except ValueError:
  372. try:
  373. d = value
  374. hs = d[-1]
  375. d = d[:-1]
  376. m = '0'
  377. s = '0.0'
  378. except ValueError:
  379. raise ValueError
  380. if hs not in ('N', 'S', 'E', 'W'):
  381. raise ValueError
  382. coef = 1.0
  383. if hs in ('S', 'W'):
  384. coef = -1.0
  385. fm = int(m) / 60.0
  386. fs = float(s) / (60 * 60)
  387. return coef * (float(d) + fm + fs)
  388. def GetCmdString(cmd):
  389. """Get GRASS command as string.
  390. :param cmd: GRASS command given as tuple
  391. :return: command string
  392. """
  393. return ' '.join(CmdTupleToList(cmd))
  394. def CmdTupleToList(cmd):
  395. """Convert command tuple to list.
  396. :param cmd: GRASS command given as tuple
  397. :return: command in list
  398. """
  399. cmdList = []
  400. if not cmd:
  401. return cmdList
  402. cmdList.append(cmd[0])
  403. if 'flags' in cmd[1]:
  404. for flag in cmd[1]['flags']:
  405. cmdList.append('-' + flag)
  406. for flag in ('help', 'verbose', 'quiet', 'overwrite'):
  407. if flag in cmd[1] and cmd[1][flag] is True:
  408. cmdList.append('--' + flag)
  409. for k, v in cmd[1].iteritems():
  410. if k in ('flags', 'help', 'verbose', 'quiet', 'overwrite'):
  411. continue
  412. cmdList.append('%s=%s' % (k, v))
  413. return cmdList
  414. def CmdToTuple(cmd):
  415. """Convert command list to tuple for gcmd.RunCommand()"""
  416. if len(cmd) < 1:
  417. return None
  418. dcmd = {}
  419. for item in cmd[1:]:
  420. if '=' in item: # params
  421. key, value = item.split('=', 1)
  422. dcmd[str(key)] = str(value).replace('"', '')
  423. elif item[:2] == '--': # long flags
  424. flag = item[2:]
  425. if flag in ('help', 'verbose', 'quiet', 'overwrite'):
  426. dcmd[str(flag)] = True
  427. elif len(item) == 2 and item[0] == '-': # -> flags
  428. if 'flags' not in dcmd:
  429. dcmd['flags'] = ''
  430. dcmd['flags'] += item[1]
  431. else: # unnamed parameter
  432. module = gtask.parse_interface(cmd[0])
  433. dcmd[module.define_first()] = item
  434. return (cmd[0], dcmd)
  435. def PathJoin(*args):
  436. """Check path created by os.path.join"""
  437. path = os.path.join(*args)
  438. if platform.system() == 'Windows' and \
  439. '/' in path:
  440. return path[1].upper() + ':\\' + path[3:].replace('/', '\\')
  441. return path
  442. def ReadEpsgCodes(path):
  443. """Read EPSG code from the file
  444. :param path: full path to the file with EPSG codes
  445. :return: dictionary of EPSG code
  446. :return: string on error
  447. """
  448. epsgCodeDict = dict()
  449. try:
  450. try:
  451. f = open(path, "r")
  452. except IOError:
  453. return _("failed to open '%s'" % path)
  454. code = None
  455. for line in f.readlines():
  456. line = line.strip()
  457. if len(line) < 1:
  458. continue
  459. if line[0] == '#':
  460. descr = line[1:].strip()
  461. elif line[0] == '<':
  462. code, params = line.split(" ", 1)
  463. try:
  464. code = int(code.replace('<', '').replace('>', ''))
  465. except ValueError as e:
  466. return e
  467. if code is not None:
  468. epsgCodeDict[code] = (descr, params)
  469. code = None
  470. f.close()
  471. except StandardError as e:
  472. return e
  473. return epsgCodeDict
  474. def ReprojectCoordinates(coord, projOut, projIn = None, flags = ''):
  475. """Reproject coordinates
  476. :param coord: coordinates given as tuple
  477. :param projOut: output projection
  478. :param projIn: input projection (use location projection settings)
  479. :return: reprojected coordinates (returned as tuple)
  480. """
  481. coors = RunCommand('m.proj',
  482. flags = flags,
  483. input = '-',
  484. proj_in = projIn,
  485. proj_out = projOut,
  486. sep = ';',
  487. stdin = '%f;%f' % (coord[0], coord[1]),
  488. read = True)
  489. if coors:
  490. coors = coors.split(';')
  491. e = coors[0]
  492. n = coors[1]
  493. try:
  494. proj = projOut.split(' ')[0].split('=')[1]
  495. except IndexError:
  496. proj = ''
  497. if proj in ('ll', 'latlong', 'longlat') and 'd' not in flags:
  498. return (proj, (e, n))
  499. else:
  500. try:
  501. return (proj, (float(e), float(n)))
  502. except ValueError:
  503. return (None, None)
  504. return (None, None)
  505. def GetListOfLocations(dbase):
  506. """Get list of GRASS locations in given dbase
  507. :param dbase: GRASS database path
  508. :return: list of locations (sorted)
  509. """
  510. listOfLocations = list()
  511. try:
  512. for location in glob.glob(os.path.join(dbase, "*")):
  513. try:
  514. if os.path.join(location, "PERMANENT") in glob.glob(os.path.join(location, "*")):
  515. listOfLocations.append(os.path.basename(location))
  516. except:
  517. pass
  518. except UnicodeEncodeError as e:
  519. raise e
  520. ListSortLower(listOfLocations)
  521. return listOfLocations
  522. def GetListOfMapsets(dbase, location, selectable = False):
  523. """Get list of mapsets in given GRASS location
  524. :param dbase: GRASS database path
  525. :param location: GRASS location
  526. :param selectable: True to get list of selectable mapsets, otherwise all
  527. :return: list of mapsets - sorted (PERMANENT first)
  528. """
  529. listOfMapsets = list()
  530. if selectable:
  531. ret = RunCommand('g.mapset',
  532. read = True,
  533. flags = 'l',
  534. location = location,
  535. gisdbase = dbase)
  536. if not ret:
  537. return listOfMapsets
  538. for line in ret.rstrip().splitlines():
  539. listOfMapsets += line.split(' ')
  540. else:
  541. for mapset in glob.glob(os.path.join(dbase, location, "*")):
  542. if os.path.isdir(mapset) and \
  543. os.path.isfile(os.path.join(dbase, location, mapset, "WIND")):
  544. listOfMapsets.append(os.path.basename(mapset))
  545. ListSortLower(listOfMapsets)
  546. return listOfMapsets
  547. def GetColorTables():
  548. """Get list of color tables"""
  549. ret = RunCommand('r.colors',
  550. read = True,
  551. flags = 'l')
  552. if not ret:
  553. return list()
  554. return ret.splitlines()
  555. def _getGDALFormats():
  556. """Get dictionary of avaialble GDAL drivers"""
  557. try:
  558. ret = grass.read_command('r.in.gdal',
  559. quiet = True,
  560. flags = 'f')
  561. except:
  562. ret = None
  563. return _parseFormats(ret), _parseFormats(ret, writableOnly = True)
  564. def _getOGRFormats():
  565. """Get dictionary of avaialble OGR drivers"""
  566. try:
  567. ret = grass.read_command('v.in.ogr',
  568. quiet = True,
  569. flags = 'f')
  570. except:
  571. ret = None
  572. return _parseFormats(ret), _parseFormats(ret, writableOnly = True)
  573. def _parseFormats(output, writableOnly = False):
  574. """Parse r.in.gdal/v.in.ogr -f output"""
  575. formats = { 'file' : list(),
  576. 'database' : list(),
  577. 'protocol' : list()
  578. }
  579. if not output:
  580. return formats
  581. patt = None
  582. if writableOnly:
  583. patt = re.compile('\(rw\+?\)$', re.IGNORECASE)
  584. for line in output.splitlines():
  585. key, name = map(lambda x: x.strip(), line.strip().rsplit(':', -1))
  586. if writableOnly and not patt.search(key):
  587. continue
  588. if name in ('Memory', 'Virtual Raster', 'In Memory Raster'):
  589. continue
  590. if name in ('PostgreSQL', 'SQLite',
  591. 'ODBC', 'ESRI Personal GeoDatabase',
  592. 'Rasterlite',
  593. 'PostGIS WKT Raster driver',
  594. 'PostGIS Raster driver',
  595. 'CouchDB',
  596. 'MSSQLSpatial',
  597. 'FileGDB'):
  598. formats['database'].append(name)
  599. elif name in ('GeoJSON',
  600. 'OGC Web Coverage Service',
  601. 'OGC Web Map Service',
  602. 'WFS',
  603. 'GeoRSS',
  604. 'HTTP Fetching Wrapper'):
  605. formats['protocol'].append(name)
  606. else:
  607. formats['file'].append(name)
  608. for items in formats.itervalues():
  609. items.sort()
  610. return formats
  611. formats = None
  612. def GetFormats(writableOnly = False):
  613. """Get GDAL/OGR formats"""
  614. global formats
  615. if not formats:
  616. gdalAll, gdalWritable = _getGDALFormats()
  617. ogrAll, ogrWritable = _getOGRFormats()
  618. formats = {
  619. 'all' : {
  620. 'gdal' : gdalAll,
  621. 'ogr' : ogrAll,
  622. },
  623. 'writable' : {
  624. 'gdal' : gdalWritable,
  625. 'ogr' : ogrWritable,
  626. },
  627. }
  628. if writableOnly:
  629. return formats['writable']
  630. return formats['all']
  631. rasterFormatExtension = {
  632. 'GeoTIFF' : 'tif',
  633. 'Erdas Imagine Images (.img)' : 'img',
  634. 'Ground-based SAR Applications Testbed File Format (.gff)' : 'gff',
  635. 'Arc/Info Binary Grid' : 'adf',
  636. 'Portable Network Graphics' : 'png',
  637. 'JPEG JFIF' : 'jpg',
  638. 'Japanese DEM (.mem)' : 'mem',
  639. 'Graphics Interchange Format (.gif)' : 'gif',
  640. 'X11 PixMap Format' : 'xpm',
  641. 'MS Windows Device Independent Bitmap' : 'bmp',
  642. 'SPOT DIMAP' : 'dim',
  643. 'RadarSat 2 XML Product' : 'xml',
  644. 'EarthWatch .TIL' : 'til',
  645. 'ERMapper .ers Labelled' : 'ers',
  646. 'ERMapper Compressed Wavelets' : 'ecw',
  647. 'GRIdded Binary (.grb)' : 'grb',
  648. 'EUMETSAT Archive native (.nat)' : 'nat',
  649. 'Idrisi Raster A.1' : 'rst',
  650. 'Golden Software ASCII Grid (.grd)' : 'grd',
  651. 'Golden Software Binary Grid (.grd)' : 'grd',
  652. 'Golden Software 7 Binary Grid (.grd)' : 'grd',
  653. 'R Object Data Store' : 'r',
  654. 'USGS DOQ (Old Style)' : 'doq',
  655. 'USGS DOQ (New Style)' : 'doq',
  656. 'ENVI .hdr Labelled' : 'hdr',
  657. 'ESRI .hdr Labelled' : 'hdr',
  658. 'Generic Binary (.hdr Labelled)' : 'hdr',
  659. 'PCI .aux Labelled' : 'aux',
  660. 'EOSAT FAST Format' : 'fst',
  661. 'VTP .bt (Binary Terrain) 1.3 Format' : 'bt',
  662. 'FARSITE v.4 Landscape File (.lcp)' : 'lcp',
  663. 'Swedish Grid RIK (.rik)' : 'rik',
  664. 'USGS Optional ASCII DEM (and CDED)' : 'dem',
  665. 'Northwood Numeric Grid Format .grd/.tab' : '',
  666. 'Northwood Classified Grid Format .grc/.tab' : '',
  667. 'ARC Digitized Raster Graphics' : 'arc',
  668. 'Magellan topo (.blx)' : 'blx',
  669. 'SAGA GIS Binary Grid (.sdat)' : 'sdat'
  670. }
  671. vectorFormatExtension = {
  672. 'ESRI Shapefile' : 'shp',
  673. 'UK .NTF' : 'ntf',
  674. 'SDTS' : 'ddf',
  675. 'DGN' : 'dgn',
  676. 'VRT' : 'vrt',
  677. 'REC' : 'rec',
  678. 'BNA' : 'bna',
  679. 'CSV' : 'csv',
  680. 'GML' : 'gml',
  681. 'GPX' : 'gpx',
  682. 'KML' : 'kml',
  683. 'GMT' : 'gmt',
  684. 'PGeo' : 'mdb',
  685. 'XPlane' : 'dat',
  686. 'AVCBin' : 'adf',
  687. 'AVCE00' : 'e00',
  688. 'DXF' : 'dxf',
  689. 'Geoconcept' : 'gxt',
  690. 'GeoRSS' : 'xml',
  691. 'GPSTrackMaker' : 'gtm',
  692. 'VFK' : 'vfk',
  693. 'SVG' : 'svg',
  694. }
  695. def GetSettingsPath():
  696. """Get full path to the settings directory
  697. """
  698. try:
  699. verFd = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  700. version = int(verFd.readlines()[0].split(' ')[0].split('.')[0])
  701. except (IOError, ValueError, TypeError, IndexError) as e:
  702. sys.exit(_("ERROR: Unable to determine GRASS version. Details: %s") % e)
  703. verFd.close()
  704. # keep location of settings files rc and wx in sync with lib/init/grass.py
  705. if sys.platform == 'win32':
  706. return os.path.join(os.getenv('APPDATA'), 'GRASS%d' % version)
  707. return os.path.join(os.getenv('HOME'), '.grass%d' % version)
  708. def StoreEnvVariable(key, value = None, envFile = None):
  709. """Store environmental variable
  710. If value is not given (is None) then environmental variable is
  711. unset.
  712. :param key: env key
  713. :param value: env value
  714. :param envFile: path to the environmental file (None for default location)
  715. """
  716. windows = sys.platform == 'win32'
  717. if not envFile:
  718. gVersion = grass.version()['version'].split('.', 1)[0]
  719. if not windows:
  720. envFile = os.path.join(os.getenv('HOME'), '.grass%s' % gVersion, 'bashrc')
  721. else:
  722. envFile = os.path.join(os.getenv('APPDATA'), 'GRASS%s' % gVersion, 'env.bat')
  723. # read env file
  724. environ = dict()
  725. lineSkipped = list()
  726. if os.path.exists(envFile):
  727. try:
  728. fd = open(envFile)
  729. except IOError as e:
  730. sys.stderr.write(_("Unable to open file '%s'\n") % envFile)
  731. return
  732. for line in fd.readlines():
  733. line = line.rstrip(os.linesep)
  734. try:
  735. k, v = map(lambda x: x.strip(), line.split(' ', 1)[1].split('=', 1))
  736. except StandardError as e:
  737. sys.stderr.write(_("%s: line skipped - unable to parse '%s'\n"
  738. "Reason: %s\n") % (envFile, line, e))
  739. lineSkipped.append(line)
  740. continue
  741. if k in environ:
  742. sys.stderr.write(_("Duplicated key: %s\n") % k)
  743. environ[k] = v
  744. fd.close()
  745. # update environmental variables
  746. if value is None:
  747. if key in environ:
  748. del environ[key]
  749. else:
  750. environ[key] = value
  751. # write update env file
  752. try:
  753. fd = open(envFile, 'w')
  754. except IOError as e:
  755. sys.stderr.write(_("Unable to create file '%s'\n") % envFile)
  756. return
  757. if windows:
  758. expCmd = 'set'
  759. else:
  760. expCmd = 'export'
  761. for key, value in environ.iteritems():
  762. fd.write('%s %s=%s\n' % (expCmd, key, value))
  763. # write also skipped lines
  764. for line in lineSkipped:
  765. fd.write(line + os.linesep)
  766. fd.close()
  767. def SetAddOnPath(addonPath = None, key = 'PATH'):
  768. """Set default AddOn path
  769. :param addonPath: path to addons (None for default)
  770. :param key: env key - 'PATH' or 'BASE'
  771. """
  772. gVersion = grass.version()['version'].split('.', 1)[0]
  773. # update env file
  774. if not addonPath:
  775. if sys.platform != 'win32':
  776. addonPath = os.path.join(os.path.join(os.getenv('HOME'),
  777. '.grass%s' % gVersion,
  778. 'addons'))
  779. else:
  780. addonPath = os.path.join(os.path.join(os.getenv('APPDATA'),
  781. 'GRASS%s' % gVersion,
  782. 'addons'))
  783. StoreEnvVariable(key = 'GRASS_ADDON_' + key, value = addonPath)
  784. os.environ['GRASS_ADDON_' + key] = addonPath
  785. # update path
  786. if addonPath not in os.environ['PATH']:
  787. os.environ['PATH'] = addonPath + os.pathsep + os.environ['PATH']
  788. # predefined colors and their names
  789. # must be in sync with lib/gis/color_str.c
  790. str2rgb = {'aqua': (100, 128, 255),
  791. 'black': (0, 0, 0),
  792. 'blue': (0, 0, 255),
  793. 'brown': (180, 77, 25),
  794. 'cyan': (0, 255, 255),
  795. 'gray': (128, 128, 128),
  796. 'grey': (128, 128, 128),
  797. 'green': (0, 255, 0),
  798. 'indigo': (0, 128, 255),
  799. 'magenta': (255, 0, 255),
  800. 'orange': (255, 128, 0),
  801. 'red': (255, 0, 0),
  802. 'violet': (128, 0, 255),
  803. 'purple': (128, 0, 255),
  804. 'white': (255, 255, 255),
  805. 'yellow': (255, 255, 0)}
  806. rgb2str = {}
  807. for (s, r) in str2rgb.items():
  808. rgb2str[r] = s
  809. # ensure that gray value has 'gray' string and not 'grey'
  810. rgb2str[str2rgb['gray']] = 'gray'
  811. # purple is defined as nickname for violet in lib/gis
  812. # (although Wikipedia says that purple is (128, 0, 128))
  813. # we will prefer the defined color, not nickname
  814. rgb2str[str2rgb['violet']] = 'violet'
  815. def color_resolve(color):
  816. if len(color) > 0 and color[0] in "0123456789":
  817. rgb = tuple(map(int, color.split(':')))
  818. label = color
  819. else:
  820. # Convert color names to RGB
  821. try:
  822. rgb = str2rgb[color]
  823. label = color
  824. except KeyError:
  825. rgb = (200, 200, 200)
  826. label = _('Select Color')
  827. return (rgb, label)
  828. command2ltype = {'d.rast' : 'raster',
  829. 'd.rast3d' : '3d-raster',
  830. 'd.rgb' : 'rgb',
  831. 'd.his' : 'his',
  832. 'd.shadedmap' : 'shaded',
  833. 'd.legend' : 'rastleg',
  834. 'd.rast.arrow' : 'rastarrow',
  835. 'd.rast.num' : 'rastnum',
  836. 'd.rast.leg' : 'maplegend',
  837. 'd.vect' : 'vector',
  838. 'd.thematic.area': 'thememap',
  839. 'd.vect.chart' : 'themechart',
  840. 'd.grid' : 'grid',
  841. 'd.geodesic' : 'geodesic',
  842. 'd.rhumbline' : 'rhumb',
  843. 'd.labels' : 'labels',
  844. 'd.barscale' : 'barscale',
  845. 'd.redraw' : 'redraw',
  846. 'd.wms' : 'wms',
  847. 'd.histogram' : 'histogram',
  848. 'd.colortable' : 'colortable',
  849. 'd.graph' : 'graph',
  850. 'd.out.file' : 'export',
  851. 'd.to.rast' : 'torast',
  852. 'd.text' : 'text'
  853. }
  854. ltype2command = {}
  855. for (cmd, ltype) in command2ltype.items():
  856. ltype2command[ltype] = cmd
  857. def GetGEventAttribsForHandler(method, event):
  858. """Get attributes from event, which can be used by handler method.
  859. Be aware of event class attributes.
  860. :param method: handler method (including self arg)
  861. :param event: event
  862. :return: (valid kwargs for method,
  863. list of method's args without default value
  864. which were not found among event attributes)
  865. """
  866. args_spec = inspect.getargspec(method)
  867. args = args_spec[0]
  868. defaults =[]
  869. if args_spec[3]:
  870. defaults = args_spec[3]
  871. # number of arguments without def value
  872. req_args = len(args) - 1 - len(defaults)
  873. kwargs = {}
  874. missing_args = []
  875. for i, a in enumerate(args):
  876. if hasattr(event, a):
  877. kwargs[a] = getattr(event, a)
  878. elif i < req_args:
  879. missing_args.append(a)
  880. return kwargs, missing_args
  881. def GuiModuleMain(mainfn):
  882. """Main function for g.gui.* modules
  883. Note: os.fork() is supported only on Unix platforms
  884. .. todo::
  885. Replace os.fork() by multiprocessing (?)
  886. :param mainfn: main function
  887. """
  888. mainfn()
  889. def PilImageToWxImage(pilImage, copyAlpha = True):
  890. """Convert PIL image to wx.Image
  891. Based on http://wiki.wxpython.org/WorkingWithImages
  892. """
  893. import wx
  894. hasAlpha = pilImage.mode[-1] == 'A'
  895. if copyAlpha and hasAlpha : # Make sure there is an alpha layer copy.
  896. wxImage = wx.EmptyImage(*pilImage.size)
  897. pilImageCopyRGBA = pilImage.copy()
  898. pilImageCopyRGB = pilImageCopyRGBA.convert('RGB') # RGBA --> RGB
  899. pilImageRgbData = pilImageCopyRGB.tostring()
  900. wxImage.SetData(pilImageRgbData)
  901. wxImage.SetAlphaData(pilImageCopyRGBA.tostring()[3::4]) # Create layer and insert alpha values.
  902. else : # The resulting image will not have alpha.
  903. wxImage = wx.EmptyImage(*pilImage.size)
  904. pilImageCopy = pilImage.copy()
  905. pilImageCopyRGB = pilImageCopy.convert('RGB') # Discard any alpha from the PIL image.
  906. pilImageRgbData = pilImageCopyRGB.tostring()
  907. wxImage.SetData(pilImageRgbData)
  908. return wxImage
  909. def autoCropImageFromFile(filename):
  910. """Loads image from file and crops it automatically.
  911. If PIL is not installed, it does not crop it.
  912. :param filename: path to file
  913. :return: wx.Image instance
  914. """
  915. try:
  916. from PIL import Image
  917. pilImage = Image.open(filename)
  918. imageBox = pilImage.getbbox()
  919. cropped = pilImage.crop(imageBox)
  920. return PilImageToWxImage(cropped, copyAlpha=True)
  921. except ImportError:
  922. import wx
  923. return wx.Image(filename)
  924. def isInRegion(regionA, regionB):
  925. """Tests if 'regionA' is inside of 'regionB'.
  926. For example, region A is a display region and region B is some reference
  927. region, e.g., a computational region.
  928. >>> displayRegion = {'n': 223900, 's': 217190, 'w': 630780, 'e': 640690}
  929. >>> compRegion = {'n': 228500, 's': 215000, 'w': 630000, 'e': 645000}
  930. >>> isInRegion(displayRegion, compRegion)
  931. True
  932. >>> displayRegion = {'n':226020, 's': 212610, 'w': 626510, 'e': 646330}
  933. >>> isInRegion(displayRegion, compRegion)
  934. False
  935. :param regionA: input region A as dictionary
  936. :param regionB: input region B as dictionary
  937. :return: True if region A is inside of region B
  938. :return: False othewise
  939. """
  940. if regionA['s'] >= regionB['s'] and \
  941. regionA['n'] <= regionB['n'] and \
  942. regionA['w'] >= regionB['w'] and \
  943. regionA['e'] <= regionB['e']:
  944. return True
  945. return False
  946. def do_doctest_gettext_workaround():
  947. """Setups environment for doing a doctest with gettext usage.
  948. When using gettext with dynamically defined underscore function
  949. (`_("For translation")`), doctest does not work properly. One option is to
  950. use `import as` instead of dynamically defined underscore function but this
  951. would require change all modules which are used by tested module. This
  952. should be considered for the future. The second option is to define dummy
  953. underscore function and one other function which creates the right
  954. environment to satisfy all. This is done by this function.
  955. """
  956. def new_displayhook(string):
  957. """A replacement for default `sys.displayhook`"""
  958. if string is not None:
  959. sys.stdout.write("%r\n" % (string,))
  960. def new_translator(string):
  961. """A fake gettext underscore function."""
  962. return string
  963. sys.displayhook = new_displayhook
  964. import __builtin__
  965. __builtin__._ = new_translator
  966. def doc_test():
  967. """Tests the module using doctest
  968. :return: a number of failed tests
  969. """
  970. import doctest
  971. do_doctest_gettext_workaround()
  972. return doctest.testmod().failed
  973. if __name__ == '__main__':
  974. sys.exit(doc_test())