digit.py 6.3 KB

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