digit.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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']) and (region['w'] <= e <= region['e'])):
  44. GWarning(parent = self.parent,
  45. message = _("You are trying to create a training area "
  46. "outside the computational region. "
  47. "Please, use g.region to set the appropriate region first."))
  48. return
  49. cat = self.GetCurrentCategory()
  50. if cat is None and action == "addLine":
  51. dlg = wx.MessageDialog(parent = self.parent,
  52. message = _("In order to create a training area, "
  53. "you have to select class first.\n\n"
  54. "There is no class yet, "
  55. "do you want to create one?"),
  56. caption = _("No class selected"),
  57. style = wx.YES_NO)
  58. if dlg.ShowModal() == wx.ID_YES:
  59. self.parent.OnCategoryManager(None)
  60. dlg.Destroy()
  61. event.Skip()
  62. return
  63. super(IClassVDigitWindow, self)._onLeftDown(event)
  64. def _addRecord(self):
  65. return False
  66. def _updateATM(self):
  67. pass
  68. def _onRightUp(self, event):
  69. super(IClassVDigitWindow, self)._onRightUp(event)
  70. self.parent.UpdateChangeState(changes = True)
  71. def GetCurrentCategory(self):
  72. """Returns current category (class).
  73. Category should be assigned to new features (areas).
  74. It is taken from parent's toolbar.
  75. """
  76. return self.parent.GetToolbar("iClass").GetSelectedCategoryIdx()
  77. def GetCategoryColor(self, cat):
  78. """Get color associated with given category"""
  79. r, g, b = map(int, self.parent.GetClassColor(cat).split(':'))
  80. return wx.Colour(r, g, b)
  81. class IClassVDigit(IVDigit):
  82. """Class similar to IVDigit but specialized for wxIClass."""
  83. def __init__(self, mapwindow):
  84. IVDigit.__init__(self, mapwindow, driver = IClassDisplayDriver)
  85. self._settings['closeBoundary'] = True # snap to the first node
  86. def _getNewFeaturesLayer(self):
  87. return 1
  88. def _getNewFeaturesCat(self):
  89. cat = self.mapWindow.GetCurrentCategory()
  90. return cat
  91. def DeleteAreasByCat(self, cats):
  92. """Delete areas (centroid+boundaries) by categories
  93. :param cats: list of categories
  94. """
  95. for cat in cats:
  96. Vedit_delete_areas_cat(self.poMapInfo, 1, cat)
  97. def CopyMap(self, name, tmp = False):
  98. """Make a copy of open vector map
  99. Note: Attributes are not copied
  100. :param name: name for a copy
  101. :param tmp: True for temporary map
  102. :return: number of copied features
  103. :return: -1 on error
  104. """
  105. if not self.poMapInfo:
  106. # nothing to copy
  107. return -1
  108. poMapInfoNew = pointer(Map_info())
  109. if not tmp:
  110. open_fn = Vect_open_new
  111. else:
  112. open_fn = Vect_open_tmp_new
  113. is3D = bool(Vect_is_3d(self.poMapInfo))
  114. if open_fn(poMapInfoNew, name, is3D) == -1:
  115. return -1
  116. verbose = G_verbose()
  117. G_set_verbose(-1) # be silent
  118. if Vect_copy_map_lines(self.poMapInfo, poMapInfoNew) == 1:
  119. G_set_verbose(verbose)
  120. return -1
  121. Vect_build(poMapInfoNew)
  122. G_set_verbose(verbose)
  123. ret = Vect_get_num_lines(poMapInfoNew)
  124. Vect_close(poMapInfoNew)
  125. return ret
  126. def GetMapInfo(self):
  127. """Returns Map_info() struct of open vector map"""
  128. return self.poMapInfo
  129. class IClassDisplayDriver(DisplayDriver):
  130. """Class similar to DisplayDriver but specialized for wxIClass
  131. .. todo::
  132. needs refactoring (glog, gprogress)
  133. """
  134. def __init__(self, device, deviceTmp, mapObj, window, glog, gprogress):
  135. DisplayDriver.__init__(self, device, deviceTmp, mapObj, window, glog, gprogress)
  136. self._cat = -1
  137. def _drawObject(self, robj):
  138. """Draw given object to the device
  139. :param robj: object to draw
  140. """
  141. if robj.type == TYPE_AREA:
  142. self._cat = Vect_get_area_cat(self.poMapInfo, robj.fid, 1)
  143. elif robj.type == TYPE_CENTROIDIN:
  144. return # skip centroids
  145. DisplayDriver._drawObject(self, robj)
  146. def _definePen(self, rtype):
  147. """Define pen/brush based on rendered object)
  148. :param rtype: type of the object
  149. :return: pen, brush
  150. """
  151. pen, brush = DisplayDriver._definePen(self, rtype)
  152. if self._cat > 0 and rtype == TYPE_AREA:
  153. brush = wx.Brush(self.window.GetCategoryColor(self._cat), wx.SOLID)
  154. return pen, brush
  155. def CloseMap(self):
  156. """Close training areas map - be quiet"""
  157. verbosity = G_verbose()
  158. G_set_verbose(0)
  159. DisplayDriver.CloseMap(self)
  160. G_set_verbose(verbosity)