<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.30.3">
</HEAD>
<BODY>
Hello,<BR>
<BR>
I am not sure whether this is the right forum but extensive Google searches have not helped. I am writing a plugin to convert the segments of a polyline to a layer of individual 2 vertex line features.<BR>
<BR>
This is the code I used to do the job<BR>
<BR>
feat = QgsFeature()<BR>
p = 0<BR>
while p < cl.featureCount():<BR>
cl.featureAtId(p, feat, True, True)<BR>
geom0 = QgsGeometry(feat.geometry())<BR>
vl = QgsVectorLayer("LineString", "Mylines", "memory")<BR>
provider = vl.dataProvider()<BR>
provider.addAttributes( [ QgsField( "id" , QVariant.String), QgsField( "agl" , QVariant.String) ])<BR>
pnt10 = QgsPoint( geom0.vertexAt(0) )<BR>
segfeat = QgsFeature()<BR>
itr = 1<BR>
while ( QgsPoint( geom0.vertexAt(itr) ) <> QgsPoint(0,0) ):<BR>
pnt10 = QgsPoint( geom0.vertexAt(itr -1) )<BR>
pnt11 = QgsPoint( geom0.vertexAt(itr) )<BR>
newGeom = QgsGeometry.fromPolyline( [ pnt10, pnt11 ] )<BR>
segfeat.setGeometry( newGeom )<BR>
segfeat.setAttributeMap( { 0 : QVariant( str(itr) ), 1 : QVariant( str(agl) ) } )<BR>
provider.addFeatures( [segfeat] )<BR>
itr += 1<BR>
p += 1<BR>
vl.commitChanges()<BR>
QgsMapLayerRegistry.instance().addMapLayer(vl)<BR>
<BR>
It appears to work fine until I need to make use of the data in the attributes.<BR>
<BR>
To do this I use the following code:<BR>
<BR>
...<BR>
p = 0<BR>
while p < self.cl.featureCount():<BR>
feat = QgsFeature()<BR>
self.cl.featureAtId(p, feat, True, True)<BR>
<BR>
attmap = feat.attributeMap()<BR>
attid = attmap[0].toString()<BR>
agl = attmap[1].toString()<BR>
geom0 = QgsGeometry(feat.geometry())<BR>
pnt10 = QgsPoint(geom0.vertexAt(0))<BR>
<BR>
...<BR>
This code works fine on features that I create manually through the QGIS user interface. But I get an error on the line with layers which I have created above<BR>
The error is raised by the line <BR>
<BR>
attmap = feat.attributeMap()<BR>
The error appears to be because the featureAtId function is returning null objects and I think it is because the way I am using the provider does not create feature id's in an orderly fashion.<BR>
<BR>
Please help.<BR>
Thanks <BR>
Gary
</BODY>
</HTML>