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

« back to all changes in this revision

Viewing changes to lib/python/pygrass/vector/abstract.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 Fri Aug 17 17:24:03 2012
 
4
 
 
5
@author: pietro
 
6
"""
 
7
import ctypes
 
8
import datetime
 
9
import grass.lib.vector as libvect
 
10
from grass.pygrass.vector.vector_type import MAPTYPE
 
11
 
 
12
from grass.pygrass import utils
 
13
from grass.pygrass.errors import GrassError, OpenError, must_be_open
 
14
from grass.pygrass.vector.table import DBlinks, Link
 
15
from grass.pygrass.vector.find import PointFinder, BboxFinder, PolygonFinder
 
16
 
 
17
 
 
18
def is_open(c_mapinfo):
 
19
    """Return if the Vector is open"""
 
20
    return (c_mapinfo.contents.open != 0 and
 
21
            c_mapinfo.contents.open != libvect.VECT_CLOSED_CODE)
 
22
 
 
23
 
 
24
#=============================================
 
25
# VECTOR ABSTRACT CLASS
 
26
#=============================================
 
27
 
 
28
 
 
29
class Info(object):
 
30
    """Basic vector info.
 
31
    To get access to the vector info the map must be opened. ::
 
32
 
 
33
        >>> cens = Info('census')
 
34
        >>> cens.open(mode='r')
 
35
 
 
36
    Then it is possible to read and write the following map attributes: ::
 
37
 
 
38
        >>> cens.organization
 
39
        'NC OneMap'
 
40
        >>> cens.person
 
41
        'hmitaso'
 
42
        >>> cens.title
 
43
        'Wake County census blocks with attributes, clipped (polygon map)'
 
44
        >>> cens.map_date
 
45
        datetime.datetime(2007, 3, 19, 22, 1, 37)
 
46
        >>> cens.date
 
47
        ''
 
48
        >>> cens.scale
 
49
        1
 
50
        >>> cens.comment
 
51
        ''
 
52
        >>> cens.comment = "One useful comment!"
 
53
        >>> cens.comment
 
54
        'One useful comment!'
 
55
        >>> cens.zone
 
56
        0
 
57
        >>> cens.proj
 
58
        99
 
59
 
 
60
    There are some read only attributes: ::
 
61
 
 
62
        >>> cens.full_name
 
63
        'census@PERMANENT'
 
64
        >>> cens.proj_name
 
65
        'Lambert Conformal Conic'
 
66
        >>> cens.maptype
 
67
        'native'
 
68
 
 
69
    And some basic methods: ::
 
70
 
 
71
        >>> cens.is_3D()
 
72
        False
 
73
        >>> cens.exist()
 
74
        True
 
75
        >>> cens.is_open()
 
76
        True
 
77
        >>> cens.close()
 
