utils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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) 2013 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 Perasova <kratochanna gmail.com>
  13. """
  14. import os
  15. import wx
  16. import hashlib
  17. import six
  18. from multiprocessing import cpu_count
  19. try:
  20. from PIL import Image
  21. hasPIL = True
  22. except ImportError:
  23. hasPIL = False
  24. import grass.temporal as tgis
  25. import grass.script as grass
  26. from grass.script.utils import encode
  27. from gui_core.wrap import EmptyBitmap
  28. from core.gcmd import GException
  29. class TemporalMode:
  30. TEMPORAL = 1
  31. NONTEMPORAL = 2
  32. class TemporalType:
  33. ABSOLUTE = 1
  34. RELATIVE = 2
  35. class Orientation:
  36. FORWARD = 1
  37. BACKWARD = 2
  38. class ReplayMode:
  39. ONESHOT = 1
  40. REVERSE = 2
  41. REPEAT = 3
  42. def validateTimeseriesName(timeseries, etype="strds"):
  43. """Checks if space time dataset exists and completes missing mapset.
  44. Raises GException if dataset doesn't exist.
  45. """
  46. trastDict = tgis.tlist_grouped(etype)
  47. if timeseries.find("@") >= 0:
  48. nameShort, mapset = timeseries.split("@", 1)
  49. if nameShort in trastDict[mapset]:
  50. return timeseries
  51. else:
  52. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  53. mapsets = tgis.get_tgis_c_library_interface().available_mapsets()
  54. for mapset in mapsets:
  55. if mapset in trastDict.keys():
  56. if timeseries in trastDict[mapset]:
  57. return timeseries + "@" + mapset
  58. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  59. def validateMapNames(names, etype):
  60. """Checks if maps exist and completes missing mapset.
  61. Input is list of map names.
  62. Raises GException if map doesn't exist.
  63. """
  64. mapDict = grass.list_grouped(etype)
  65. newNames = []
  66. for name in names:
  67. if name.find("@") >= 0:
  68. nameShort, mapset = name.split("@", 1)
  69. if nameShort in mapDict[mapset]:
  70. newNames.append(name)
  71. else:
  72. raise GException(_("Map <%s> not found.") % name)
  73. else:
  74. found = False
  75. for mapset, mapNames in six.iteritems(mapDict):
  76. if name in mapNames:
  77. found = True
  78. newNames.append(name + "@" + mapset)
  79. if not found:
  80. raise GException(_("Map <%s> not found.") % name)
  81. return newNames
  82. def getRegisteredMaps(timeseries, etype):
  83. """Returns list of maps registered in dataset.
  84. Can throw ScriptError if the dataset doesn't exist.
  85. """
  86. timeseriesMaps = []
  87. sp = tgis.open_old_stds(timeseries, etype)
  88. rows = sp.get_registered_maps(columns="id", where=None, order="start_time")
  89. timeseriesMaps = []
  90. if rows:
  91. for row in rows:
  92. timeseriesMaps.append(row["id"])
  93. return timeseriesMaps
  94. def getNameAndLayer(name):
  95. """Checks whether map name contains layer
  96. and returns map name with mapset (when there was mapset)
  97. and layer (can be None).
  98. >>> getNameAndLayer('name:2@mapset')
  99. ('name@mapset', '2')
  100. >>> getNameAndLayer('name@mapset')
  101. ('name@mapset', None)
  102. >>> getNameAndLayer('name:2')
  103. ('name', '2')
  104. """
  105. mapset = layer = None
  106. if "@" in name:
  107. name, mapset = name.split("@")
  108. if ":" in name:
  109. name, layer = name.split(":")
  110. if mapset:
  111. name = name + "@" + mapset
  112. return name, layer
  113. def checkSeriesCompatibility(mapSeriesList=None, timeseriesList=None):
  114. """Checks whether time series (map series and stds) are compatible,
  115. which means they have equal number of maps ad times (in case of stds).
  116. This is needed within layer list, not within the entire animation tool.
  117. Throws GException if these are incompatible.
  118. :return: number of maps for animation
  119. """
  120. timeseriesInfo = {
  121. "count": set(),
  122. "temporalType": set(),
  123. "mapType": set(),
  124. "mapTimes": set(),
  125. }
  126. if timeseriesList:
  127. for stds, etype in timeseriesList:
  128. sp = tgis.open_old_stds(stds, etype)
  129. mapType = sp.get_map_time() # interval, ...
  130. tempType = sp.get_initial_values()[0] # absolute
  131. timeseriesInfo["mapType"].add(mapType)
  132. timeseriesInfo["temporalType"].add(tempType)
  133. rows = sp.get_registered_maps_as_objects(where=None, order="start_time")
  134. if rows:
  135. times = []
  136. timeseriesInfo["count"].add(len(rows))
  137. for row in rows:
  138. if tempType == "absolute":
  139. time = row.get_absolute_time()
  140. else:
  141. time = row.get_relative_time()
  142. times.append(time)
  143. timeseriesInfo["mapTimes"].add(tuple(times))
  144. else:
  145. timeseriesInfo["mapTimes"].add(None)
  146. timeseriesInfo["count"].add(None)
  147. if len(timeseriesInfo["count"]) > 1:
  148. raise GException(
  149. _("The number of maps in space-time datasets " "has to be the same.")
  150. )
  151. if len(timeseriesInfo["temporalType"]) > 1:
  152. raise GException(
  153. _(
  154. "The temporal type (absolute/relative) of space-time datasets "
  155. "has to be the same."
  156. )
  157. )
  158. if len(timeseriesInfo["mapType"]) > 1:
  159. raise GException(
  160. _(
  161. "The map type (point/interval) of space-time datasets "
  162. "has to be the same."
  163. )
  164. )
  165. if len(timeseriesInfo["mapTimes"]) > 1:
  166. raise GException(
  167. _(
  168. "The temporal extents of maps in space-time datasets "
  169. "have to be the same."
  170. )
  171. )
  172. if mapSeriesList:
  173. count = set()
  174. for mapSeries in mapSeriesList:
  175. count.add(len(mapSeries))
  176. if len(count) > 1:
  177. raise GException(
  178. _(
  179. "The number of maps to animate has to be "
  180. "the same for each map series."
  181. )
  182. )
  183. if timeseriesList and list(count)[0] != list(timeseriesInfo["count"])[0]:
  184. raise GException(
  185. _(
  186. "The number of maps to animate has to be "
  187. "the same as the number of maps in temporal dataset."
  188. )
  189. )
  190. if mapSeriesList:
  191. return list(count)[0]
  192. if timeseriesList:
  193. return list(timeseriesInfo["count"])[0]
  194. def ComputeScaledRect(sourceSize, destSize):
  195. """Fits source rectangle into destination rectangle
  196. by scaling and centering.
  197. >>> ComputeScaledRect(sourceSize = (10, 40), destSize = (100, 50))
  198. {'height': 50, 'scale': 1.25, 'width': 13, 'x': 44, 'y': 0}
  199. :param sourceSize: size of source rectangle
  200. :param destSize: size of destination rectangle
  201. """
  202. ratio1 = destSize[0] / float(sourceSize[0])
  203. ratio2 = destSize[1] / float(sourceSize[1])
  204. if ratio1 < ratio2:
  205. scale = ratio1
  206. width = int(sourceSize[0] * scale + 0.5)
  207. height = int(sourceSize[1] * scale + 0.5)
  208. x = 0
  209. y = int((destSize[1] - height) / 2.0 + 0.5)
  210. else:
  211. scale = ratio2
  212. width = int(sourceSize[0] * scale + 0.5)
  213. height = int(sourceSize[1] * scale + 0.5)
  214. y = 0
  215. x = int((destSize[0] - width) / 2.0 + 0.5)
  216. return {"width": width, "height": height, "x": x, "y": y, "scale": scale}
  217. def RenderText(text, font, bgcolor, fgcolor):
  218. """Renders text with given font to bitmap."""
  219. dc = wx.MemoryDC(EmptyBitmap(20, 20))
  220. dc.SetFont(font)
  221. w, h = dc.GetTextExtent(text)
  222. bmp = EmptyBitmap(w + 2, h + 2)
  223. dc.SelectObject(bmp)
  224. dc.SetBackgroundMode(wx.SOLID)
  225. dc.SetTextBackground(wx.Colour(*bgcolor))
  226. dc.SetTextForeground(wx.Colour(*fgcolor))
  227. dc.Clear()
  228. dc.DrawText(text, 1, 1)
  229. dc.SelectObject(wx.NullBitmap)
  230. return bmp
  231. def WxImageToPil(image):
  232. """Converts wx.Image to PIL image"""
  233. pilImage = Image.new("RGB", (image.GetWidth(), image.GetHeight()))
  234. pilImage.frombytes(bytes(image.GetData()))
  235. return pilImage
  236. def HashCmd(cmd, region):
  237. """Returns a hash from command given as a list and a region as a dict."""
  238. name = "_".join(cmd)
  239. if region:
  240. name += str(sorted(region.items()))
  241. return hashlib.sha1(encode(name)).hexdigest()
  242. def HashCmds(cmds, region):
  243. """Returns a hash from list of commands and regions as dicts."""
  244. name = ";".join([item for sublist in cmds for item in sublist])
  245. if region:
  246. name += str(sorted(region.items()))
  247. return hashlib.sha1(encode(name)).hexdigest()
  248. def GetFileFromCmd(dirname, cmd, region, extension="ppm"):
  249. """Returns file path created as a hash from command and region."""
  250. return os.path.join(dirname, HashCmd(cmd, region) + "." + extension)
  251. def GetFileFromCmds(dirname, cmds, region, extension="ppm"):
  252. """Returns file path created as a hash from list of commands and regions."""
  253. return os.path.join(dirname, HashCmds(cmds, region) + "." + extension)
  254. def layerListToCmdsMatrix(layerList):
  255. """Goes thru layerList and create matrix of commands
  256. for the composition of map series.:
  257. :return: matrix of cmds for composition
  258. """
  259. count = 0
  260. for layer in layerList:
  261. if layer.active and hasattr(layer, "maps"):
  262. # assuming the consistency of map number is checked already
  263. count = len(layer.maps)
  264. break
  265. cmdsForComposition = []
  266. for layer in layerList:
  267. if not layer.active:
  268. continue
  269. if hasattr(layer, "maps"):
  270. for i, part in enumerate(layer.cmd):
  271. if part.startswith("map="):
  272. cmd = layer.cmd[:]
  273. cmds = []
  274. for map_ in layer.maps:
  275. # check if dataset uses layers instead of maps
  276. mapName, mapLayer = getNameAndLayer(map_)
  277. cmd[i] = "map={name}".format(name=mapName)
  278. if mapLayer:
  279. try:
  280. idx = cmd.index("layer")
  281. cmd[idx] = "layer={layer}".format(layer=mapLayer)
  282. except ValueError:
  283. cmd.append("layer={layer}".format(layer=mapLayer))
  284. cmds.append(cmd[:])
  285. cmdsForComposition.append(cmds)
  286. else:
  287. cmdsForComposition.append([layer.cmd] * count)
  288. return list(zip(*cmdsForComposition))
  289. def sampleCmdMatrixAndCreateNames(cmdMatrix, sampledSeries, regions):
  290. """Applies information from temporal sampling
  291. to the command matrix."""
  292. namesList = []
  293. j = -1
  294. lastName = ""
  295. for name in sampledSeries:
  296. if name is not None:
  297. if lastName != name:
  298. lastName = name
  299. j += 1
  300. namesList.append(HashCmds(cmdMatrix[j], regions[j]))
  301. else:
  302. namesList.append(None)
  303. assert j == len(cmdMatrix) - 1
  304. return namesList
  305. def getCpuCount():
  306. """Returns number of available cpus.
  307. If fails, default (4) is returned.
  308. """
  309. try:
  310. return cpu_count()
  311. except NotImplementedError:
  312. return 4
  313. def interpolate(start, end, count):
  314. """Interpolates values between start and end.
  315. :param start: start value (float)
  316. :param end: end value (float)
  317. :param count: total number of values including start and end
  318. >>> interpolate(0, 10, 5)
  319. [0, 2.5, 5.0, 7.5, 10]
  320. >>> interpolate(10, 0, 5)
  321. [10, 7.5, 5.0, 2.5, 0]
  322. """
  323. step = (end - start) / float(count - 1)
  324. values = []
  325. if start < end:
  326. while start < end:
  327. values.append(start)
  328. start += step
  329. elif end < start:
  330. while end < start:
  331. values.append(start)
  332. start += step
  333. else:
  334. values = [start] * (count - 1)
  335. values.append(end)
  336. return values