functions.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = "GRASS8"
  43. grass_config_dir = os.path.join(os.getenv("APPDATA"), grass_config_dirname)
  44. else:
  45. grass_config_dirname = ".grass8"
  46. grass_config_dir = os.path.join(os.getenv("HOME"), grass_config_dirname)
  47. rlipath = os.path.join(grass_config_dir, "r.li")
  48. if os.path.exists(rlipath):
  49. return rlipath
  50. else:
  51. os.mkdir(rlipath)
  52. return rlipath
  53. def checkMapExists(name, typ="raster"):
  54. """Check if a map already exist in the working mapset"""
  55. env = grass.gisenv()
  56. mapset = env["MAPSET"]
  57. mapp = grass.find_file(name, typ, mapset)
  58. if mapp.name != "":
  59. return True
  60. else:
  61. return False
  62. def convertFeature(vect, outrast, cat, origrast, layer="1", overwrite=False):
  63. """Convert a single feature to a raster"""
  64. tmp_vect = "tmp_{rast}".format(rast=outrast)
  65. grass.run_command(
  66. "v.extract",
  67. input=vect,
  68. cats=cat,
  69. type="area",
  70. layer=layer,
  71. output=tmp_vect,
  72. flags="d",
  73. overwrite=overwrite,
  74. quiet=True,
  75. )
  76. grass.run_command("g.region", raster=origrast)
  77. grass.run_command("g.region", vector=tmp_vect)
  78. grass.run_command("g.region", align=origrast)
  79. grass.run_command(
  80. "v.to.rast",
  81. input=tmp_vect,
  82. type="area",
  83. layer=layer,
  84. use="val",
  85. value=cat,
  86. output=outrast,
  87. overwrite=overwrite,
  88. quiet=True,
  89. )
  90. grass.run_command("g.remove", flags="f", type="vector", name=tmp_vect, quiet=True)
  91. def obtainCategories(vector, layer="1"):
  92. """This function returns a list of categories for all areas in
  93. the given layer"""
  94. vect_cats = []
  95. vc = grass.read_command(
  96. "v.category", input=vector, layer=layer, option="print", type="centroid"
  97. )
  98. for lc in vc.splitlines():
  99. for cat in lc.split("/"):
  100. vect_cats.append(int(cat))
  101. return sorted(set(vect_cats))
  102. def obtainAreaVector(outrast):
  103. """Create the string for configuration file"""
  104. reg = grass.region()
  105. return "MASKEDOVERLAYAREA {name}|{n}|{s}|{e}|{w}\n".format(
  106. name=outrast, n=reg["n"], s=reg["s"], e=reg["e"], w=reg["w"]
  107. )
  108. def sampleAreaVector(
  109. vect, rast, vect_cats, layer="1", overwrite=False, progDialog=None
  110. ):
  111. """Create the strings to add to the configuration file using vector"""
  112. areanum = len(vect_cats)
  113. output = []
  114. # TODO if areanum == 0 exit from the program
  115. if areanum == 0:
  116. GError(message=_("The polygon seems to have 0 areas"))
  117. return None
  118. for n in range(areanum):
  119. cat = str(vect_cats[n])
  120. outpref = "{rast}_{vect}_".format(
  121. vect=vect.split("@")[0], rast=rast.split("@")[0]
  122. )
  123. rast_name = "{pref}{cat}".format(pref=outpref, cat=cat)
  124. # check if raster already axist
  125. if (
  126. len(grass.list_strings("raster", pattern=rast_name, mapset=".")) == 1
  127. and not overwrite
  128. ):
  129. GError(
  130. message=_(
  131. "The raster map <%s> already exists."
  132. " Please remove or rename the maps "
  133. "with the prefix '%s' or select the "
  134. "option to overwrite existing maps" % (rast_name, outpref)
  135. )
  136. )
  137. return None
  138. convertFeature(vect, rast_name, cat, rast, layer, overwrite)
  139. output.append(obtainAreaVector(rast_name))
  140. if progDialog:
  141. progDialog.Update(n)
  142. return output