[GRASS-SVN] r54043 - grass/trunk/lib/python/pygrass/vector
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Nov 26 01:41:47 PST 2012
Author: zarch
Date: 2012-11-26 01:41:47 -0800 (Mon, 26 Nov 2012)
New Revision: 54043
Modified:
grass/trunk/lib/python/pygrass/vector/__init__.py
grass/trunk/lib/python/pygrass/vector/abstract.py
grass/trunk/lib/python/pygrass/vector/geometry.py
Log:
Add an easy way to access (read/write) to the attributes of a geometry feature in a vector map
Modified: grass/trunk/lib/python/pygrass/vector/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/__init__.py 2012-11-26 09:41:18 UTC (rev 54042)
+++ grass/trunk/lib/python/pygrass/vector/__init__.py 2012-11-26 09:41:47 UTC (rev 54043)
@@ -295,7 +295,8 @@
"""
if vtype in _GEOOBJ.keys():
if _GEOOBJ[vtype] is not None:
- return (_GEOOBJ[vtype](v_id=indx, c_mapinfo=self.c_mapinfo)
+ return (_GEOOBJ[vtype](v_id=indx, c_mapinfo=self.c_mapinfo,
+ table=self.table)
for indx in xrange(1, self.number_of(vtype) + 1))
else:
keys = "', '".join(sorted(_GEOOBJ.keys()))
@@ -362,7 +363,8 @@
return GV_TYPE[ftype]['obj'](v_id=feature_id,
c_mapinfo=self.c_mapinfo,
c_points=c_points,
- c_cats=c_cats)
+ c_cats=c_cats,
+ table=self.table)
else:
raise ValueError('The index must be >0, %r given.' % feature_id)
Modified: grass/trunk/lib/python/pygrass/vector/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/abstract.py 2012-11-26 09:41:18 UTC (rev 54042)
+++ grass/trunk/lib/python/pygrass/vector/abstract.py 2012-11-26 09:41:47 UTC (rev 54043)
@@ -72,7 +72,7 @@
>>> municip.close()
"""
- def __init__(self, name, mapset=''):
+ def __init__(self, name, mapset='', link_id=None):
# Set map name and mapset
self._name = name
self.mapset = mapset
@@ -81,6 +81,7 @@
self._class_name = 'Vector'
self.overwrite = False
self.date_fmt = '%a %b %d %H:%M:%S %Y'
+ self.link_id = link_id
def _get_name(self):
if self.exist() and self.is_open():
@@ -259,7 +260,19 @@
if openvect == -1:
str_err = "Not able to open the map, C function return %d."
raise OpenError(str_err % openvect)
+ # istantiate the table
+ self.table = self.get_table(link_id=self.link_id)
+ def get_table(self, link_id=None, link_name=None,):
+ if link_id is None and link_name is None and len(self.dblinks) == 0:
+ return None
+ if link_id is not None:
+ return self.dblinks.by_number(link_id).table()
+ elif link_name is not None:
+ return self.dblinks.by_name(link_name).table()
+ else:
+ return self.dblinks.by_number(1).table()
+
def close(self):
if self.is_open():
if libvect.Vect_close(self.c_mapinfo) != 0:
Modified: grass/trunk/lib/python/pygrass/vector/geometry.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/geometry.py 2012-11-26 09:41:18 UTC (rev 54042)
+++ grass/trunk/lib/python/pygrass/vector/geometry.py 2012-11-26 09:41:47 UTC (rev 54043)
@@ -6,13 +6,15 @@
"""
import ctypes
-import numpy as np
import re
+import numpy as np
+
import grass.lib.gis as libgis
import grass.lib.vector as libvect
from basic import Ilist, Bbox, Cats
+import sql
WKT = {'POINT\((.*)\)': 'point', # 'POINT\(\s*([+-]*\d+\.*\d*)+\s*\)'
@@ -107,6 +109,64 @@
return x, y, z
+class Attrs(object):
+ def __init__(self, v_id, table):
+ self.id = v_id
+ self.table = table
+ self.cond = "%s=%d" % (self.table.key, self.id)
+
+ def __getitem__(self, key):
+ """Return the value stored in the attribute table. ::
+
+ >>> attrs = Attrs(v_id, table)
+ >>> attrs['LABEL']
+ .
+
+ .."""
+ #SELECT {cols} FROM {tname} WHERE {condition};
+ cur = self.table.execute(sql.SELECT_WHERE.format(cols=key,
+ tname=self.table.name,
+ condition=self.cond))
+ return cur.fetchone()[0]
+
+ def __setitem__(self, key, value):
+ """Set value of a given column of a table attribute. ::
+
+ >>> attrs = Attrs(v_id, table)
+ >>> attrs['LABEL'] = 'New Label'
+
+ .."""
+ #UPDATE {tname} SET {new_col} = {old_col} WHERE {condition}
+ self.table.execute(sql.UPDATE_WHERE.format(tname=self.table.name,
+ new_col=key,
+ old_col=repr(value),
+ condition=self.cond))
+ #self.table.conn.commit()
+
+ def __dict__(self):
+ """Reurn a dict of the attribute table row."""
+ dic = {}
+ for key, val in zip(self.keys(), self.values()):
+ dic[key] = val
+ return dic
+
+ def values(self):
+ """Return the values of the attribute table row."""
+ #SELECT {cols} FROM {tname} WHERE {condition}
+ cur = self.table.execute(sql.SELECT_WHERE.format(cols='*',
+ tname=self.table.name,
+ condition=self.cond))
+ return cur.fetchone()
+
+ def keys(self):
+ """Return the column name of the attribute table."""
+ return self.table.columns.names()
+
+ def commit(self):
+ """Save the changes"""
+ self.table.conn.commit()
+
+
class Geo(object):
"""
>>> geo0 = Geo()
@@ -114,7 +174,8 @@
>>> cats = ctypes.pointer(libvect.line_cats())
>>> geo1 = Geo(c_points=points, c_cats=cats)
"""
- def __init__(self, v_id=None, c_mapinfo=None, c_points=None, c_cats=None):
+ def __init__(self, v_id=None, c_mapinfo=None, c_points=None, c_cats=None,
+ table=None):
self.id = v_id # vector id
self.c_mapinfo = c_mapinfo
@@ -130,6 +191,10 @@
else:
self.c_cats = c_cats
+ # set the attributes
+ if table:
+ self.attrs = Attrs(self.id, table)
+
def is_with_topology(self):
if self.c_mapinfo is not None:
return self.c_mapinfo.contents.level == 2
More information about the grass-commit
mailing list