78
 
 
79
    """
 
80
    def __init__(self, name, mapset='', *aopen, **kwopen):
 
81
        self._name = ''
 
82
        self._mapset = ''
 
83
        # Set map name and mapset
 
84
        self.name = name
 
85
        self.mapset = mapset
 
86
        self._aopen = aopen
 
87
        self._kwopen = kwopen
 
88
        self.c_mapinfo = ctypes.pointer(libvect.Map_info())
 
89
        self._topo_level = 1
 
90
        self._class_name = 'Vector'
 
91
        self.overwrite = False
 
92
        self.date_fmt = '%a %b  %d %H:%M:%S %Y'
 
93
 
 
94
    def __enter__(self):
 
95
        self.open(*self._aopen, **self._kwopen)
 
96
        return self
 
97
 
 
98
    def __exit__(self, exc_type, exc_value, traceback):
 
99
        self.close()
 
100
 
 
101
    def _get_name(self):
 
102
        """Private method to obtain the Vector name"""
 
103
        return self._name
 
104
 
 
105
    def _set_name(self, newname):
 
106
        """Private method to change the Vector name"""
 
107
        if not utils.is_clean_name(newname):
 
108
            str_err = _("Map name {0} not valid")
 
109
            raise ValueError(str_err.format(newname))
 
110
        if self.exist():
 
111
            self.rename(newname)
 
112
        self._name = newname
 
113
 
 
114
    name = property(fget=_get_name, fset=_set_name,
 
115
                    doc="Set or obtain the Vector name")
 
116
 
 
117
    def _get_mapset(self):
 
118
        """Private method to obtain the Vector mapset"""
 
119
        return self._mapset
 
120
 
 
121
    def _set_mapset(self, mapset):
 
122
        """Private method to change the Vector mapset"""
 
123
        if mapset:
 
124
            self._mapset = mapset
 
125
 
 
126
    mapset = property(fget=_get_mapset, fset=_set_mapset,
 
127
                      doc="Set or obtain the Vector mapset")
 
128
 
 
129
    def _get_organization(self):
 
130
        """Private method to obtain the Vector organization"""
 
131
        return libvect.Vect_get_organization(self.c_mapinfo)
 
132
 
 
133
    def _set_organization(self, org):
 
134
        """Private method to change the Vector organization"""
 
135
        libvect.Vect_set_organization(self.c_mapinfo, ctypes.c_char_p(org))
 
136
 
 
137
    organization = property(fget=_get_organization, fset=_set_organization,
 
138
                            doc="Set or obtain the Vector organization")
 
139
 
 
140
    def _get_date(self):
 
141
        """Private method to obtain the Vector date"""
 
142
        return libvect.Vect_get_date(self.c_mapinfo)
 
143
 
 
144
    def _set_date(self, date):
 
145
        """Private method to change the Vector date"""
 
146
        return libvect.Vect_set_date(self.c_mapinfo, ctypes.c_char_p(date))
 
147
 
 
148
    date = property(fget=_get_date, fset=_set_date,
 
149
                    doc="Set or obtain the Vector date")
 
150
 
 
151
    def _get_person(self):
 
152
        """Private method to obtain the Vector person"""
 
153
        return libvect.Vect_get_person(self.c_mapinfo)
 
154
 
 
155
    def _set_person(self, person):
 
156
        """Private method to change the Vector person"""
 
157
        libvect.Vect_set_person(self.c_mapinfo, ctypes.c_char_p(person))
 
158
 
 
159
    person = property(fget=_get_person, fset=_set_person,
 
160
                      doc="Set or obtain the Vector author")
 
161
 
 
162
    def _get_title(self):
 
163
        """Private method to obtain the Vector title"""
 
164
        return libvect.Vect_get_map_name(self.c_mapinfo)
 
165
 
 
166
    def _set_title(self, title):
 
167
        """Private method to change the Vector title"""
 
168
        libvect.Vect_set_map_name(self.c_mapinfo, ctypes.c_char_p(title))
 
169
 
 
170
    title = property(fget=_get_title, fset=_set_title,
 
171
                     doc="Set or obtain the Vector title")
 
172
 
 
173
    def _get_map_date(self):
 
174
        """Private method to obtain the Vector map date"""
 
175
        date_str = libvect.Vect_get_map_date(self.c_mapinfo)
 
176
        return datetime.datetime.strptime(date_str, self.date_fmt)
 
177
 
 
178
    def _set_map_date(self, datetimeobj):
 
179
        """Private method to change the Vector map date"""
 
180
        date_str = datetimeobj.strftime(self.date_fmt)
 
181
        libvect.Vect_set_map_date(self.c_mapinfo, ctypes.c_char_p(date_str))
 
182
 
 
183
    map_date = property(fget=_get_map_date, fset=_set_map_date,
 
184
                        doc="Set or obtain the Vector map date")
 
185
 
 
186
    def _get_scale(self):
 
187
        """Private method to obtain the Vector scale"""
 
188
        return libvect.Vect_get_scale(self.c_mapinfo)
 
189
 
 
190
    def _set_scale(self, scale):
 
191
        """Private method to set the Vector scale"""
 
192
        return libvect.Vect_set_scale(self.c_mapinfo, ctypes.c_int(scale))
 
193
 
 
194
    scale = property(fget=_get_scale, fset=_set_scale,
 
195
                     doc="Set or obtain the Vector scale")
 
196
 
 
197
    def _get_comment(self):
 
198
        """Private method to obtain the Vector comment"""
 
199
        return libvect.Vect_get_comment(self.c_mapinfo)
 
200
 
 
201
    def _set_comment(self, comm):
 
202
        """Private method to set the Vector comment"""
 
203
        return libvect.Vect_set_comment(self.c_mapinfo, ctypes.c_char_p(comm))
 
204
 
 
205
    comment = property(fget=_get_comment, fset=_set_comment,
 
206
                       doc="Set or obtain the Vector comment")
 
207
 
 
208
    def _get_zone(self):
 
209
        """Private method to obtain the Vector projection zone"""
 
210
        return libvect.Vect_get_zone(self.c_mapinfo)
 
211
 
 
212
    def _set_zone(self, zone):
 
213
        """Private method to set the Vector projection zone"""
 
214
        return libvect.Vect_set_zone(self.c_mapinfo, ctypes.c_int(zone))
 
215
 
 
216
    zone = property(fget=_get_zone, fset=_set_zone,
 
217
                    doc="Set or obtain the Vector projection zone")
 
218
 
 
219
    def _get_proj(self):
 
220
        """Private method to obtain the Vector projection code"""
 
221
        return libvect.Vect_get_proj(self.c_mapinfo)
 
222
 
 
223
    def _set_proj(self, proj):
 
224
        """Private method to set the Vector projection code"""
 
225
        libvect.Vect_set_proj(self.c_mapinfo, ctypes.c_int(proj))
 
226
 
 
227
    proj = property(fget=_get_proj, fset=_set_proj,
 
228
                    doc="Set or obtain the Vector projection code")
 
229
 
 
230
    def _get_thresh(self):
 
231
        """Private method to obtain the Vector threshold"""
 
232
        return libvect.Vect_get_thresh(self.c_mapinfo)
 
233
 
 
234
    def _set_thresh(self, thresh):
 
235
        """Private method to set the Vector threshold"""
 
236
        return libvect.Vect_set_thresh(self.c_mapinfo, ctypes.c_double(thresh))
 
237
 
 
238
    thresh = property(fget=_get_thresh, fset=_set_thresh,
 
239
                      doc="Set or obtain the Vector threshold")
 
240
 
 
241
    @property
 
242
    @must_be_open
 
243
    def full_name(self):
 
244
        """Return the full name of Vector"""
 
245
        return libvect.Vect_get_full_name(self.c_mapinfo)
 
246
 
 
247
    @property
 
248
    @must_be_open
 
249
    def maptype(self):
 
250
        """Return the map type of Vector"""
 
251
        return MAPTYPE[libvect.Vect_maptype(self.c_mapinfo)]
 
252
 
 
253
    @property
 
254
    @must_be_open
 
255
    def proj_name(self):
 
256
        """Return the project name of Vector"""
 
257
        return libvect.Vect_get_proj_name(self.c_mapinfo)
 
258
 
 
259
    def write_header(self):
 
260
        """Save the change in the C struct permanently to disk."""
 
261
        libvect.Vect_write_header(self.c_mapinfo)
 
262
 
 
263
    def rename(self, newname):
 
264
        """Method to rename the Vector map
 
