[Qgis-developer] Remove Feature

Alexandre Badez alexandre.badez at gmail.com
Fri Mar 2 04:14:33 EST 2012


Thanks Martin for your help.

On Thu, Mar 1, 2012 at 21:28, Martin Dobias <wonder.sk at gmail.com> wrote:
> The actual implementation is in classes derived from
> QgsVectorDataProvider. Each data provider that allows adding/deleting
> features has its own logic.

Ok... I'm using the memory data provider (I'm creating my layer as
this: layer = core.QgsVectorLayer(uri, name, "memory") )


> Using data providers directly for adding/deleting features should
> work. If it does not, please check return values from the functions
> and provider's result from errors() method. It is also possible that
> the provider you are using does not support adding/deleting - check
> its capabilities first.

I check his capabilities as this:
            self._layer.startEditing()
            dp = self._layer.dataProvider()

            attr = ['NoCapabilities', 'AddFeatures',
             'DeleteFeatures', 'ChangeAttributeValues',
             'AddAttributes', 'DeleteAttributes',
             'SaveAsShapefile', 'CreateSpatialIndex',
             'SelectAtId', 'ChangeGeometries',
             'SelectGeometryAtId', 'RandomSelectGeometryAtId',
             'SequentialSelectGeometryAtId', 'CreateAttributeIndex',
             'SetEncoding']
            cap = dp.capabilities()
            for at in attr:
                logger.debug("capabilities: %s is %s",
                             at,
                             bool(cap & getattr(dp, at))
                             if hasattr(dp, at) else False)

Here is what I have in my log:
	capabilities: NoCapabilities is False
	capabilities: AddFeatures is True
	capabilities: DeleteFeatures is True
	capabilities: ChangeAttributeValues is True
	capabilities: AddAttributes is True
	capabilities: DeleteAttributes is True
	capabilities: SaveAsShapefile is False
	capabilities: CreateSpatialIndex is True
	capabilities: SelectAtId is True
	capabilities: ChangeGeometries is True
	capabilities: SelectGeometryAtId is True
	capabilities: RandomSelectGeometryAtId is False
	capabilities: SequentialSelectGeometryAtId is False
	capabilities: CreateAttributeIndex is False
	capabilities: SetEncoding is False

So I try do delete features as this:
            f = core.QgsFeature()
            fids = []
            while dp.nextFeature(f):
                fids.append(f.id())
            logger.debug("deleting feature: %s", fids)
            if not dp.deleteFeatures(fids):
                # handle error
                for e in dp.errors():
                    logger.critical(e)

What I have in my log:
	deleting feature: []
...but I have features in my layer (!)


> Using QgsVectorLayer's add/delete features functionality requires you
> to first start editing, then do editing calls and finally commit the
> changes. Vector layer has a buffer that records the changes and allows
> you to do undo/redo or rollback all changes. When committing the
> changes the actual add/delete functions from provider are called that
> permanently modify the data.


Ha is that so ?
thanks... it's not really *clear* in the documentation...

> Wait! We have not been using SVN for quite some time! We are on git now:
> https://github.com/qgis/Quantum-GIS

Oo.... thanks...

> Martin

-- 
Alex


More information about the Qgis-developer mailing list