history.py 8.2 KB

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