utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """!
  2. @package animation.utils
  3. @brief Miscellaneous functions and enum classes
  4. Classes:
  5. - utils::TemporalMode
  6. - utils::TemporalType
  7. - utils::Orientation
  8. - utils::ReplayMode
  9. (C) 2012 by the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. @author Anna Kratochvilova <kratochanna gmail.com>
  13. """
  14. import wx
  15. import grass.temporal as tgis
  16. import grass.script as grass
  17. from core.gcmd import GException
  18. class TemporalMode:
  19. TEMPORAL = 1
  20. NONTEMPORAL = 2
  21. class TemporalType:
  22. ABSOLUTE = 1
  23. RELATIVE = 2
  24. class Orientation:
  25. FORWARD = 1
  26. BACKWARD = 2
  27. class ReplayMode:
  28. ONESHOT = 1
  29. REVERSE = 2
  30. REPEAT = 3
  31. def validateTimeseriesName(timeseries, etype = 'strds'):
  32. """!Checks if space time dataset exists and completes missing mapset.
  33. Raises GException if dataset doesn't exist.
  34. """
  35. trastDict = tgis.tlist_grouped(etype)
  36. if timeseries.find("@") >= 0:
  37. nameShort, mapset = timeseries.split('@', 1)
  38. if nameShort in trastDict[mapset]:
  39. return timeseries
  40. else:
  41. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  42. for mapset, names in trastDict.iteritems():
  43. if timeseries in names:
  44. return timeseries + "@" + mapset
  45. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  46. def validateMapNames(names, etype):
  47. """!Checks if maps exist and completes missing mapset.
  48. Input is list of map names.
  49. Raises GException if map doesn't exist.
  50. """
  51. mapDict = grass.list_grouped(etype)
  52. newNames = []
  53. for name in names:
  54. if name.find("@") >= 0:
  55. nameShort, mapset = name.split('@', 1)
  56. if nameShort in mapDict[mapset]:
  57. newNames.append(name)
  58. else:
  59. raise GException(_("Map <%s> not found.") % name)
  60. else:
  61. found = False
  62. for mapset, mapNames in mapDict.iteritems():
  63. if name in mapNames:
  64. found = True
  65. newNames.append(name + "@" + mapset)
  66. if not found:
  67. raise GException(_("Map <%s> not found.") % name)
  68. return newNames
  69. def ComputeScaledRect(sourceSize, destSize):
  70. """!Fits source rectangle into destination rectangle
  71. by scaling and centering.
  72. @code
  73. >>> ComputeScaledRect(sourceSize = (10, 40), destSize = (100, 50))
  74. {'height': 50, 'scale': 1.25, 'width': 13, 'x': 44, 'y': 0}
  75. @endcode
  76. @param sourceSize size of source rectangle
  77. @param destSize size of destination rectangle
  78. """
  79. ratio1 = destSize[0] / float(sourceSize[0])
  80. ratio2 = destSize[1] / float(sourceSize[1])
  81. if ratio1 < ratio2:
  82. scale = ratio1
  83. width = int(sourceSize[0] * scale + 0.5)
  84. height = int(sourceSize[1] * scale + 0.5)
  85. x = 0
  86. y = int((destSize[1] - height) / 2. + 0.5)
  87. else:
  88. scale = ratio2
  89. width = int(sourceSize[0] * scale + 0.5)
  90. height = int(sourceSize[1] * scale + 0.5)
  91. y = 0
  92. x = int((destSize[0] - width) / 2. + 0.5)
  93. return {'width': width, 'height': height, 'x': x, 'y': y, 'scale': scale}
  94. def RenderText(text, font):
  95. """!Renderes text with given font to bitmap."""
  96. dc = wx.MemoryDC()
  97. dc.SetFont(font)
  98. w, h = dc.GetTextExtent(text)
  99. bmp = wx.EmptyBitmap(w + 2, h + 2)
  100. dc.SelectObject(bmp)
  101. dc.SetBrush(wx.TRANSPARENT_BRUSH)
  102. dc.SetBackgroundMode(wx.TRANSPARENT)
  103. dc.Clear()
  104. dc.DrawText(text, 1, 1)
  105. dc.SelectObject(wx.NullBitmap)
  106. return bmp