1
# -*- coding: utf-8 -*-
3
Created on Thu Jun 28 17:44:45 2012
8
import grass.lib.raster as libraster
12
class History(object):
13
"""History class help to manage all the metadata of a raster map
15
>>> import grass.lib.gis as libgis
16
>>> libgis.G_gisinit('')
17
>>> hist = History('elevation')
28
datetime.datetime(2006, 11, 7, 1, 9, 51)
37
def __init__(self, name, mapset='', mtype='',
38
creator='', src1='', src2='', keyword='',
40
self.c_hist = ctypes.pointer(libraster.History())
41
# 'Tue Nov 7 01:11:23 2006'
42
self.date_fmt = '%a %b %d %H:%M:%S %Y'
46
self.creator = creator
49
self.keyword = keyword
54
attrs = ['name', 'mapset', 'mtype', 'creator', 'src1', 'src2',
55
'keyword', 'date', 'title']
56
return "History(%s)" % ', '.join(["%s=%r" % (attr, getattr(self, attr))
60
"""Rast_free_history"""
66
#----------------------------------------------------------------------
67
#libraster.HIST_CREATOR
68
def _get_creator(self):
69
return libraster.Rast_get_history(self.c_hist,
70
libraster.HIST_CREATOR)
72
def _set_creator(self, creator):
73
return libraster.Rast_set_history(self.c_hist,
74
libraster.HIST_CREATOR,
75
ctypes.c_char_p(creator))
77
creator = property(fget=_get_creator, fset=_set_creator,
78
doc="Set or obtain the creator of map")
80
#----------------------------------------------------------------------
81
#libraster.HIST_DATSRC_1
83
return libraster.Rast_get_history(self.c_hist,
84
libraster.HIST_DATSRC_1)
86
def _set_src1(self, src1):
87
return libraster.Rast_set_history(self.c_hist,
88
libraster.HIST_DATSRC_1,
89
ctypes.c_char_p(src1))
91
src1 = property(fget=_get_src1, fset=_set_src1,
92
doc="Set or obtain the first source of map")
94
#----------------------------------------------------------------------
95
#libraster.HIST_DATSRC_2
97
return libraster.Rast_get_history(self.c_hist,
98
libraster.HIST_DATSRC_2)
100
def _set_src2(self, src2):
101
return libraster.Rast_set_history(self.c_hist,
102
libraster.HIST_DATSRC_2,
103
ctypes.c_char_p(src2))
105
src2 = property(fget=_get_src2, fset=_set_src2,
106
doc="Set or obtain the second source of map")
108
#----------------------------------------------------------------------
109
#libraster.HIST_KEYWORD
110
def _get_keyword(self):
111
return libraster.Rast_get_history(self.c_hist,
112
libraster.HIST_KEYWRD)
114
def _set_keyword(self, keyword):
115
return libraster.Rast_set_history(self.c_hist,
116
libraster.HIST_KEYWRD,
117
ctypes.c_char_p(keyword))
119
keyword = property(fget=_get_keyword, fset=_set_keyword,
120
doc="Set or obtain the keywords of map")
122
#----------------------------------------------------------------------
123
#libraster.HIST_MAPID
125
date_str = libraster.Rast_get_history(self.c_hist,
126
libraster.HIST_MAPID)
128
return datetime.datetime.strptime(date_str, self.date_fmt)
130
def _set_date(self, datetimeobj):
132
date_str = datetimeobj.strftime(self.date_fmt)
133
return libraster.Rast_set_history(self.c_hist,
134
libraster.HIST_MAPID,
135
ctypes.c_char_p(date_str))
137
date = property(fget=_get_date, fset=_set_date,
138
doc="Set or obtain the date of map")
140
#----------------------------------------------------------------------
141
#libraster.HIST_MAPSET
142
def _get_mapset(self):
143
return libraster.Rast_get_history(self.c_hist,
144
libraster.HIST_MAPSET)
146
def _set_mapset(self, mapset):
147
return libraster.Rast_set_history(self.c_hist,
148
libraster.HIST_MAPSET,
149
ctypes.c_char_p(mapset))
151
mapset = property(fget=_get_mapset, fset=_set_mapset,
152
doc="Set or obtain the mapset of map")
154
#----------------------------------------------------------------------
155
#libraster.HIST_MAPTYPE
156
def _get_maptype(self):
157
return libraster.Rast_get_history(self.c_hist,
158
libraster.HIST_MAPTYPE)
160
def _set_maptype(self, maptype):
161
return libraster.Rast_set_history(self.c_hist,
162
libraster.HIST_MAPTYPE,
163
ctypes.c_char_p(maptype))
165
maptype = property(fget=_get_maptype, fset=_set_maptype,
166
doc="Set or obtain the type of map")
168
#----------------------------------------------------------------------
169
#libraster.HIST_NUM_FIELDS
171
# Never used in any raster modules
173
# def _get_num_fields(self):
174
# return libraster.Rast_get_history(self.c_hist,
175
# libraster.HIST_NUM_FIELDS)
177
# def _set_num_fields(self, num_fields):
178
# return libraster.Rast_set_history(self.c_hist,
179
# libraster.HIST_NUM_FIELDS,
180
# ctypes.c_char_p(num_fields))
182
# num_fields = property(fget = _get_num_fields, fset = _set_num_fields)
183
#----------------------------------------------------------------------
184
#libraster.HIST_TITLE
185
def _get_title(self):
186
return libraster.Rast_get_history(self.c_hist,
187
libraster.HIST_TITLE)
189
def _set_title(self, title):
190
return libraster.Rast_set_history(self.c_hist,
191
libraster.HIST_TITLE,
192
ctypes.c_char_p(title))
194
title = property(fget=_get_title, fset=_set_title,
195
doc="Set or obtain the title of map")
197
def append(self, obj):
198
"""Rast_append_history"""
199
libraster.Rast_append_history(self.c_hist,
200
ctypes.c_char_p(str(obj)))
202
def append_fmt(self, fmt, *args):
203
"""Rast_append_format_history"""
204
libraster.Rast_append_format_history(self.c_hist,
205
ctypes.c_char_p(fmt),
209
"""Clear the history"""
210
libraster.Rast_clear_history(self.c_hist)
213
"""Rast_command_history"""
214
libraster.Rast_command_history(self.c_hist)
216
def format(self, field, fmt, *args):
217
"""Rast_format_history"""
218
libraster.Rast_format_history(self.c_hist,
220
ctypes.c_char_p(fmt),
224
"""Rast_history_length"""
225
return libraster.Rast_history_length(self.c_hist)
227
def line(self, line):
228
"""Rast_history_line"""
229
return libraster.Rast_history_line(self.c_hist,
233
"""Read the history of map, users need to use this function to
234
obtain all the information of map. ::
236
>>> import grass.lib.gis as libgis
237
>>> libgis.G_gisinit('')
239
>>> import grass.lib.raster as libraster
240
>>> hist = libraster.History()
241
>>> libraster.Rast_read_history(ctypes.c_char_p('elevation'),
242
... ctypes.c_char_p(''),
243
... ctypes.byref(hist))
245
>>> libraster.Rast_get_history(ctypes.byref(hist),
246
... libraster.HIST_MAPID)
247
'Tue Nov 7 01:09:51 2006'
251
libraster.Rast_read_history(self.name, self.mapset, self.c_hist)
254
"""Rast_write_history"""
255
libraster.Rast_write_history(self.name,
259
"""Rast_short_history"""
260
libraster.Rast_short_history(self.name,