utils.py 3.9 KB

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