[GRASS-dev] PyGrass: rewrite function

Jesús Fernández Capel jfc at alcd.net
Mon Jul 4 08:37:23 PDT 2016


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 <module>
        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
<https://grass.osgeo.org/programming7/structMap__info.html> *  *Map*,
off_t  *line*, int  *type*, const struct line_pnts
<https://grass.osgeo.org/programming7/structline__pnts.html> *  *points*,
const struct line_cats
<https://grass.osgeo.org/programming7/structline__cats.html> *  *cats*  )
    Parameters
Map pointer to Map_info
<https://grass.osgeo.org/programming7/structMap__info.html> 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         Returnsnew 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
Best regards,
Jesús
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20160704/1eac5c84/attachment.html>


More information about the grass-dev mailing list