functions.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Nov 26 11:48:03 2012
  4. @author: lucadelu
  5. """
  6. import wx
  7. import os
  8. import sys
  9. from grass.script import core as grass
  10. from core.gcmd import GError
  11. class SamplingType:
  12. """"
  13. KMVWINC = samplingtype=moving, regionbox=keyboard, shape=circle
  14. KMVWINR = samplingtype moving, regionbox=keyboard, shape=rectangle
  15. MMVWINC = samplingtype=moving, regionbox=mouse, shape=circle
  16. MMVWINR = samplingtype moving, regionbox=mouse, shape=rectangle
  17. KUNITSC = samplingtype=units, regionbox=keyboard, shape=cirlce
  18. KUNITSR = samplingtype=units, regionbox=keyboard, shape=rectangle
  19. MUNITSC = samplingtype=units, regionbox=mouse, shape=cirlce
  20. MUNITSR = samplingtype=units, regionbox=mouse, shape=rectangle
  21. """
  22. WHOLE = 'whole'
  23. REGIONS = 'regions'
  24. UNITS = 'units'
  25. VECT = 'vect'
  26. MVWIN = 'moving'
  27. KMVWINC = 'kmvwin_circle'
  28. KMVWINR = 'kmvwin_rectangle'
  29. MMVWINC = 'mmvwin_circle'
  30. MMVWINR = 'mmvwin_rectangle'
  31. KUNITSC = 'kunits_circle'
  32. KUNITSR = 'kunits_rectangle'
  33. MUNITSC = 'munits_circle'
  34. MUNITSR = 'munits_rectangle'
  35. def checkValue(value):
  36. if value == '':
  37. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  38. else:
  39. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  40. def retRLiPath():
  41. """Return the directory of configuration files for r.li"""
  42. if sys.platform == 'win32':
  43. grass_config_dirname = "GRASS7"
  44. grass_config_dir = os.path.join(os.getenv('APPDATA'),
  45. grass_config_dirname)
  46. else:
  47. grass_config_dirname = ".grass7"
  48. grass_config_dir = os.path.join(os.getenv('HOME'),
  49. grass_config_dirname)
  50. rlipath = os.path.join(grass_config_dir, 'r.li')
  51. if os.path.exists(rlipath):
  52. return rlipath
  53. else:
  54. os.mkdir(rlipath)
  55. return rlipath
  56. def checkMapExists(name, typ='rast'):
  57. """Check if a map already exist in the working mapset"""
  58. env = grass.gisenv()
  59. mapset = env['MAPSET']
  60. mapp = grass.find_file(name, typ, mapset)
  61. if mapp.name != '':
  62. return True
  63. else:
  64. return False
  65. def convertFeature(vect, outrast, cat, origrast):
  66. """Convert a single feature to a raster"""
  67. tmp_vect = "tmp_{rast}".format(rast=outrast)
  68. grass.run_command('v.extract', input=vect, output=tmp_vect, cats=cat,
  69. overwrite=True, quiet=True)
  70. grass.run_command('g.region', vect=tmp_vect, align=origrast)
  71. grass.run_command('v.to.rast', input=vect, output=outrast, use='cat',
  72. cats=cat, overwrite=True, quiet=True)
  73. grass.run_command('g.remove', flags='f', type='vect',
  74. name=tmp_vect, quiet=True)
  75. def obtainAreaVector(outrast):
  76. """Create the string for configuration file"""
  77. reg = grass.region()
  78. return "MASKEDOVERLAYAREA {name}|{n}|{s}|{e}|{w}\n".format(name=outrast,
  79. n=reg['n'],
  80. s=reg['s'],
  81. e=reg['e'],
  82. w=reg['w'])
  83. def sampleAreaVector(vect, rast, vect_cats, progDialog=None):
  84. """Create the strings to add to the configuration file using vector"""
  85. areanum = len(vect_cats)
  86. output = []
  87. #TODO if areanum == 0 exit from the program
  88. if areanum == 0:
  89. GError(message=_("The polygon seems to have 0 areas"))
  90. return None
  91. for n in range(areanum):
  92. cat = vect_cats[n]
  93. rast_name = "{name}_{cat}".format(name=vect.split('@')[0], cat=cat)
  94. convertFeature(vect, rast_name, cat, rast)
  95. output.append(obtainAreaVector(rast_name))
  96. if progDialog:
  97. progDialog.Update(n)
  98. return output