history.py 9.3 KB

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