1
# -*- coding: utf-8 -*-
3
Created on Fri Aug 17 17:24:03 2012
9
import grass.lib.vector as libvect
10
from grass.pygrass.vector.vector_type import MAPTYPE
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
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)
24
#=============================================
25
# VECTOR ABSTRACT CLASS
26
#=============================================
31
To get access to the vector info the map must be opened. ::
33
>>> cens = Info('census')
34
>>> cens.open(mode='r')
36
Then it is possible to read and write the following map attributes: ::
43
'Wake County census blocks with attributes, clipped (polygon map)'
45
datetime.datetime(2007, 3, 19, 22, 1, 37)
52
>>> cens.comment = "One useful comment!"
60
There are some read only attributes: ::
65
'Lambert Conformal Conic'
69
And some basic methods: ::
80
def __init__(self, name, mapset='', *aopen, **kwopen):
83
# Set map name and mapset
88
self.c_mapinfo = ctypes.pointer(libvect.Map_info())
90
self._class_name = 'Vector'
91
self.overwrite = False
92
self.date_fmt = '%a %b %d %H:%M:%S %Y'
95
self.open(*self._aopen, **self._kwopen)
98
def __exit__(self, exc_type, exc_value, traceback):
102
"""Private method to obtain the Vector name"""
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))
114
name = property(fget=_get_name, fset=_set_name,
115
doc="Set or obtain the Vector name")
117
def _get_mapset(self):
118
"""Private method to obtain the Vector mapset"""
121
def _set_mapset(self, mapset):
122
"""Private method to change the Vector mapset"""
124
self._mapset = mapset
126
mapset = property(fget=_get_mapset, fset=_set_mapset,
127
doc="Set or obtain the Vector mapset")
129
def _get_organization(self):
130
"""Private method to obtain the Vector organization"""
131
return libvect.Vect_get_organization(self.c_mapinfo)
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))
137
organization = property(fget=_get_organization, fset=_set_organization,
138
doc="Set or obtain the Vector organization")
141
"""Private method to obtain the Vector date"""
142
return libvect.Vect_get_date(self.c_mapinfo)
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))
148
date = property(fget=_get_date, fset=_set_date,
149
doc="Set or obtain the Vector date")
151
def _get_person(self):
152
"""Private method to obtain the Vector person"""
153
return libvect.Vect_get_person(self.c_mapinfo)
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))
159
person = property(fget=_get_person, fset=_set_person,
160
doc="Set or obtain the Vector author")
162
def _get_title(self):
163
"""Private method to obtain the Vector title"""
164
return libvect.Vect_get_map_name(self.c_mapinfo)
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))
170
title = property(fget=_get_title, fset=_set_title,
171
doc="Set or obtain the Vector title")
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)
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))
183
map_date = property(fget=_get_map_date, fset=_set_map_date,
184
doc="Set or obtain the Vector map date")
186
def _get_scale(self):
187
"""Private method to obtain the Vector scale"""
188
return libvect.Vect_get_scale(self.c_mapinfo)
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))
194
scale = property(fget=_get_scale, fset=_set_scale,
195
doc="Set or obtain the Vector scale")
197
def _get_comment(self):
198
"""Private method to obtain the Vector comment"""
199
return libvect.Vect_get_comment(self.c_mapinfo)
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))
205
comment = property(fget=_get_comment, fset=_set_comment,
206
doc="Set or obtain the Vector comment")
209
"""Private method to obtain the Vector projection zone"""
210
return libvect.Vect_get_zone(self.c_mapinfo)
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))
216
zone = property(fget=_get_zone, fset=_set_zone,
217
doc="Set or obtain the Vector projection zone")
220
"""Private method to obtain the Vector projection code"""
221
return libvect.Vect_get_proj(self.c_mapinfo)
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))
227
proj = property(fget=_get_proj, fset=_set_proj,
228
doc="Set or obtain the Vector projection code")
230
def _get_thresh(self):
231
"""Private method to obtain the Vector threshold"""
232
return libvect.Vect_get_thresh(self.c_mapinfo)
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))
238
thresh = property(fget=_get_thresh, fset=_set_thresh,
239
doc="Set or obtain the Vector threshold")
244
"""Return the full name of Vector"""
245
return libvect.Vect_get_full_name(self.c_mapinfo)
250
"""Return the map type of Vector"""
251
return MAPTYPE[libvect.Vect_maptype(self.c_mapinfo)]
256
"""Return the project name of Vector"""
257
return libvect.Vect_get_proj_name(self.c_mapinfo)
259
def write_header(self):
260
"""Save the change in the C struct permanently to disk."""
261
libvect.Vect_write_header(self.c_mapinfo)
263
def rename(self, newname):
264
"""Method to rename the Vector map
266
:param newname: the new name for the Vector map
270
if not self.is_open():
271
utils.rename(self.name, newname, 'vect')
273
raise GrassError("The map is open, not able to renamed it.")
277
"""Return if the Vector is 3D"""
278
return bool(libvect.Vect_is_3d(self.c_mapinfo))
281
"""Return if the Vector exists or not"""
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))
292
"""Return if the Vector is open"""
293
return is_open(self.c_mapinfo)
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.
303
:param mode: open a vector map in ``r`` in reading, ``w`` in writing
304
and in ``rw`` read and write mode
306
:param layer: specify the layer that you want to use
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,
314
:param tab_name: define the name of the table that will be generate
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
322
:param link_key: define the nema of the column that will be use as
325
:param link_db: define the database connection parameters
327
:param link_driver: define witch database driver will be used
328
:param link_driver: str
330
Some of the parameters are valid only with mode ``w`` or ``rw``
332
See more examples in the documentation of the ``read`` and ``write``
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'.")
347
# check if the map exist
348
if self.exist() and mode in ('r', 'rw'):
351
openvect = libvect.Vect_open_old2(self.c_mapinfo, self.name,
352
self.mapset, str(layer))
353
# open in READ and WRITE mode
355
openvect = libvect.Vect_open_update2(self.c_mapinfo, self.name,
356
self.mapset, str(layer))
358
# instantiate class attributes
359
self.dblinks = DBlinks(self.c_mapinfo)
361
# If it is opened in write mode
363
openvect = libvect.Vect_open_new(self.c_mapinfo, self.name, with_z)
364
self.dblinks = DBlinks(self.c_mapinfo)
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)
372
self.dblinks.add(link)
375
table.create(tab_cols)
378
# check the C function result.
380
str_err = "Not able to open the map, C function return %d."
381
raise OpenError(str_err % openvect)
383
if len(self.dblinks) == 0:
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,
394
'by_box': BboxFinder(self.c_mapinfo, self.table,
396
'by_polygon': PolygonFinder(self.c_mapinfo, self.table,
399
def close(self, build=False):
400
"""Method to close the Vector
402
:param build: True if the vector map should be build before close it
405
if hasattr(self, 'table') and self.table is not None:
406
self.table.conn.close()
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
417
"""Remove vector map"""
420
utils.remove(self.name, 'vect')
423
"""Close the vector map and build vector Topology"""
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)