本文整理汇总了Python中qgis.core.QgsFeature.changeAttribute方法的典型用法代码示例。如果您正苦于以下问题:Python QgsFeature.changeAttribute方法的具体用法?Python QgsFeature.changeAttribute怎么用?Python QgsFeature.changeAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsFeature
的用法示例。
在下文中一共展示了QgsFeature.changeAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ChangeAttribute
# 需要导入模块: from qgis.core import QgsFeature [as 别名]
# 或者: from qgis.core.QgsFeature import changeAttribute [as 别名]
def test_ChangeAttribute(self):
feat = QgsFeature()
feat.addAttribute(1, "text")
feat.changeAttribute(1, "changed")
myChangedAttribute = feat.attributeMap()[1].toString()
myExpectedAttribute = "changed"
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedAttribute,
myChangedAttribute)
assert myChangedAttribute == myExpectedAttribute, myMessage
示例2: _clip_vector_layer
# 需要导入模块: from qgis.core import QgsFeature [as 别名]
# 或者: from qgis.core.QgsFeature import changeAttribute [as 别名]
#.........这里部分代码省略.........
myMessage = tr('Could not obtain data provider from '
'layer "%s"' % layer.source())
raise Exception(myMessage)
# Get the layer field list, select by our extent then write to disk
# .. todo:: FIXME - for different geometry types we should implement
# different clipping behaviour e.g. reject polygons that
# intersect the edge of the bbox. Tim
myAttributes = myProvider.attributeIndexes()
myFetchGeometryFlag = True
myUseIntersectFlag = True
myProvider.select(
myAttributes,
myProjectedExtent,
myFetchGeometryFlag,
myUseIntersectFlag)
myFieldList = myProvider.fields()
myWriter = QgsVectorFileWriter(
myFilename,
'UTF-8',
myFieldList,
layer.wkbType(),
myGeoCrs,
#'SQLite') # FIXME (Ole): This works but is far too slow
'ESRI Shapefile')
if myWriter.hasError() != QgsVectorFileWriter.NoError:
myMessage = tr('Error when creating shapefile: <br>Filename:'
'%s<br>Error: %s' %
(myFilename, myWriter.hasError()))
raise Exception(myMessage)
# Reverse the coordinate xform now so that we can convert
# geometries from layer crs to geocrs.
myXForm = QgsCoordinateTransform(layer.crs(), myGeoCrs)
# Retrieve every feature with its geometry and attributes
myFeature = QgsFeature()
myCount = 0
myHasMultipart = False
if explode_attribute is not None:
theExplodeAttributeIndex = myProvider.fieldNameIndex(
explode_attribute)
while myProvider.nextFeature(myFeature):
myGeometry = myFeature.geometry()
if explode_attribute is not None:
myAttrs = myFeature.attributeMap()
# Loop through the parts adding them to the output file
# we write out single part features unless explode_flag is False
if explode_flag:
myGeometryList = explode_multipart_geometry(myGeometry)
else:
myGeometryList = [myGeometry]
for myPartIndex, myPart in enumerate(myGeometryList):
myPart.transform(myXForm)
if hard_clip_flag:
# Remove any dangling bits so only intersecting area is
# kept.
myPart = clip_geometry(myClipPolygon, myPart)
if myPart is None:
continue
myFeature.setGeometry(myPart)
# There are multiple parts and we want to show it in the
# explode_attribute
if myPartIndex > 0 and explode_attribute is not None:
myHasMultipart = True
myPartAttr = QVariant(
'%s #%s' % (myAttrs[theExplodeAttributeIndex].toString(),
myPartIndex))
myFeature.changeAttribute(theExplodeAttributeIndex, myPartAttr)
myWriter.addFeature(myFeature)
myCount += 1
del myWriter # Flush to disk
if myCount < 1:
myMessage = tr(
'No features fall within the clip extents. Try panning / zooming '
'to an area containing data and then try to run your analysis '
'again. If hazard and exposure data doesn\'t overlap at all, it '
'is not possible to do an analysis. Another possibility is that '
'the layers do overlap but because they may have different '
'spatial references, they appear to be disjointed. If this is the '
'case, try to turn on reproject on-the-fly in QGIS.')
raise NoFeaturesInExtentError(myMessage)
myKeywordIO = KeywordIO()
if extra_keywords is None:
extra_keywords = {}
extra_keywords['HAD_MULTIPART_POLY'] = myHasMultipart
myKeywordIO.copy_keywords(
layer, myFilename, extra_keywords=extra_keywords)
myBaseName = '%s clipped' % layer.name()
myLayer = QgsVectorLayer(myFilename, myBaseName, 'ogr')
return myLayer