265
 
 
266
        :param newname: the new name for the Vector map
 
267
        :type newname: str
 
268
        """
 
269
        if self.exist():
 
270
            if not self.is_open():
 
271
                utils.rename(self.name, newname, 'vect')
 
272
            else:
 
273
                raise GrassError("The map is open, not able to renamed it.")
 
274
        self._name = newname
 
275
 
 
276
    def is_3D(self):
 
277
        """Return if the Vector is 3D"""
 
278
        return bool(libvect.Vect_is_3d(self.c_mapinfo))
 
279
 
 
280
    def exist(self):
 
281
        """Return if the Vector exists or not"""
 
282
        if self.name:
 
283
            if self.mapset == '':
 
284
                mapset = utils.get_mapset_vector(self.name, self.mapset)
 
285
                self.mapset = mapset if mapset else ''
 
286
                return True if mapset else False
 
287
            return bool(utils.get_mapset_vector(self.name, self.mapset))
 
288
        else:
 
289
            return False
 
290
 
 
291
    def is_open(self):
 
292
        """Return if the Vector is open"""
 
293
        return is_open(self.c_mapinfo)
 
294
 
 
295
    def open(self, mode=None, layer=1, overwrite=None, with_z=None,
 
296
             # parameters valid only if mode == 'w'
 
297
             tab_name='', tab_cols=None, link_name=None, link_key='cat',
 
298
             link_db='$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db',
 
299
             link_driver='sqlite'):
 
300
        """Open a Vector map.
 
301
 
 
302
 
 
303
        :param mode: open a vector map in ``r`` in reading, ``w`` in writing
 
304
                     and in ``rw`` read and write mode
 
305
        :type mode: str
 
306
        :param layer: specify the layer that you want to use
 
307
        :type layer: int
 
308
        :param overwrite: valid only for ``w`` mode
 
309
        :type overwrite: bool
 
310
        :param with_z: specify if vector map must be open with third dimension
 
311
                       enabled or not. Valid only for ``w`` mode,
 
312
                       default: False
 
313
        :type with_z: bool
 
314
        :param tab_name: define the name of the table that will be generate
 
315
        :type tab_name: str
 
316
        :param tab_cols: define the name and type of the columns of the
 
317
                         attribute table of the vecto map
 
318
        :type tab_cols: list of pairs
 
319
        :param link_name: define the name of the link connecttion with the
 
320
                          database
 
321
        :type link_name: str
 
322
        :param link_key: define the nema of the column that will be use as
 
323
                         vector category
 
324
        :type link_key: str
 
325
        :param link_db: define the database connection parameters
 
326
        :type link_db: str
 
327
        :param link_driver: define witch database driver will be used
 
328
        :param link_driver: str
 
329
 
 
330
        Some of the parameters are valid only with mode ``w`` or ``rw``
 
331
 
 
332
        See more examples in the documentation of the ``read`` and ``write``
 
333
        methods
 
