فهرست منبع

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

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@68718 15284696-431f-4ddb-bdfa-cd5b030d7da7
Pietro Zambelli 8 سال پیش
والد
کامیت
5d22fe3003
3فایلهای تغییر یافته به همراه54 افزوده شده و 10 حذف شده
  1. 46 6
      lib/python/pygrass/vector/__init__.py
  2. 7 3
      lib/python/pygrass/vector/geometry.py
  3. 1 1
      lib/python/pygrass/vector/table.py

+ 46 - 6
lib/python/pygrass/vector/__init__.py

@@ -527,19 +527,59 @@ class VectorTopo(Vector):
         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:

+ 7 - 3
lib/python/pygrass/vector/geometry.py

@@ -202,13 +202,17 @@ class Attrs(object):
         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"

+ 1 - 1
lib/python/pygrass/vector/table.py

@@ -1077,7 +1077,7 @@ class Table(object):
             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"