Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Created January 8, 2015 23:28
Show Gist options
  • Select an option

  • Save NathanW2/c76a34cb66b87dfcc573 to your computer and use it in GitHub Desktop.

Select an option

Save NathanW2/c76a34cb66b87dfcc573 to your computer and use it in GitHub Desktop.
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()

Usage:

>>> selection.update(field1=1, field2="balh")
>>> selection['mylayer'].update(field1=1, field2="balh")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment