utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 getRegisteredMaps(timeseries, etype):
  76. """!Returns list of maps registered in dataset"""
  77. timeseriesMaps = []
  78. if etype == 'strds':
  79. sp = tgis.SpaceTimeRasterDataset(ident=timeseries)
  80. elif etype == 'stvds':
  81. sp = tgis.SpaceTimeVectorDataset(ident=timeseries)
  82. if sp.is_in_db() == False:
  83. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  84. sp.select()
  85. rows = sp.get_registered_maps(columns="id", where=None, order="start_time", dbif=None)
  86. timeseriesMaps = []
  87. if rows:
  88. for row in rows:
  89. timeseriesMaps.append(row["id"])
  90. return timeseriesMaps
  91. def ComputeScaledRect(sourceSize, destSize):
  92. """!Fits source rectangle into destination rectangle
  93. by scaling and centering.
  94. @code
  95. >>> ComputeScaledRect(sourceSize = (10, 40), destSize = (100, 50))
  96. {'height': 50, 'scale': 1.25, 'width': 13, 'x': 44, 'y': 0}
  97. @endcode
  98. @param sourceSize size of source rectangle
  99. @param destSize size of destination rectangle
  100. """
  101. ratio1 = destSize[0] / float(sourceSize[0])
  102. ratio2 = destSize[1] / float(sourceSize[1])
  103. if ratio1 < ratio2:
  104. scale = ratio1
  105. width = int(sourceSize[0] * scale + 0.5)
  106. height = int(sourceSize[1] * scale + 0.5)
  107. x = 0
  108. y = int((destSize[1] - height) / 2. + 0.5)
  109. else:
  110. scale = ratio2
  111. width = int(sourceSize[0] * scale + 0.5)
  112. height = int(sourceSize[1] * scale + 0.5)
  113. y = 0
  114. x = int((destSize[0] - width) / 2. + 0.5)
  115. return {'width': width, 'height': height, 'x': x, 'y': y, 'scale': scale}
  116. def RenderText(text, font):
  117. """!Renderes text with given font to bitmap."""
  118. dc = wx.MemoryDC()
  119. dc.SetFont(font)
  120. w, h = dc.GetTextExtent(text)
  121. bmp = wx.EmptyBitmap(w + 2, h + 2)
  122. dc.SelectObject(bmp)
  123. dc.SetBrush(wx.TRANSPARENT_BRUSH)
  124. dc.SetBackgroundMode(wx.TRANSPARENT)
  125. dc.Clear()
  126. dc.DrawText(text, 1, 1)
  127. dc.SelectObject(wx.NullBitmap)
  128. return bmp
  129. def WxImageToPil(image):
  130. """!Converts wx.Image to PIL image"""
  131. pilImage = Image.new( 'RGB', (image.GetWidth(), image.GetHeight()) )
  132. pilImage.fromstring( image.GetData() )
  133. return pilImage