334
        """
 
335
        with_z = libvect.WITH_Z if with_z else libvect.WITHOUT_Z
 
336
        # check if map exists or not
 
337
        if not self.exist() and mode != 'w':
 
338
            raise OpenError("Map <%s> not found." % self._name)
 
339
        if libvect.Vect_set_open_level(self._topo_level) != 0:
 
340
            raise OpenError("Invalid access level.")
 
341
        # update the overwrite attribute
 
342
        self.overwrite = overwrite if overwrite is not None else self.overwrite
 
343
        # check if the mode is valid
 
344
        if mode not in ('r', 'rw', 'w'):
 
345
            raise ValueError("Mode not supported. Use one of: 'r', 'rw', 'w'.")
 
346
 
 
347
        # check if the map exist
 
348
        if self.exist() and mode in ('r', 'rw'):
 
349
            # open in READ mode
 
350
            if mode == 'r':
 
351
                openvect = libvect.Vect_open_old2(self.c_mapinfo, self.name,
 
352
                                                  self.mapset, str(layer))
 
353
            # open in READ and WRITE mode
 
354
            elif mode == 'rw':
 
355
                openvect = libvect.Vect_open_update2(self.c_mapinfo, self.name,
 
356
                                                     self.mapset, str(layer))
 
357
 
 
358
            # instantiate class attributes
 
359
            self.dblinks = DBlinks(self.c_mapinfo)
 
360
 
 
361
        # If it is opened in write mode
 
362
        if mode == 'w':
 
363
            openvect = libvect.Vect_open_new(self.c_mapinfo, self.name, with_z)
 
364
            self.dblinks = DBlinks(self.c_mapinfo)
 
365
            if tab_cols:
 
366
                # create a link
 
367
                link = Link(layer,
 
368
                            link_name if link_name else self.name,
 
369
                            tab_name if tab_name else self.name,
 
370
                            link_key, link_db, link_driver)
 
371
                # add the new link
 
372
                self.dblinks.add(link)
 
373
                # create the table
 
374
                table = link.table()
 
375
                table.create(tab_cols)
 
376
                table.conn.commit()
 
377
 
 
378
        # check the C function result.
 
379
        if openvect == -1:
 
380
            str_err = "Not able to open the map, C function return %d."
 
381
            raise OpenError(str_err % openvect)
 
382
 
 
383
        if len(self.dblinks) == 0:
 
384
            self.layer = layer
 
385
            self.table = None
 
386
            self.n_lines = 0
 
387
        else:
 
388
            self.layer = self.dblinks.by_layer(layer).layer
 
389
            self.table = self.dblinks.by_layer(layer).table()
 
390
            self.n_lines = self.table.n_rows()
 
391
        self.writable = self.mapset == utils.getenv("MAPSET")
 
392
        self.find = {'by_point': PointFinder(self.c_mapinfo, self.table,
 
393
                                             self.writable),
 
394
                     'by_box': BboxFinder(self.c_mapinfo, self.table,
 
395
                                          self.writable),
 
396
                     'by_polygon': PolygonFinder(self.c_mapinfo, self.table,
 
397
                                                 self.writable), }
 
398
 
 
399
    def close(self, build=False):
 
400
        """Method to close the Vector
 
401
 
 
402
        :param build: True if the vector map should be build before close it
 
403
        :type build: bool
 
404
        """
 
405
        if hasattr(self, 'table') and self.table is not None:
 
406
            self.table.conn.close()
 
407
        if self.is_open():
 
408
            if libvect.Vect_close(self.c_mapinfo) != 0:
 
409
                str_err = 'Error when trying to close the map with Vect_close'
 
410
                raise GrassError(str_err)
 
411
            if ((self.c_mapinfo.contents.mode == libvect.GV_MODE_RW or
 
412
                    self.c_mapinfo.contents.mode == libvect.GV_MODE_WRITE) and
 
413
                    build):
 
414
                self.build()
 
415
 
 
416
    def remove(self):
 
417
        """Remove vector map"""
 
418
        if self.is_open():
 
419
            self.close()
 
420
        utils.remove(self.name, 'vect')
 
421
 
 
422
    def build(self):
 
423
        """Close the vector map and build vector Topology"""
 
424
        self.close()
 
425
        libvect.Vect_set_open_level(1)
 
426
        if libvect.Vect_open_old2(self.c_mapinfo, self.name,
 
427
                                  self.mapset, '0') != 1:
 
428
            str_err = 'Error when trying to open the vector map.'
 
429
            raise GrassError(str_err)
 
430
        # Vect_build returns 1 on success and 0 on error (bool approach)
 
431
        if libvect.Vect_build(self.c_mapinfo) != 1:
 
432
            str_err = 'Error when trying build topology with Vect_build'
 
433
            raise GrassError(str_err)
 
434
        libvect.Vect_close(self.c_mapinfo)