functions.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """
  2. Created on Mon Nov 26 11:48:03 2012
  3. @author: lucadelu
  4. """
  5. import wx
  6. import os
  7. import sys
  8. from grass.script import core as grass
  9. from core.gcmd import GError
  10. class SamplingType:
  11. """"
  12. KMVWINC = samplingtype=moving, regionbox=keyboard, shape=circle
  13. KMVWINR = samplingtype moving, regionbox=keyboard, shape=rectangle
  14. MMVWINC = samplingtype=moving, regionbox=mouse, shape=circle
  15. MMVWINR = samplingtype moving, regionbox=mouse, shape=rectangle
  16. KUNITSC = samplingtype=units, regionbox=keyboard, shape=cirlce
  17. KUNITSR = samplingtype=units, regionbox=keyboard, shape=rectangle
  18. MUNITSC = samplingtype=units, regionbox=mouse, shape=cirlce
  19. MUNITSR = samplingtype=units, regionbox=mouse, shape=rectangle
  20. """
  21. WHOLE = 'whole'
  22. REGIONS = 'regions'
  23. UNITS = 'units'
  24. VECT = 'vector'
  25. MVWIN = 'moving'
  26. KMVWINC = 'kmvwin_circle'
  27. KMVWINR = 'kmvwin_rectangle'
  28. MMVWINC = 'mmvwin_circle'
  29. MMVWINR = 'mmvwin_rectangle'
  30. KUNITSC = 'kunits_circle'
  31. KUNITSR = 'kunits_rectangle'
  32. MUNITSC = 'munits_circle'
  33. MUNITSR = 'munits_rectangle'
  34. def checkValue(value):
  35. if value == '':
  36. wx.FindWindowById(wx.ID_FORWARD).Enable(False)
  37. else:
  38. wx.FindWindowById(wx.ID_FORWARD).Enable(True)
  39. def retRLiPath():
  40. """Return the directory of configuration files for r.li"""
  41. if sys.platform == 'win32':
  42. grass_config_dirname = "GRASS7"
  43. grass_config_dir = os.path.join(os.getenv('APPDATA'),
  44. grass_config_dirname)
  45. else:
  46. grass_config_dirname = ".grass7"
  47. grass_config_dir = os.path.join(os.getenv('HOME'),
  48. grass_config_dirname)
  49. rlipath = os.path.join(grass_config_dir, 'r.li')
  50. if os.path.exists(rlipath):
  51. return rlipath
  52. else:
  53. os.mkdir(rlipath)
  54. return rlipath
  55. def checkMapExists(name, typ='raster'):
  56. """Check if a map already exist in the working mapset"""
  57. env = grass.gisenv()
  58. mapset = env['MAPSET']
  59. mapp = grass.find_file(name, typ, mapset)
  60. if mapp.name != '':
  61. return True
  62. else:
  63. return False
  64. def convertFeature(vect, outrast, cat, origrast, layer='1', overwrite=False):
  65. """Convert a single feature to a raster"""
  66. tmp_vect = "tmp_{rast}".format(rast=outrast)
  67. grass.run_command('v.extract', input=vect, cats=cat, type='area',
  68. layer=layer, output=tmp_vect, flags='d',
  69. overwrite=overwrite, quiet=True)
  70. grass.run_command('g.region', raster=origrast)
  71. grass.run_command('g.region', vector=tmp_vect)
  72. grass.run_command('g.region', align=origrast)
  73. grass.run_command('v.to.rast', input=tmp_vect, type='area',
  74. layer=layer, use='val', value=cat, output=outrast,
  75. overwrite=overwrite, quiet=True)
  76. grass.run_command('g.remove', flags='f', type='vector',
  77. name=tmp_vect, quiet=True)
  78. def obtainCategories(vector, layer='1'):
  79. """This function returns a list of categories for all areas in
  80. the given layer"""
  81. vect_cats = []
  82. vc = grass.read_command('v.category', input=vector, layer=layer,
  83. option='print', type='centroid')
  84. for lc in vc.splitlines():
  85. for cat in lc.split('/'):
  86. vect_cats.append(int(cat))
  87. return sorted(set(vect_cats))
  88. def obtainAreaVector(outrast):
  89. """Create the string for configuration file"""
  90. reg = grass.region()
  91. return "MASKEDOVERLAYAREA {name}|{n}|{s}|{e}|{w}\n".format(name=outrast,
  92. n=reg['n'],
  93. s=reg['s'],
  94. e=reg['e'],
  95. w=reg['w'])
  96. def sampleAreaVector(vect, rast, vect_cats, layer='1', overwrite=False,
  97. progDialog=None):
  98. """Create the strings to add to the configuration file using vector"""
  99. areanum = len(vect_cats)
  100. output = []
  101. # TODO if areanum == 0 exit from the program
  102. if areanum == 0:
  103. GError(message=_("The polygon seems to have 0 areas"))
  104. return None
  105. for n in range(areanum):
  106. cat = str(vect_cats[n])
  107. outpref = "{rast}_{vect}_".format(vect=vect.split('@')[0],
  108. rast=rast.split('@')[0])
  109. rast_name = "{pref}{cat}".format(pref=outpref, cat=cat)
  110. # check if raster already axist
  111. if len(grass.list_strings('raster', pattern=rast_name,
  112. mapset='.')) == 1 and not overwrite:
  113. GError(message=_("The raster map <%s> already exists."
  114. " Please remove or rename the maps "
  115. "with the prefix '%s' or select the "
  116. "option to overwrite existing maps"
  117. % (rast_name, outpref)))
  118. return None
  119. convertFeature(vect, rast_name, cat, rast, layer, overwrite)
  120. output.append(obtainAreaVector(rast_name))
  121. if progDialog:
  122. progDialog.Update(n)
  123. return output