本文整理汇总了Python中qgis.core.QgsVectorLayer.updateFeature方法的典型用法代码示例。如果您正苦于以下问题:Python QgsVectorLayer.updateFeature方法的具体用法?Python QgsVectorLayer.updateFeature怎么用?Python QgsVectorLayer.updateFeature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsVectorLayer
的用法示例。
在下文中一共展示了QgsVectorLayer.updateFeature方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_flooded_field
# 需要导入模块: from qgis.core import QgsVectorLayer [as 别名]
# 或者: from qgis.core.QgsVectorLayer import updateFeature [as 别名]
def add_flooded_field(self, shapefile_path):
"""Create the layer from the local shp adding the flooded field.
.. versionadded:: 3.3
Use this method to add a calculated field to a shapefile. The shapefile
should have a field called 'count' containing the number of flood
reports for the field. The field values will be set to 0 if the count
field is < 1, otherwise it will be set to 1.
:param shapefile_path: Path to the shapefile that will have the flooded
field added.
:type shapefile_path: basestring
:return: A vector layer with the flooded field added.
:rtype: QgsVectorLayer
"""
layer = QgsVectorLayer(
shapefile_path, self.tr('Jakarta Floods'), 'ogr')
# Add a calculated field indicating if a poly is flooded or not
# from PyQt4.QtCore import QVariant
layer.startEditing()
field = QgsField('flooded', QVariant.Int)
layer.dataProvider().addAttributes([field])
layer.commitChanges()
layer.startEditing()
idx = layer.fieldNameIndex('flooded')
expression = QgsExpression('state > 0')
expression.prepare(layer.pendingFields())
for feature in layer.getFeatures():
feature[idx] = expression.evaluate(feature)
layer.updateFeature(feature)
layer.commitChanges()
return layer
示例2: add_flooded_field
# 需要导入模块: from qgis.core import QgsVectorLayer [as 别名]
# 或者: from qgis.core.QgsVectorLayer import updateFeature [as 别名]
def add_flooded_field(self, shapefile_path):
"""Create the layer from the local shp adding the flooded field.
.. versionadded:: 3.3
Use this method to add a calculated field to a shapefile. The shapefile
should have a field called 'count' containing the number of flood
reports for the field. The field values will be set to 0 if the count
field is < 1, otherwise it will be set to 1.
:param shapefile_path: Path to the shapefile that will have the flooded
field added.
:type shapefile_path: basestring
:return: A vector layer with the flooded field added.
:rtype: QgsVectorLayer
"""
layer = QgsVectorLayer(
shapefile_path, self.tr('Jakarta Floods'), 'ogr')
# Add a calculated field indicating if a poly is flooded or not
# from qgis.PyQt.QtCore import QVariant
layer.startEditing()
# Add field with integer from 0 to 4 which represents the flood
# class. Its the same as 'state' field except that is being treated
# as a string.
# This is used for cartography
flood_class_field = QgsField('floodclass', QVariant.Int)
layer.addAttribute(flood_class_field)
layer.commitChanges()
layer.startEditing()
flood_class_idx = layer.fields().lookupField('floodclass')
flood_class_expression = QgsExpression('to_int(state)')
context = QgsExpressionContext()
context.setFields(layer.fields())
flood_class_expression.prepare(context)
# Add field with boolean flag to say if the area is flooded
# This is used by the impact function
flooded_field = QgsField('flooded', QVariant.Int)
layer.dataProvider().addAttributes([flooded_field])
layer.commitChanges()
layer.startEditing()
flooded_idx = layer.fields().lookupField('flooded')
flood_flag_expression = QgsExpression('state > 0')
flood_flag_expression.prepare(context)
for feature in layer.getFeatures():
context.setFeature(feature)
feature[flood_class_idx] = flood_class_expression.evaluate(context)
feature[flooded_idx] = flood_flag_expression.evaluate(context)
layer.updateFeature(feature)
layer.commitChanges()
return layer
示例3: checkCartoDBId
# 需要导入模块: from qgis.core import QgsVectorLayer [as 别名]
# 或者: from qgis.core.QgsVectorLayer import updateFeature [as 别名]
def checkCartoDBId(layer, convert=False):
"""Check if layer has cartodb_id field"""
new_layer = layer
if convert and layer.fieldNameIndex('cartodb_id') == -1:
checkTempDir()
temp = tempfile.NamedTemporaryFile()
error = QgsVectorFileWriter.writeAsVectorFormat(layer, temp.name, 'utf-8', None, 'ESRI Shapefile')
if error == QgsVectorFileWriter.NoError:
new_layer = QgsVectorLayer(temp.name + '.shp', layer.name(), 'ogr')
new_layer.dataProvider().addAttributes([QgsField('cartodb_id', QVariant.Int)])
new_layer.updateFields()
features = new_layer.getFeatures()
i = 1
for feature in features:
fid = feature.id()
aid = new_layer.fieldNameIndex('cartodb_id')
attrs = {aid: i}
new_layer.dataProvider().changeAttributeValues({fid : attrs})
i = i + 1
new_layer.updateFeature(feature)
return new_layer
示例4: testEdit
# 需要导入模块: from qgis.core import QgsVectorLayer [as 别名]
# 或者: from qgis.core.QgsVectorLayer import updateFeature [as 别名]
def testEdit(self):
"""Test `with edit(layer):` code"""
ml = QgsVectorLayer("Point?crs=epsg:4236&field=id:integer&field=value:double",
"test_data", "memory")
# Data as list of x, y, id, value
self.assertTrue(ml.isValid())
fields = ml.fields()
# Check insert
with edit(ml):
feat = QgsFeature(fields)
feat['id'] = 1
feat['value'] = 0.9
self.assertTrue(ml.addFeature(feat))
self.assertEqual(next(ml.dataProvider().getFeatures())['value'], 0.9)
# Check update
with edit(ml):
f = next(ml.getFeatures())
f['value'] = 9.9
self.assertTrue(ml.updateFeature(f))
self.assertEqual(next(ml.dataProvider().getFeatures())['value'], 9.9)
# Check for rollBack after exceptions
with self.assertRaises(NameError):
with edit(ml):
f = next(ml.getFeatures())
f['value'] = 3.8
crashycrash() # NOQA
self.assertEqual(next(ml.dataProvider().getFeatures())['value'], 9.9)
self.assertEqual(next(ml.getFeatures())['value'], 9.9)
# Check for `as`
with edit(ml) as l:
f = next(l.getFeatures())
f['value'] = 10
self.assertTrue(l.updateFeature(f))
self.assertEqual(next(ml.dataProvider().getFeatures())['value'], 10)
# Check that we get a QgsEditError exception when the commit fails
with self.assertRaises(QgsEditError):
with edit(ml) as l:
l.rollBack()