history.py 9.5 KB

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