Hi,
The rewrite function return an error if cat is not equal to id. In the example given:
test_vector_name = “vector_doctest_map”
cols = [(u’cat’, ‘INTEGER PRIMARY KEY’), (u’name’, ‘TEXT’)]
test_vect = VectorTopo(test_vector_name)
test_vect.open(‘w’, tab_name=‘newvect’, tab_cols=cols, overwrite=True)
point0 = Point(0, 0)
point1 = Point(1, 1)
point2 = Point(2, 2)
test_vect.write(point0, cat=1, attrs=(‘pub’,))
test_vect.write(point1, cat=2, attrs=(‘resturant’,))
test_vect.write(point2, cat=5, attrs=(‘resturant2’,))
test_vect.table.conn.commit() # save changes in the DB
test_vect.close()
test_vect.open(‘r’)
print test_vect[1]
print test_vect[2]
print test_vect[3]
test_vect.close()
test_vect.open(‘rw’)
test_vect.rewrite(point2, cat=1, attrs=(‘Irish Pub’,)) # this work
test_vect.table.conn.commit()
test_vect.close()
test_vect.open(‘rw’)
test_vect.rewrite(point1, cat=5, attrs=(‘Irish Pub2’,)) # this fail
test_vect.table.conn.commit()
test_vect.close()
The result is:
ADVERTENCIA: Attempt to access feature with invalid id (5)
ADVERTENCIA: Unable to rewrite feature/offset 5 in vector map
<vector_doctest_map>
Traceback (most recent call last):
File “ppp.py”, line 44, in
test_vect.rewrite(point1, cat=5, attrs=(‘Irish Pub2’,))
File “/mnt/trb/grass_src/grass_trunk/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/errors.py”, line 15, in wrapper
return method(self, *args, **kargs)
File “/mnt/trb/grass_src/grass_trunk/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/vector/init.py”, line 586, in rewrite
raise GrassError(“Not able to write the vector feature.”)
grass.exceptions.GrassError: Not able to write the vector feature.
The rewrite function call to Vect_rewrite_line c function. This fail because rewrite function send the cat=5 and Vect_rewrite_line search for feature id=5, when for cat=5 the id=3.
def rewrite(self, geo_obj, cat, attrs=None, **kargs):
“”“Rewrite a geometry features …
“””
if self.table is not None and attrs:
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, cat)
result = libvect.Vect_rewrite_line(self.c_mapinfo,
cat, geo_obj.gtype, # Call with cat no with id
geo_obj.c_points,
geo_obj.c_cats)
if result == -1:
raise GrassError(“Not able to write the vector feature.”)
return offset into file where the feature starts
geo_obj.offset = result
The Vect_rewrite_line c funtion is:
Vect_rewrite_line ( struct Map_info * Map, off_t line, int type, const struct line_pnts * points, const struct line_cats * cats )
- Parameters
-
Map pointer to Map_info structure line feature id (level 2) or feature offset (level 1) # Here line is feature id type feature type (GV_POINT, GV_LINE, …) points feature geometry cats feature categories
- Returns
- new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
- offset into file where the feature starts (on level 1)
- -1 on error
Jesús