~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to lib/python/pygrass/raster/history.py

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
Created on Thu Jun 28 17:44:45 2012
 
4
 
 
5
@author: pietro
 
6
"""
 
7
import ctypes
 
8
import grass.lib.raster as libraster
 
9
import datetime
 
10
 
 
11
 
 
12
class History(object):
 
13
    """History class help to manage all the metadata of a raster map
 
14
 
 
15
    >>> import grass.lib.gis as libgis
 
16
    >>> libgis.G_gisinit('')
 
17
    >>> hist = History('elevation')
 
18
    >>> hist.read()
 
19
    >>> hist.creator
 
20
    'helena'
 
21
    >>> hist.src1
 
22
    ''
 
23
    >>> hist.src2
 
24
    ''
 
25
    >>> hist.keyword
 
26
    'generated by r.proj'
 
27
    >>> hist.date
 
28
    datetime.datetime(2006, 11, 7, 1, 9, 51)
 
29
    >>> hist.mapset
 
30
    'PERMANENT'
 
31
    >>> hist.maptype
 
32
    'raster'
 
33
    >>> hist.title
 
34
    'elev_ned10m'
 
35
 
 
36
    """
 
37
    def __init__(self, name, mapset='', mtype='',
 
38
                 creator='', src1='', src2='', keyword='',
 
39
                 date='', title=''):
 
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'
 
43
        self.name = name
 
44
        self.mapset = mapset
 
45
        self.mtype = mtype
 
46
        self.creator = creator
 
47
        self.src1 = src1
 
48
        self.src2 = src2
 
49
        self.keyword = keyword
 
50
        self.date = date
 
51
        self.title = title
 
52
 
 
53
    def __repr__(self):
 
54
        attrs = ['name', 'mapset', 'mtype', 'creator', 'src1', 'src2',
 
55
                 'keyword', 'date', 'title']
 
56
        return "History(%s)" % ', '.join(["%s=%r" % (attr, getattr(self, attr))
 
57
                                          for attr in attrs])
 
58
 
 
59
    def __del__(self):
 
60
        """Rast_free_history"""
 
61
        pass
 
62
 
 
63
    def __len__(self):
 
64
        return self.length()
 
65
 
 
66
    #----------------------------------------------------------------------
 
67
    #libraster.HIST_CREATOR
 
68
    def _get_creator(self):
 
69
        return libraster.Rast_get_history(self.c_hist,
 
70
                                          libraster.HIST_CREATOR)
 
71
 
 
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))
 
76
 
 
77
    creator = property(fget=_get_creator, fset=_set_creator,
 
78
                       doc="Set or obtain the creator of map")
 
79
 
 
80
    #----------------------------------------------------------------------
 
81
    #libraster.HIST_DATSRC_1
 
82
    def _get_src1(self):
 
83
        return libraster.Rast_get_history(self.c_hist,
 
84
                                          libraster.HIST_DATSRC_1)
 
85
 
 
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))
 
90
 
 
91
    src1 = property(fget=_get_src1, fset=_set_src1,
 
92
                    doc="Set or obtain the first source of map")
 
93
 
 
94
    #----------------------------------------------------------------------
 
95
    #libraster.HIST_DATSRC_2
 
96
    def _get_src2(self):
 
97
        return libraster.Rast_get_history(self.c_hist,
 
98
                                          libraster.HIST_DATSRC_2)
 
99
 
 
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))
 
104
 
 
105
    src2 = property(fget=_get_src2, fset=_set_src2,
 
106
                    doc="Set or obtain the second source of map")
 
107
 
 
108
    #----------------------------------------------------------------------
 
109
    #libraster.HIST_KEYWORD
 
110
    def _get_keyword(self):
 
111
        return libraster.Rast_get_history(self.c_hist,
 
112
                                          libraster.HIST_KEYWRD)
 
113
 
 
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))
 
118
 
 
119
    keyword = property(fget=_get_keyword, fset=_set_keyword,
 
120
                       doc="Set or obtain the keywords of map")
 
121
 
 
122
    #----------------------------------------------------------------------
 
123
    #libraster.HIST_MAPID
 
124
    def _get_date(self):
 
125
        date_str = libraster.Rast_get_history(self.c_hist,
 
126
                                              libraster.HIST_MAPID)
 
127
        if date_str:
 
128
            return datetime.datetime.strptime(date_str, self.date_fmt)
 
129
 
 
130
    def _set_date(self, datetimeobj):
 
131
        if 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))
 
136
 
 
137
    date = property(fget=_get_date, fset=_set_date,
 
138
                    doc="Set or obtain the date of map")
 
139
 
 
140
    #----------------------------------------------------------------------
 
141
    #libraster.HIST_MAPSET
 
142
    def _get_mapset(self):
 
143
        return libraster.Rast_get_history(self.c_hist,
 
144
                                          libraster.HIST_MAPSET)
 
145
 
 
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))
 
150
 
 
151
    mapset = property(fget=_get_mapset, fset=_set_mapset,
 
152
                      doc="Set or obtain the mapset of map")
 
153
 
 
154
    #----------------------------------------------------------------------
 
155
    #libraster.HIST_MAPTYPE
 
156
    def _get_maptype(self):
 
157
        return libraster.Rast_get_history(self.c_hist,
 
158
                                          libraster.HIST_MAPTYPE)
 
159
 
 
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))
 
164
 
 
165
    maptype = property(fget=_get_maptype, fset=_set_maptype,
 
166
                       doc="Set or obtain the type of map")
 
167
 
 
168
    #----------------------------------------------------------------------
 
169
    #libraster.HIST_NUM_FIELDS
 
170
    #
 
171
    # Never used in any raster modules
 
172
    #
 
173
    #    def _get_num_fields(self):
 
174
    #        return libraster.Rast_get_history(self.c_hist,
 
175
    #                                          libraster.HIST_NUM_FIELDS)
 
176
    #
 
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))
 
181
    #
 
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)
 
188
 
 
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))
 
193
 
 
194
    title = property(fget=_get_title, fset=_set_title,
 
195
                     doc="Set or obtain the title of map")
 
196
 
 
197
    def append(self, obj):
 
198
        """Rast_append_history"""
 
199
        libraster.Rast_append_history(self.c_hist,
 
200
                                      ctypes.c_char_p(str(obj)))
 
201
 
 
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),
 
206
                                             *args)
 
207
 
 
208
    def clear(self):
 
209
        """Clear the history"""
 
210
        libraster.Rast_clear_history(self.c_hist)
 
211
 
 
212
    def command(self):
 
213
        """Rast_command_history"""
 
214
        libraster.Rast_command_history(self.c_hist)
 
215
 
 
216
    def format(self, field, fmt, *args):
 
217
        """Rast_format_history"""
 
218
        libraster.Rast_format_history(self.c_hist,
 
219
                                      ctypes.c_int(field),
 
220
                                      ctypes.c_char_p(fmt),
 
221
                                      *args)
 
222
 
 
223
    def length(self):
 
224
        """Rast_history_length"""
 
225
        return libraster.Rast_history_length(self.c_hist)
 
226
 
 
227
    def line(self, line):
 
228
        """Rast_history_line"""
 
229
        return libraster.Rast_history_line(self.c_hist,
 
230
                                           ctypes.c_int(line))
 
231
 
 
232
    def read(self):
 
233
        """Read the history of map, users need to use this function to
 
234
        obtain all the information of map. ::
 
235
 
 
236
            >>> import grass.lib.gis as libgis
 
237
            >>> libgis.G_gisinit('')
 
238
            >>> import ctypes
 
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))
 
244
            0
 
245
            >>> libraster.Rast_get_history(ctypes.byref(hist),
 
246
            ...                            libraster.HIST_MAPID)
 
247
            'Tue Nov  7 01:09:51 2006'
 
248
 
 
249
        ..
 
250
        """
 
251
        libraster.Rast_read_history(self.name, self.mapset, self.c_hist)
 
252
 
 
253
    def write(self):
 
254
        """Rast_write_history"""
 
255
        libraster.Rast_write_history(self.name,
 
256
                                     self.c_hist)
 
257
 
 
258
    def short(self):
 
259
        """Rast_short_history"""
 
260
        libraster.Rast_short_history(self.name,
 
261
                                     'raster',
 
262
                                     self.c_hist)