[GRASS-SVN] r55447 - grass/trunk/lib/python/pygrass/vector

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Mar 19 08:24:06 PDT 2013


Author: zarch
Date: 2013-03-19 08:24:06 -0700 (Tue, 19 Mar 2013)
New Revision: 55447

Modified:
   grass/trunk/lib/python/pygrass/vector/__init__.py
   grass/trunk/lib/python/pygrass/vector/geometry.py
Log:
Move some geometry code from __init__ to geometry

Modified: grass/trunk/lib/python/pygrass/vector/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/__init__.py	2013-03-19 15:06:56 UTC (rev 55446)
+++ grass/trunk/lib/python/pygrass/vector/__init__.py	2013-03-19 15:24:06 UTC (rev 55447)
@@ -4,17 +4,16 @@
 
 @author: pietro
 """
-import ctypes
-
 import grass.lib.vector as libvect
-from vector_type import VTYPE, GV_TYPE
+from vector_type import VTYPE
 
 #
 # import pygrass modules
 #
 from grass.pygrass.errors import GrassError, must_be_open
 
-import geometry
+from geometry import GEOOBJ as _GEOOBJ
+from geometry import read_line, read_next_line
 from abstract import Info
 from basic import Bbox
 
@@ -32,19 +31,7 @@
           "updated_nodes": libvect.Vect_get_num_updated_nodes,
           "volumes": libvect.Vect_get_num_volumes}
 
-_GEOOBJ = {"areas": geometry.Area,
-           "dblinks": None,
-           "faces": None,
-           "holes": None,
-           "islands": geometry.Isle,
-           "kernels": None,
-           "line_points": None,
-           "points": geometry.Point,
-           "lines": geometry.Line,
-           "nodes": geometry.Node,
-           "volumes": None}
 
-
 #=============================================
 # VECTOR
 #=============================================
@@ -109,22 +96,7 @@
 
         ..
         """
-        v_id = self.c_mapinfo.contents.next_line
-        v_id = v_id if v_id != 0 else None
-        c_points = ctypes.pointer(libvect.line_pnts())
-        c_cats = ctypes.pointer(libvect.line_cats())
-        ftype = libvect.Vect_read_next_line(self.c_mapinfo, c_points, c_cats)
-        if ftype == -2:
-            raise StopIteration()
-        if ftype == -1:
-            raise
-        #if  GV_TYPE[ftype]['obj'] is not None:
-        return GV_TYPE[ftype]['obj'](v_id=v_id,
-                                     c_mapinfo=self.c_mapinfo,
-                                     c_points=c_points,
-                                     c_cats=c_cats,
-                                     table=self.table,
-                                     writable=self.writable)
+        return read_next_line(self.c_mapinfo, self.table, self.writable)
 
     @must_be_open
     def rewind(self):
@@ -206,8 +178,6 @@
             geo_obj.offset = result
 
 
-
-
 #=============================================
 # VECTOR WITH TOPOLOGY
 #=============================================
@@ -407,24 +377,7 @@
 
         ..
         """
-        if feature_id < 0:  # Handle negative indices
-                feature_id += self.__len__() + 1
-        if feature_id > (self.__len__()):
-            raise IndexError('Index out of range')
-        if feature_id > 0:
-            c_points = ctypes.pointer(libvect.line_pnts())
-            c_cats = ctypes.pointer(libvect.line_cats())
-            ftype = libvect.Vect_read_line(self.c_mapinfo, c_points,
-                                           c_cats, feature_id)
-            if  GV_TYPE[ftype]['obj'] is not None:
-                return GV_TYPE[ftype]['obj'](v_id=feature_id,
-                                             c_mapinfo=self.c_mapinfo,
-                                             c_points=c_points,
-                                             c_cats=c_cats,
-                                             table=self.table,
-                                             writable=self.writable)
-        else:
-            raise ValueError('The index must be >0, %r given.' % feature_id)
+        return read_line(feature_id, self.c_mapinfo, self.table, self.writable)
 
     @must_be_open
     def is_empty(self):

Modified: grass/trunk/lib/python/pygrass/vector/geometry.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/geometry.py	2013-03-19 15:06:56 UTC (rev 55446)
+++ grass/trunk/lib/python/pygrass/vector/geometry.py	2013-03-19 15:24:06 UTC (rev 55447)
@@ -127,14 +127,10 @@
 
 
 class Attrs(object):
-    def __init__(self, cat, table, writable=None):
+    def __init__(self, cat, table, writable=False):
         self.cat = cat
         self.table = table
         self.cond = "%s=%d" % (self.table.key, self.cat)
-        # TODO add 
-        #if writable == None:
-        #    self.writable
-        #else:
         self.writable = writable
 
     def __getitem__(self, key):
@@ -221,7 +217,7 @@
            >>> attrs.keys()                             # doctest: +ELLIPSIS
            (u'cat',
            ...
-           u'NOTES')        
+           u'NOTES')
         """
         return self.table.columns.names()
 
