history.py 9.4 KB

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