utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. try:
  16. from PIL import Image
  17. hasPIL = True
  18. except ImportError:
  19. hasPIL = False
  20. import grass.temporal as tgis
  21. import grass.script as grass
  22. from core.gcmd import GException
  23. from core.utils import _
  24. class TemporalMode:
  25. TEMPORAL = 1
  26. NONTEMPORAL = 2
  27. class TemporalType:
  28. ABSOLUTE = 1
  29. RELATIVE = 2
  30. class Orientation:
  31. FORWARD = 1
  32. BACKWARD = 2
  33. class ReplayMode:
  34. ONESHOT = 1
  35. REVERSE = 2
  36. REPEAT = 3
  37. def validateTimeseriesName(timeseries, etype = 'strds'):
  38. """!Checks if space time dataset exists and completes missing mapset.
  39. Raises GException if dataset doesn't exist.
  40. """
  41. trastDict = tgis.tlist_grouped(etype)
  42. if timeseries.find("@") >= 0:
  43. nameShort, mapset = timeseries.split('@', 1)
  44. if nameShort in trastDict[mapset]:
  45. return timeseries
  46. else:
  47. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  48. for mapset, names in trastDict.iteritems():
  49. if timeseries in names:
  50. return timeseries + "@" + mapset
  51. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  52. def validateMapNames(names, etype):
  53. """!Checks if maps exist and completes missing mapset.
  54. Input is list of map names.
  55. Raises GException if map doesn't exist.
  56. """
  57. mapDict = grass.list_grouped(etype)
  58. newNames = []
  59. for name in names:
  60. if name.find("@") >= 0:
  61. nameShort, mapset = name.split('@', 1)
  62. if nameShort in mapDict[mapset]:
  63. newNames.append(name)
  64. else:
  65. raise GException(_("Map <%s> not found.") % name)
  66. else:
  67. found = False
  68. for mapset, mapNames in mapDict.iteritems():
  69. if name in mapNames:
  70. found = True
  71. newNames.append(name + "@" + mapset)
  72. if not found:
  73. raise GException(_("Map <%s> not found.") % name)
  74. return newNames
  75. def ComputeScaledRect(sourceSize, destSize):
  76. """!Fits source rectangle into destination rectangle
  77. by scaling and centering.
  78. @code
  79. >>> ComputeScaledRect(sourceSize = (10, 40), destSize = (100, 50))
  80. {'height': 50, 'scale': 1.25, 'width': 13, 'x': 44, 'y': 0}
  81. @endcode
  82. @param sourceSize size of source rectangle
  83. @param destSize size of destination rectangle
  84. """
  85. ratio1 = destSize[0] / float(sourceSize[0])
  86. ratio2 = destSize[1] / float(sourceSize[1])
  87. if ratio1 < ratio2:
  88. scale = ratio1
  89. width = int(sourceSize[0] * scale + 0.5)
  90. height = int(sourceSize[1] * scale + 0.5)
  91. x = 0
  92. y = int((destSize[1] - height) / 2. + 0.5)
  93. else:
  94. scale = ratio2
  95. width = int(sourceSize[0] * scale + 0.5)
  96. height = int(sourceSize[1] * scale + 0.5)
  97. y = 0
  98. x = int((destSize[0] - width) / 2. + 0.5)
  99. return {'width': width, 'height': height, 'x': x, 'y': y, 'scale': scale}
  100. def RenderText(text, font):
  101. """!Renderes text with given font to bitmap."""
  102. dc = wx.MemoryDC()
  103. dc.SetFont(font)
  104. w, h = dc.GetTextExtent(text)
  105. bmp = wx.EmptyBitmap(w + 2, h + 2)
  106. dc.SelectObject(bmp)
  107. dc.SetBrush(wx.TRANSPARENT_BRUSH)
  108. dc.SetBackgroundMode(wx.TRANSPARENT)
  109. dc.Clear()
  110. dc.DrawText(text, 1, 1)
  111. dc.SelectObject(wx.NullBitmap)
  112. return bmp
  113. def WxImageToPil(image):
  114. """!Converts wx.Image to PIL image"""
  115. pilImage = Image.new( 'RGB', (image.GetWidth(), image.GetHeight()) )
  116. pilImage.fromstring( image.GetData() )
  117. return pilImage