Usage:
>>> selection.update(field1=1, field2="balh")
>>> selection['mylayer'].update(field1=1, field2="balh")
| class Selection(object): | |
| def __init__(self, layer=None): | |
| self._layer = layer | |
| @property | |
| def layer(self): | |
| return self._layer or iface.activeLayer() | |
| def update(self, **kwargs): | |
| debug = kwargs.get('debug', False) | |
| if debug: | |
| del kwargs['debug'] | |
| self.layer.startEditing() | |
| for feature in self: | |
| for field, value in kwargs.iteritems(): | |
| feature[field] = value | |
| if debug: | |
| print "Updated {} with {}".format(feature.id(), str(kwargs)) | |
| self.layer.updateFeature(feature) | |
| def __getitem__(self, attr): | |
| return Selection(QgsMapLayerRegistry.instance().mapLayersByName(attr)[0]) | |
| def __iter__(self): | |
| return iter(self.layer.selectedFeatures()) | |
| def __len__(self): | |
| return self.layer.selectedFeatureCount() | |
| selection = Selection() |