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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 21 00:44:44 PDT 2016


Author: zarch
Date: 2016-06-21 00:44:43 -0700 (Tue, 21 Jun 2016)
New Revision: 68718

Modified:
   grass/trunk/lib/python/pygrass/vector/__init__.py
   grass/trunk/lib/python/pygrass/vector/geometry.py
   grass/trunk/lib/python/pygrass/vector/table.py
Log:
pygrass: Change Table execute method to accept sql with qmark, use qmark in the Attrs setitem method and update the VectorTopo rewrite method to use the same parameters order of the write method

Modified: grass/trunk/lib/python/pygrass/vector/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/__init__.py	2016-06-20 22:25:52 UTC (rev 68717)
+++ grass/trunk/lib/python/pygrass/vector/__init__.py	2016-06-21 07:44:43 UTC (rev 68718)
@@ -527,19 +527,59 @@
         return output
 
     @must_be_open
-    def rewrite(self, line, geo_obj, attrs=None, **kargs):
+    def rewrite(self, geo_obj, cat, attrs=None, **kargs):
         """Rewrite a geometry features
+
+            >>> cols = [(u'cat',       'INTEGER PRIMARY KEY'),
+            ...         (u'name',      'TEXT')]
+
+        Generate a new vector map
+
+            >>> test_vect = VectorTopo(test_vector_name)
+            >>> test_vect.open('w', tab_name='newvect', tab_cols=cols,
+            ...                overwrite=True)
+
+        import a geometry feature ::
+
+            >>> from grass.pygrass.vector.geometry import Point
+
+        create two points ::
+
+            >>> point0 = Point(0, 0)
+            >>> point1 = Point(1, 1)
+            >>> point2 = Point(2, 2)
+
+        then write the two points on the map, with ::
+
+            >>> test_vect.write(point0, cat=1, attrs=('pub',))
+            >>> test_vect.write(point1, cat=2, attrs=('resturant',))
+            >>> test_vect.table.conn.commit()  # save changes in the DB
+            >>> test_vect.close()
+
+        Now rewrite on point of the vector map: ::
+
+            >>> test_vect.open('rw')
+            >>> test_vect.rewrite(point2, cat=1, attrs('Irish Pub'))
+            >>> test_vect.table.conn.commit()  # save changes in the DB
+            >>> test_vect.close()
+
+        Check the output:
+
+            >>> test_vect.open('r')
+            >>> test_vect[1] == point2
+            True
+            >>> test_vect[1].attrs['name'] == 'Irish Pub'
+            True
+            >>> test_vect.close()
         """
         if self.table is not None and attrs:
-            attr = [line, ]
-            attr.extend(attrs)
-            self.table.update(key=line, values=attr)
+            self.table.update(key=cat, values=attrs)
         elif self.table is None and attrs:
             print("Table for vector {name} does not exist, attributes not"
                   " loaded".format(name=self.name))
-        libvect.Vect_cat_set(geo_obj.c_cats, self.layer, line)
+        libvect.Vect_cat_set(geo_obj.c_cats, self.layer, cat)
         result = libvect.Vect_rewrite_line(self.c_mapinfo,
-                                           line, geo_obj.gtype,
+                                           cat, geo_obj.gtype,
                                            geo_obj.c_points,
                                            geo_obj.c_cats)
         if result == -1:

Modified: grass/trunk/lib/python/pygrass/vector/geometry.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/geometry.py	2016-06-20 22:25:52 UTC (rev 68717)
+++ grass/trunk/lib/python/pygrass/vector/geometry.py	2016-06-21 07:44:43 UTC (rev 68718)
@@ -202,13 +202,17 @@
         if self.writeable:
             if np.isscalar(keys):
                 keys, values = (keys, ), (values, )
-
-            vals = ','.join(['%s=%r' % (k, v) for k, v in zip(keys, values)])
+            # check if key is a column of the table or not
+            for key in keys:
+                if key not in self.table.columns:
+                    raise KeyError('Column: %s not in table' % key)
+            # prepare the string using as paramstyle: qmark
+            vals = ','.join(['%s=?' % k for k in keys])
             # "UPDATE {tname} SET {values} WHERE {condition};"
             sqlcode = sql.UPDATE_WHERE.format(tname=self.table.name,
                                               values=vals,
                                               condition=self.cond)
-            self.table.execute(sqlcode)
+            self.table.execute(sqlcode, values=values)
             #self.table.conn.commit()
         else:
             str_err = "You can only read the attributes if the map is in another mapset"

Modified: grass/trunk/lib/python/pygrass/vector/table.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/table.py	2016-06-20 22:25:52 UTC (rev 68717)
+++ grass/trunk/lib/python/pygrass/vector/table.py	2016-06-21 07:44:43 UTC (rev 68718)
@@ -1077,7 +1077,7 @@
             cur = cursor if cursor else self.conn.cursor()
             if many and values:
                 return cur.executemany(sqlc, values)
-            return cur.execute(sqlc)
+            return cur.execute(sqlc, values) if values else cur.execute(sqlc)
         except Exception as exc:
             raise ValueError("The SQL statement is not correct:\n%r,\n"
                              "values: %r,\n"



More information about the grass-commit mailing list