history.py 8.6 KB

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