functions.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = 'vector'
  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='raster'):
  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, layer='1', overwrite=False):
  66. """Convert a single feature to a raster"""
  67. tmp_vect = "tmp_{rast}".format(rast=outrast)
  68. grass.run_command('v.extract', input=vect, cats=cat, type='area',
  69. layer=layer, output=tmp_vect, flags='d',
  70. overwrite=overwrite, quiet=True)
  71. grass.run_command('g.region', raster=origrast)
  72. grass.run_command('g.region', vector=tmp_vect)
  73. grass.run_command('g.region', align=origrast)
  74. grass.run_command('v.to.rast', input=tmp_vect, type='area',
  75. layer=layer, use='val', value=cat, output=outrast,
  76. overwrite=overwrite, quiet=True)
  77. grass.run_command('g.remove', flags='f', type='vector',
  78. name=tmp_vect, quiet=True)
  79. def obtainCategories(vector, layer='1'):
  80. """This function returns a list of categories for all areas in
  81. the given layer"""
  82. vect_cats = []
  83. vc = grass.read_command('v.category', input=vector, layer=layer,
  84. option='print', type='centroid')
  85. for lc in vc.splitlines():
  86. for cat in lc.split('/'):
  87. vect_cats.append(int(cat))
  88. return sorted(set(vect_cats))
  89. def obtainAreaVector(outrast):
  90. """Create the string for configuration file"""
  91. reg = grass.region()
  92. return "MASKEDOVERLAYAREA {name}|{n}|{s}|{e}|{w}\n".format(name=outrast,
  93. n=reg['n'],
  94. s=reg['s'],
  95. e=reg['e'],
  96. w=reg['w'])
  97. def sampleAreaVector(vect, rast, vect_cats, layer='1', overwrite=False,
  98. progDialog=None):
  99. """Create the strings to add to the configuration file using vector"""
  100. areanum = len(vect_cats)
  101. output = []
  102. # TODO if areanum == 0 exit from the program
  103. if areanum == 0:
  104. GError(message=_("The polygon seems to have 0 areas"))
  105. return None
  106. for n in range(areanum):
  107. cat = str(vect_cats[n])
  108. outpref = "{rast}_{vect}_".format(vect=vect.split('@')[0],
  109. rast=rast.split('@')[0])
  110. rast_name = "{pref}{cat}".format(pref=outpref, cat=cat)
  111. # check if raster already axist
  112. if len(grass.list_strings('raster', pattern=rast_name,
  113. mapset='.')) == 1 and not overwrite:
  114. GError(message=_("The raster map <%s> already exists."
  115. " Please remove or rename the maps "
  116. "with the prefix '%s' or select the "
  117. "option to overwrite existing maps"
  118. % (rast_name, outpref)))
  119. return None
  120. convertFeature(vect, rast_name, cat, rast, layer, overwrite)
  121. output.append(obtainAreaVector(rast_name))
  122. if progDialog:
  123. progDialog.Update(n)
  124. return output