history.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Jun 28 17:44:45 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import grass.lib.raster as libraster
  8. import datetime
  9. from grass.script.utils import encode
  10. from grass.pygrass.utils import decode
  11. class History(object):
  12. """History class help to manage all the metadata of a raster map"""
  13. def __init__(
  14. self,
  15. name,
  16. mapset="",
  17. mtype="",
  18. creator="",
  19. src1="",
  20. src2="",
  21. keyword="",
  22. date="",
  23. title="",
  24. ):
  25. self.c_hist = ctypes.pointer(libraster.History())
  26. # 'Tue Nov 7 01:11:23 2006'
  27. self.date_fmt = "%a %b %d %H:%M:%S %Y"
  28. self.name = name
  29. self.mapset = mapset
  30. self.mtype = mtype
  31. self.creator = creator
  32. self.src1 = src1
  33. self.src2 = src2
  34. self.keyword = keyword
  35. self.date = date
  36. self.title = title
  37. self.attrs = [
  38. "name",
  39. "mapset",
  40. "mtype",
  41. "creator",
  42. "src1",
  43. "src2",
  44. "keyword",
  45. "date",
  46. "title",
  47. ]
  48. def __repr__(self):
  49. return "History(%s)" % ", ".join(
  50. ["%s=%r" % (self.attr, getattr(self, attr)) for attr in self.attrs]
  51. )
  52. def __del__(self):
  53. """Rast_free_history"""
  54. pass
  55. def __eq__(self, hist):
  56. for attr in self.attrs:
  57. if getattr(self, attr) != getattr(hist, attr):
  58. return False
  59. return True
  60. def __len__(self):
  61. return self.length()
  62. def __iter__(self):
  63. return ((attr, getattr(self, attr)) for attr in self.attrs)
  64. # ----------------------------------------------------------------------
  65. # libraster.HIST_CREATOR
  66. def _get_creator(self):
  67. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_CREATOR))
  68. def _set_creator(self, creator):
  69. creator = encode(creator)
  70. return libraster.Rast_set_history(
  71. self.c_hist, libraster.HIST_CREATOR, ctypes.c_char_p(creator)
  72. )
  73. creator = property(
  74. fget=_get_creator, fset=_set_creator, doc="Set or obtain the creator of map"
  75. )
  76. # ----------------------------------------------------------------------
  77. # libraster.HIST_DATSRC_1
  78. def _get_src1(self):
  79. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_DATSRC_1))
  80. def _set_src1(self, src1):
  81. src1 = encode(src1)
  82. return libraster.Rast_set_history(
  83. self.c_hist, libraster.HIST_DATSRC_1, ctypes.c_char_p(src1)
  84. )
  85. src1 = property(
  86. fget=_get_src1, fset=_set_src1, doc="Set or obtain the first source of map"
  87. )
  88. # ----------------------------------------------------------------------
  89. # libraster.HIST_DATSRC_2
  90. def _get_src2(self):
  91. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_DATSRC_2))
  92. def _set_src2(self, src2):
  93. src2 = encode(src2)
  94. return libraster.Rast_set_history(
  95. self.c_hist, libraster.HIST_DATSRC_2, ctypes.c_char_p(src2)
  96. )
  97. src2 = property(
  98. fget=_get_src2, fset=_set_src2, doc="Set or obtain the second source of map"
  99. )
  100. # ----------------------------------------------------------------------
  101. # libraster.HIST_KEYWORD
  102. def _get_keyword(self):
  103. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_KEYWRD))
  104. def _set_keyword(self, keyword):
  105. keyword = encode(keyword)
  106. return libraster.Rast_set_history(
  107. self.c_hist, libraster.HIST_KEYWRD, ctypes.c_char_p(keyword)
  108. )
  109. keyword = property(
  110. fget=_get_keyword, fset=_set_keyword, doc="Set or obtain the keywords of map"
  111. )
  112. # ----------------------------------------------------------------------
  113. # libraster.HIST_MAPID
  114. def _get_date(self):
  115. date_str = decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_MAPID))
  116. if date_str:
  117. try:
  118. return datetime.datetime.strptime(date_str, self.date_fmt)
  119. except:
  120. return date_str
  121. def _set_date(self, datetimeobj):
  122. if datetimeobj:
  123. date_str = datetimeobj.strftime(self.date_fmt)
  124. date_str = encode(date_str)
  125. return libraster.Rast_set_history(
  126. self.c_hist, libraster.HIST_MAPID, ctypes.c_char_p(date_str)
  127. )
  128. date = property(fget=_get_date, fset=_set_date, doc="Set or obtain the date of map")
  129. # ----------------------------------------------------------------------
  130. # libraster.HIST_MAPSET
  131. def _get_mapset(self):
  132. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_MAPSET))
  133. def _set_mapset(self, mapset):
  134. mapset = encode(mapset)
  135. return libraster.Rast_set_history(
  136. self.c_hist, libraster.HIST_MAPSET, ctypes.c_char_p(mapset)
  137. )
  138. mapset = property(
  139. fget=_get_mapset, fset=_set_mapset, doc="Set or obtain the mapset of map"
  140. )
  141. # ----------------------------------------------------------------------
  142. # libraster.HIST_MAPTYPE
  143. def _get_maptype(self):
  144. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_MAPTYPE))
  145. def _set_maptype(self, maptype):
  146. maptype = encode(maptype)
  147. return libraster.Rast_set_history(
  148. self.c_hist, libraster.HIST_MAPTYPE, ctypes.c_char_p(maptype)
  149. )
  150. maptype = property(
  151. fget=_get_maptype, fset=_set_maptype, doc="Set or obtain the type of map"
  152. )
  153. # ----------------------------------------------------------------------
  154. # libraster.HIST_NUM_FIELDS
  155. #
  156. # Never used in any raster modules
  157. #
  158. # def _get_num_fields(self):
  159. # return libraster.Rast_get_history(self.c_hist,
  160. # libraster.HIST_NUM_FIELDS)
  161. #
  162. # def _set_num_fields(self, num_fields):
  163. # return libraster.Rast_set_history(self.c_hist,
  164. # libraster.HIST_NUM_FIELDS,
  165. # ctypes.c_char_p(num_fields))
  166. #
  167. # num_fields = property(fget = _get_num_fields, fset = _set_num_fields)
  168. # ----------------------------------------------------------------------
  169. # libraster.HIST_TITLE
  170. def _get_title(self):
  171. return decode(libraster.Rast_get_history(self.c_hist, libraster.HIST_TITLE))
  172. def _set_title(self, title):
  173. title = encode(title)
  174. return libraster.Rast_set_history(
  175. self.c_hist, libraster.HIST_TITLE, ctypes.c_char_p(title)
  176. )
  177. title = property(
  178. fget=_get_title, fset=_set_title, doc="Set or obtain the title of map"
  179. )
  180. def append(self, obj):
  181. """Rast_append_history"""
  182. libraster.Rast_append_history(self.c_hist, ctypes.c_char_p(str(obj)))
  183. def append_fmt(self, fmt, *args):
  184. """Rast_append_format_history"""
  185. libraster.Rast_append_format_history(self.c_hist, ctypes.c_char_p(fmt), *args)
  186. def clear(self):
  187. """Clear the history"""
  188. libraster.Rast_clear_history(self.c_hist)
  189. def command(self):
  190. """Rast_command_history"""
  191. libraster.Rast_command_history(self.c_hist)
  192. def format(self, field, fmt, *args):
  193. """Rast_format_history"""
  194. libraster.Rast_format_history(
  195. self.c_hist, ctypes.c_int(field), ctypes.c_char_p(fmt), *args
  196. )
  197. def length(self):
  198. """Rast_history_length"""
  199. return libraster.Rast_history_length(self.c_hist)
  200. def line(self, line):
  201. """Rast_history_line"""
  202. return libraster.Rast_history_line(self.c_hist, ctypes.c_int(line))
  203. def read(self):
  204. """Read the history of map, users need to use this function to
  205. obtain all the information of map. ::
  206. >>> import grass.lib.gis as libgis
  207. >>> import ctypes
  208. >>> import grass.lib.raster as libraster
  209. >>> hist = libraster.History()
  210. ..
  211. """
  212. libraster.Rast_read_history(self.name, self.mapset, self.c_hist)
  213. def write(self):
  214. """Rast_write_history"""
  215. libraster.Rast_write_history(self.name, self.c_hist)
  216. def short(self):
  217. """Rast_short_history"""
  218. libraster.Rast_short_history(self.name, "raster", self.c_hist)