digit.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. """
  2. @package iclass.digit
  3. @brief wxIClass digitizer classes
  4. Classes:
  5. - digit::IClassVDigit
  6. - digit::IClassVDigitWindow
  7. (C) 2006-2012 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. @author Vaclav Petras <wenzeslaus gmail.com>
  12. @author Anna Kratochvilova <kratochanna gmail.com>
  13. """
  14. import wx
  15. from vdigit.mapwindow import VDigitWindow
  16. from vdigit.wxdigit import IVDigit
  17. from vdigit.wxdisplay import DisplayDriver, TYPE_AREA
  18. from core.gcmd import GWarning
  19. from core.utils import _
  20. try:
  21. from grass.lib.gis import G_verbose, G_set_verbose
  22. from grass.lib.vector import *
  23. from grass.lib.vedit import *
  24. except ImportError:
  25. pass
  26. import grass.script as grass
  27. class IClassVDigitWindow(VDigitWindow):
  28. """Class similar to VDigitWindow but specialized for wxIClass."""
  29. def __init__(self, parent, giface, map, properties):
  30. """
  31. @a parent should has toolbar providing current class (category).
  32. :param parent: gui parent
  33. :param map: map renderer instance
  34. """
  35. VDigitWindow.__init__(self, parent=parent, giface=giface,
  36. Map=map, properties=properties)
  37. def _onLeftDown(self, event):
  38. action = self.toolbar.GetAction()
  39. if not action:
  40. return
  41. region = grass.region()
  42. e, n = self.Pixel2Cell(event.GetPositionTuple())
  43. if not((region['s'] <= n <= region['n'])
  44. and(region['w'] <= e <= region['e'])):
  45. GWarning(
  46. parent=self.parent, message=_(
  47. "You are trying to create a training area "
  48. "outside the computational region. "
  49. "Please, use g.region to set the appropriate region first."))
  50. return
  51. cat = self.GetCurrentCategory()
  52. if cat is None and action == "addLine":
  53. dlg = wx.MessageDialog(
  54. parent=self.parent,
  55. message=_(
  56. "In order to create a training area, "
  57. "you have to select class first.\n\n"
  58. "There is no class yet, "
  59. "do you want to create one?"),
  60. caption=_("No class selected"),
  61. style=wx.YES_NO)
  62. if dlg.ShowModal() == wx.ID_YES:
  63. self.parent.OnCategoryManager(None)
  64. dlg.Destroy()
  65. event.Skip()
  66. return
  67. super(IClassVDigitWindow, self)._onLeftDown(event)
  68. def _addRecord(self):
  69. return False
  70. def _updateATM(self):
  71. pass
  72. def _onRightUp(self, event):
  73. super(IClassVDigitWindow, self)._onRightUp(event)
  74. self.parent.UpdateChangeState(changes=True)
  75. def GetCurrentCategory(self):
  76. """Returns current category (class).
  77. Category should be assigned to new features (areas).
  78. It is taken from parent's toolbar.
  79. """
  80. return self.parent.GetToolbar("iClass").GetSelectedCategoryIdx()
  81. def GetCategoryColor(self, cat):
  82. """Get color associated with given category"""
  83. r, g, b = map(int, self.parent.GetClassColor(cat).split(':'))
  84. return wx.Colour(r, g, b)
  85. class IClassVDigit(IVDigit):
  86. """Class similar to IVDigit but specialized for wxIClass."""
  87. def __init__(self, mapwindow):
  88. IVDigit.__init__(self, mapwindow, driver=IClassDisplayDriver)
  89. self._settings['closeBoundary'] = True # snap to the first node
  90. def _getNewFeaturesLayer(self):
  91. return 1
  92. def _getNewFeaturesCat(self):
  93. cat = self.mapWindow.GetCurrentCategory()
  94. return cat
  95. def DeleteAreasByCat(self, cats):
  96. """Delete areas (centroid+boundaries) by categories
  97. :param cats: list of categories
  98. """
  99. for cat in cats:
  100. Vedit_delete_areas_cat(self.poMapInfo, 1, cat)
  101. def CopyMap(self, name, tmp=False):
  102. """Make a copy of open vector map
  103. Note: Attributes are not copied
  104. :param name: name for a copy
  105. :param tmp: True for temporary map
  106. :return: number of copied features
  107. :return: -1 on error
  108. """
  109. if not self.poMapInfo:
  110. # nothing to copy
  111. return -1
  112. poMapInfoNew = pointer(Map_info())
  113. if not tmp:
  114. open_fn = Vect_open_new
  115. else:
  116. open_fn = Vect_open_tmp_new
  117. is3D = bool(Vect_is_3d(self.poMapInfo))
  118. if open_fn(poMapInfoNew, name, is3D) == -1:
  119. return -1
  120. verbose = G_verbose()
  121. G_set_verbose(-1) # be silent
  122. if Vect_copy_map_lines(self.poMapInfo, poMapInfoNew) == 1:
  123. G_set_verbose(verbose)
  124. return -1
  125. Vect_build(poMapInfoNew)
  126. G_set_verbose(verbose)
  127. ret = Vect_get_num_lines(poMapInfoNew)
  128. print os.listdir('/home/anna/grassdata/nc_spm_08_grass7/user1/.tmp/anna-laptop/vector')
  129. Vect_close(poMapInfoNew)
  130. print os.listdir('/home/anna/grassdata/nc_spm_08_grass7/user1/.tmp/anna-laptop/vector')
  131. return ret
  132. def GetMapInfo(self):
  133. """Returns Map_info() struct of open vector map"""
  134. return self.poMapInfo
  135. class IClassDisplayDriver(DisplayDriver):
  136. """Class similar to DisplayDriver but specialized for wxIClass
  137. .. todo::
  138. needs refactoring (glog, gprogress)
  139. """
  140. def __init__(self, device, deviceTmp, mapObj, window, glog, gprogress):
  141. DisplayDriver.__init__(
  142. self,
  143. device,
  144. deviceTmp,
  145. mapObj,
  146. window,
  147. glog,
  148. gprogress)
  149. self._cat = -1
  150. def _drawObject(self, robj):
  151. """Draw given object to the device
  152. :param robj: object to draw
  153. """
  154. if robj.type == TYPE_AREA:
  155. self._cat = Vect_get_area_cat(self.poMapInfo, robj.fid, 1)
  156. elif robj.type == TYPE_CENTROIDIN:
  157. return # skip centroids
  158. DisplayDriver._drawObject(self, robj)
  159. def _definePen(self, rtype):
  160. """Define pen/brush based on rendered object)
  161. :param rtype: type of the object
  162. :return: pen, brush
  163. """
  164. pen, brush = DisplayDriver._definePen(self, rtype)
  165. if self._cat > 0 and rtype == TYPE_AREA:
  166. brush = wx.Brush(self.window.GetCategoryColor(self._cat), wx.SOLID)
  167. return pen, brush
  168. def CloseMap(self):
  169. """Close training areas map - be quiet"""
  170. verbosity = G_verbose()
  171. G_set_verbose(0)
  172. DisplayDriver.CloseMap(self)
  173. G_set_verbose(verbosity)