<div dir="ltr"><div><div>Hi,<br><br></div>The rewrite function return an error if cat is not equal to id. In the example given:<br><br>    test_vector_name = "vector_<span>doctest</span>_map"<br>    cols = [(u'cat', 'INTEGER PRIMARY KEY'), (u'name', 'TEXT')]<br><br>    test_<span>vect</span> = <span>VectorTopo</span>(test_vector_name)<br>    test_<span>vect</span>.open('w', tab_name='newvect', tab_cols=cols, overwrite=True)<br><br>    point0 = Point(0, 0)<br>    point1 = Point(1, 1)<br>    point2 = Point(2, 2)<br><br>    test_<span>vect</span>.write(point0, cat=1, <span>attrs</span>=('pub',))<br>    test_<span>vect</span>.write(point1, cat=2, <span>attrs</span>=('resturant',))<br><br>    test_<span>vect</span>.write(point2, cat=5, <span>attrs</span>=('resturant2',))<br><br>    test_<span>vect</span>.table.<span>conn</span>.commit()  # save changes in the DB<br>    test_<span>vect</span>.close()<br><br>    test_<span>vect</span>.open('r')<br>    print test_<span>vect</span>[1]<br>    print test_<span>vect</span>[2]<br>    print test_<span>vect</span>[3]<br>    test_<span>vect</span>.close()<br><br>    test_<span>vect</span>.open('rw')<br>    test_<span>vect</span>.rewrite(point2, cat=1, <span>attrs</span>=('Irish Pub',)) # this work<br>    test_<span>vect</span>.table.<span>conn</span>.commit()  <br>    test_<span>vect</span>.close()<br><br>    test_<span>vect</span>.open('rw')<br>    test_<span>vect</span>.rewrite(point1, cat=5, <span>attrs</span>=('Irish Pub2',)) # this fail<br>    test_<span>vect</span>.table.<span>conn</span>.commit()  <br>    test_<span>vect</span>.close()<br><br></div>The result is:<br><div><br>    <span>ADVERTENCIA</span>: Attempt to access feature with invalid id (5)<br>    <span>ADVERTENCIA</span>: Unable to rewrite feature/offset 5 in vector map<br>                 <vector_<span>doctest</span>_map><br>    <span>Traceback</span> (most recent call last):<br>      File "<span>ppp</span>.<span>py</span>", line 44, in <module><br>        test_<span>vect</span>.rewrite(point1, cat=5, <span>attrs</span>=('Irish Pub2',))<br>      File "/<span>mnt</span>/<span>trb</span>/grass_<span>src</span>/grass_trunk/dist.x86_64-<span>pc</span>-<span>linux</span>-gnu/etc/python/grass/<span>pygrass</span>/errors.<span>py</span>", line 15, in wrapper<br>        return method(self, *<span>args</span>, **<span>kargs</span>)<br>      File "/<span>mnt</span>/<span>trb</span>/grass_<span>src</span>/grass_trunk/dist.x86_64-<span>pc</span>-<span>linux</span>-gnu/etc/python/grass/<span>pygrass</span>/vector/__<span>init</span>__.<span>py</span>", line 586, in rewrite<br>        raise <span>GrassError</span>("Not able to write the vector feature.")<br>    grass.exceptions.<span>GrassError</span>: Not able to write the vector feature.<br><br><br>The rewrite function call to <span>Vect</span>_rewrite_line c function. This fail because rewrite function send the cat=5 and <span>Vect</span>_rewrite_line <span lang="en"><span>search</span></span> for feature id=5, when for cat=5 the id=3.<br><br>    def rewrite(self, <span>geo</span>_obj, cat, <span>attrs</span>=None, **<span>kargs</span>):<br>        """Rewrite a geometry features  ...<br>        """<br>        if self.table is not None and <span>attrs</span>:<br>            self.table.update(key=cat, values=<span>attrs</span>)<br>        <span>elif</span> self.table is None and <span>attrs</span>:<br>            print("Table for vector {name} does not exist, attributes not"<br>                  " loaded".format(name=<a href="http://self.name" target="_blank">self.name</a>))<br>        <span>libvect</span>.<span>Vect</span>_cat_set(<span>geo</span>_obj.c_cats, self.layer, cat)<br>        result = <span>libvect</span>.<span>Vect</span>_rewrite_line(self.c_<span>mapinfo</span>,<br>                                           cat, <span>geo</span>_obj.<span>gtype</span>,  # Call with cat no with id<br>                                           <span>geo</span>_obj.c_points,<br>                                           <span>geo</span>_obj.c_cats)<br>        if result == -1:<br>            raise <span>GrassError</span>("Not able to write the vector feature.")<br><br>        # return offset into file where the feature starts<br>        <span>geo</span>_obj.offset = result<br><br><br></div><div>The <span>Vect</span>_rewrite_line c <span>funtion</span> is:<br><br>    <span>Vect</span>_rewrite_line 
          (
          <span>struct</span> <a href="https://grass.osgeo.org/programming7/structMap__info.html" target="_blank">Map_info</a> * 
          <i>Map</i>, off_t 
          <i>line</i>, int 
          <i>type</i>, <span>const</span> <span>struct</span> <a href="https://grass.osgeo.org/programming7/structline__pnts.html" target="_blank">line_<span>pnts</span></a> * 
          <i>points</i>, <span>const</span> <span>struct</span> <a href="https://grass.osgeo.org/programming7/structline__cats.html" target="_blank">line_cats</a> * 
          <i>cats</i> 
        
        
          
          )<dl><dt>        Parameters</dt><dd>
  <table>
    <tbody><tr><td>Map</td><td>pointer to <a href="https://grass.osgeo.org/programming7/structMap__info.html" title="Vector map info. " target="_blank">Map_info</a> structure </td></tr>
    <tr><td>line</td><td>feature id (level 2) or feature offset (level 1)  # Here line is feature id<br></td></tr>
    <tr><td>type</td><td>feature type (<span>GV</span>_POINT, <span>GV</span>_LINE, ...) </td></tr>
    <tr><td>points</td><td>feature geometry </td></tr>
    <tr><td>cats</td><td>feature categories</td></tr>
  </tbody></table>
  </dd></dl>
<dl><dt>        Returns</dt><dd>new feature id (on level 2) (or 0 when build level < <span>GV</span>_BUILD_BASE) </dd><dd>
offset into file where the feature starts (on level 1) </dd><dd>
-1 on error </dd></dl></div><div>Best regards,<br></div><div>Jesús<br></div><div><br></div></div>