attributeValueBug.py

Stig Henning Fredheim, 2012-07-25 09:56 AM

Download (1.32 KB)

 
1
# creates two features with the add feature tool
2
layer = qgis.utils.iface.activeLayer()
3

    
4

    
5
# manual interaction !
6
# select the first feature with the "select features be rectangle"-tool and then do the following line:
7
featId1 = layer.selectedFeaturesIds()[0]
8

    
9
# add values  to the first feature
10
for i in range(5):
11
    layer.changeAttributeValue(featId1, i, (i*10))
12

    
13
# manual interaction !
14
# select the second feature with the "select features be rectangle"-tool and then do the following line:
15
featId2 = layer.selectedFeaturesIds()[0]
16

    
17
# changes the geometry of the second feature
18
newGeom = QgsGeometry.fromPolyline([QgsPoint(1,1), QgsPoint(2,2)])
19
layer.changeGeometry(featId2, newGeom)
20
qgis.utils.iface.mapCanvas().refresh ()
21

    
22
# add values  to the second feature
23
for i in range(5):
24
    layer.changeAttributeValue(featId2, i, (i*10 + 1000))
25

    
26

    
27
# get the second feature by using featureAtId...    
28
feat = QgsFeature()
29
layer.featureAtId(featId2, feat, True, True)
30

    
31
# print values from the second feature
32
for i in range(5):
33
    print feat[i].toString()
34

    
35
# for some reason the attributeValues to the first feature get printed out. 
36
# in the attribute table it looks correct. 
37
# feat.geometry().asPolyline() seems to return the right geometry
38
# if I save edits after creating the two new features it works as it should