digit.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, update=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. :param bool update: True if copy target vector map (poMapInfoNew)
  106. exist
  107. :return: number of copied features
  108. :return: -1 on error
  109. """
  110. if not self.poMapInfo:
  111. # nothing to copy
  112. return -1
  113. poMapInfoNew = pointer(Map_info())
  114. if not tmp:
  115. if update:
  116. open_fn = Vect_open_update
  117. else:
  118. open_fn = Vect_open_new
  119. else:
  120. if update:
  121. open_fn = Vect_open_tmp_update
  122. else:
  123. open_fn = Vect_open_tmp_new
  124. if update:
  125. if open_fn(poMapInfoNew, name, '') == -1:
  126. return -1
  127. else:
  128. is3D = bool(Vect_is_3d(self.poMapInfo))
  129. if open_fn(poMapInfoNew, name, is3D) == -1:
  130. return -1
  131. verbose = G_verbose()
  132. G_set_verbose(-1) # be silent
  133. if Vect_copy_map_lines(self.poMapInfo, poMapInfoNew) == 1:
  134. G_set_verbose(verbose)
  135. return -1
  136. Vect_build(poMapInfoNew)
  137. G_set_verbose(verbose)
  138. ret = Vect_get_num_lines(poMapInfoNew)
  139. Vect_close(poMapInfoNew)
  140. return ret
  141. def GetMapInfo(self):
  142. """Returns Map_info() struct of open vector map"""
  143. return self.poMapInfo
  144. class IClassDisplayDriver(DisplayDriver):
  145. """Class similar to DisplayDriver but specialized for wxIClass
  146. .. todo::
  147. needs refactoring (glog, gprogress)
  148. """
  149. def __init__(self, device, deviceTmp, mapObj, window, glog, gprogress):
  150. DisplayDriver.__init__(
  151. self,
  152. device,
  153. deviceTmp,
  154. mapObj,
  155. window,
  156. glog,
  157. gprogress)
  158. self._cat = -1
  159. def _drawObject(self, robj):
  160. """Draw given object to the device
  161. :param robj: object to draw
  162. """
  163. if robj.type == TYPE_AREA:
  164. self._cat = Vect_get_area_cat(self.poMapInfo, robj.fid, 1)
  165. elif robj.type == TYPE_CENTROIDIN:
  166. return # skip centroids
  167. DisplayDriver._drawObject(self, robj)
  168. def _definePen(self, rtype):
  169. """Define pen/brush based on rendered object)
  170. :param rtype: type of the object
  171. :return: pen, brush
  172. """
  173. pen, brush = DisplayDriver._definePen(self, rtype)
  174. if self._cat > 0 and rtype == TYPE_AREA:
  175. brush = wx.Brush(self.window.GetCategoryColor(self._cat), wx.SOLID)
  176. return pen, brush
  177. def CloseMap(self):
  178. """Close training areas map - be quiet"""
  179. verbosity = G_verbose()
  180. G_set_verbose(0)
  181. DisplayDriver.CloseMap(self)
  182. G_set_verbose(verbosity)