history.py 8.9 KB

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