@@ -1019,6 +1015,7 @@
 class Node(object):
     pass
 
+
 class Boundary(Line):
     """
     """
@@ -1407,3 +1404,68 @@
         """
         border = self.points()
         return libvect.Vect_area_perimeter(border.c_points)
+
+
+#
+# Define a dictionary to convert the feature type to name and or object
+#
+
+GV_TYPE = {libvect.GV_POINT:    {'label': 'point',    'obj': Point},
+           libvect.GV_LINE:     {'label': 'line',     'obj': Line},
+           libvect.GV_BOUNDARY: {'label': 'boundary', 'obj': Boundary},
+           libvect.GV_CENTROID: {'label': 'centroid', 'obj': Centroid},
+           libvect.GV_FACE:     {'label': 'face',     'obj': None},
+           libvect.GV_KERNEL:   {'label': 'kernel',   'obj': None},
+           libvect.GV_AREA:     {'label': 'area',     'obj': Area},
+           libvect.GV_VOLUME:   {'label': 'volume',   'obj': None}, }
+
+GEOOBJ = {"areas": Area,
+          "dblinks": None,
+          "faces": None,
+          "holes": None,
+          "islands": Isle,
+          "kernels": None,
+          "line_points": None,
+          "points": Point,
+          "lines": Line,
+          "nodes": Node,
+          "volumes": None}
+
+
+def read_next_line(c_mapinfo, table=None, writable=False):
+    """Return the next geometry feature of a vector map."""
+    v_id = c_mapinfo.contents.next_line
+    v_id = v_id if v_id != 0 else None
+    c_points = ctypes.pointer(libvect.line_pnts())
+    c_cats = ctypes.pointer(libvect.line_cats())
+    ftype = libvect.Vect_read_next_line(c_mapinfo, c_points, c_cats)
+    if ftype == -2:
+        raise StopIteration()
+    if ftype == -1:
+        raise
+    #if  GV_TYPE[ftype]['obj'] is not None:
+    return GV_TYPE[ftype]['obj'](v_id=v_id, c_mapinfo=c_mapinfo,
+                                 c_points=c_points, c_cats=c_cats,
+                                 table=table, writable=writable)
+
+
+def read_line(feature_id, c_mapinfo, table=None, writable=False):
+    """Return a geometry object given the feature id and the c_mapinfo.
+    """
+    nmax = libvect.Vect_get_num_lines(c_mapinfo)
+    if feature_id < 0:  # Handle negative indices
+            feature_id += nmax + 1
+    if feature_id > nmax:
+        raise IndexError('Index out of range')
+    if feature_id > 0:
+        c_points = ctypes.pointer(libvect.line_pnts())
+        c_cats = ctypes.pointer(libvect.line_cats())
+        ftype = libvect.Vect_read_line(c_mapinfo, c_points, c_cats, feature_id)
+        if  GV_TYPE[ftype]['obj'] is not None:
+            return GV_TYPE[ftype]['obj'](v_id=feature_id, c_mapinfo=c_mapinfo,
+                                         c_points=c_points, c_cats=c_cats,
+                                         table=table, writable=writable)
+    else:
+        raise ValueError('The index must be >0, %r given.' % feature_id)
+
+



More information about the grass-commit mailing list