history.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. self.attrs = ['name', 'mapset', 'mtype', 'creator', 'src1', 'src2',
  28. 'keyword', 'date', 'title']
  29. def __repr__(self):
  30. return "History(%s)" % ', '.join(["%s=%r" % (self.attr, getattr(self, attr))
  31. for attr in self.attrs])
  32. def __del__(self):
  33. """Rast_free_history"""
  34. pass
  35. def __eq__(self, hist):
  36. for attr in self.attrs:
  37. if getattr(self, attr) != getattr(hist, attr):
  38. return False
  39. return True
  40. def __len__(self):
  41. return self.length()
  42. def __iter__(self):
  43. return ((attr, getattr(self, attr)) for attr in self.attrs)
  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. try:
  95. return datetime.datetime.strptime(date_str, self.date_fmt)
  96. except:
  97. return date_str
  98. def _set_date(self, datetimeobj):
  99. if datetimeobj:
  100. date_str = datetimeobj.strftime(self.date_fmt)
  101. return libraster.Rast_set_history(self.c_hist,
  102. libraster.HIST_MAPID,
  103. ctypes.c_char_p(date_str))
  104. date = property(fget=_get_date, fset=_set_date,
  105. doc="Set or obtain the date of map")
  106. # ----------------------------------------------------------------------
  107. # libraster.HIST_MAPSET
  108. def _get_mapset(self):
  109. return libraster.Rast_get_history(self.c_hist,
  110. libraster.HIST_MAPSET)
  111. def _set_mapset(self, mapset):
  112. return libraster.Rast_set_history(self.c_hist,
  113. libraster.HIST_MAPSET,
  114. ctypes.c_char_p(mapset))
  115. mapset = property(fget=_get_mapset, fset=_set_mapset,
  116. doc="Set or obtain the mapset of map")
  117. # ----------------------------------------------------------------------
  118. # libraster.HIST_MAPTYPE
  119. def _get_maptype(self):
  120. return libraster.Rast_get_history(self.c_hist,
  121. libraster.HIST_MAPTYPE)
  122. def _set_maptype(self, maptype):
  123. return libraster.Rast_set_history(self.c_hist,
  124. libraster.HIST_MAPTYPE,
  125. ctypes.c_char_p(maptype))
  126. maptype = property(fget=_get_maptype, fset=_set_maptype,
  127. doc="Set or obtain the type of map")
  128. # ----------------------------------------------------------------------
  129. # libraster.HIST_NUM_FIELDS
  130. #
  131. # Never used in any raster modules
  132. #
  133. # def _get_num_fields(self):
  134. # return libraster.Rast_get_history(self.c_hist,
  135. # libraster.HIST_NUM_FIELDS)
  136. #
  137. # def _set_num_fields(self, num_fields):
  138. # return libraster.Rast_set_history(self.c_hist,
  139. # libraster.HIST_NUM_FIELDS,
  140. # ctypes.c_char_p(num_fields))
  141. #
  142. # num_fields = property(fget = _get_num_fields, fset = _set_num_fields)
  143. # ----------------------------------------------------------------------
  144. # libraster.HIST_TITLE
  145. def _get_title(self):
  146. return libraster.Rast_get_history(self.c_hist,
  147. libraster.HIST_TITLE)
  148. def _set_title(self, title):
  149. return libraster.Rast_set_history(self.c_hist,
  150. libraster.HIST_TITLE,
  151. ctypes.c_char_p(title))
  152. title = property(fget=_get_title, fset=_set_title,
  153. doc="Set or obtain the title of map")
  154. def append(self, obj):
  155. """Rast_append_history"""
  156. libraster.Rast_append_history(self.c_hist,
  157. ctypes.c_char_p(str(obj)))
  158. def append_fmt(self, fmt, *args):
  159. """Rast_append_format_history"""
  160. libraster.Rast_append_format_history(self.c_hist,
  161. ctypes.c_char_p(fmt),
  162. *args)
  163. def clear(self):
  164. """Clear the history"""
  165. libraster.Rast_clear_history(self.c_hist)
  166. def command(self):
  167. """Rast_command_history"""
  168. libraster.Rast_command_history(self.c_hist)
  169. def format(self, field, fmt, *args):
  170. """Rast_format_history"""
  171. libraster.Rast_format_history(self.c_hist,
  172. ctypes.c_int(field),
  173. ctypes.c_char_p(fmt),
  174. *args)
  175. def length(self):
  176. """Rast_history_length"""
  177. return libraster.Rast_history_length(self.c_hist)
  178. def line(self, line):
  179. """Rast_history_line"""
  180. return libraster.Rast_history_line(self.c_hist,
  181. ctypes.c_int(line))
  182. def read(self):
  183. """Read the history of map, users need to use this function to
  184. obtain all the information of map. ::
  185. >>> import grass.lib.gis as libgis
  186. >>> import ctypes
  187. >>> import grass.lib.raster as libraster
  188. >>> hist = libraster.History()
  189. ..
  190. """
  191. libraster.Rast_read_history(self.name, self.mapset, self.c_hist)
  192. def write(self):
  193. """Rast_write_history"""
  194. libraster.Rast_write_history(self.name,
  195. self.c_hist)
  196. def short(self):
  197. """Rast_short_history"""
  198. libraster.Rast_short_history(self.name,
  199. 'raster',
  200. self.c_hist)