history.py 9.4 KB

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