2

I am trying to create a layer from the selected points coordinates. I am drawing a circle to the coordinate of selected points. I also need to add initial attributes of my selected features to my new layer. I tried some examples but every time I failed. Can anyone help me with this. Need to add something before "provider.addFeatures( [feature] )"

def draw_circle_to_selected_coordinates(self):
    """
    Gets the coordinate of selected features and draws the circles for desired radius in qgis
    """

    #set active layer as layer
    layer = utils.iface.activeLayer()
    selected_features = layer.selectedFeatures()


    #creating layer structure using selected layer fields.
    fields = layer.pendingFields()
    fields_in="Polygon?"
    field_counter=1

    for field in fields:
        fields_in=fields_in+'field='+str(field.name())+ ':'+str(field.typeName())+'&'
        field_counter+=field_counter


    fields_in= fields_in+('field=Band:Double')


    #creates new memory layer for radial bands with selected layer attributes
    vpoly = QgsVectorLayer(fields_in, "RadialBand", "memory")
    feature = QgsFeature()
    provider = vpoly.dataProvider()
    vpoly.startEditing()

    #get band radiuses from the file
    list=[]
    f=open("radial_band.nodelete", "r")
    with open('radial_band.nodelete') as openfileobject:
        for line in openfileobject:
            value=float(line.strip())
            list.append(value)
    f.close()

    #filling radial band table with selected feature coordinates and desired radiuses
    for f in selected_features:
        geom = f.geometry()
        x=float(geom.asPoint().x())
        y=float(geom.asPoint().y())
        item=0
        for item in range(0, len(list)):
            radius=list[item]
    #draw circle to the selected coordinates and radius one by one
            feature.setGeometry( QgsGeometry.fromPoint(QgsPoint(x,y)).buffer(radius,100))

            #need to add something here to get the values from selected features and add the radius value to the Band field
            provider.addFeatures( [feature] )
            item+=item


    vpoly.commitChanges()         
    QgsMapLayerRegistry.instance().addMapLayer(vpoly)

1 Answer 1

3

You can add an attribute field and get its id with:

res = provider.addAttributes( [ QgsField("RADIUS", QVariant.Float) ] )
RADIUS_idx = vlayer.fieldNameIndex('RADIUS')

If, as you requested, want to set up all the attributes BEFORE you add the feature you need to know all the attributes in advance then create them with:

feature.setAttributes([attrib_id0_val,attrib_id1_val, attrib_id2_val ...])

However I think you might prefer to subsiquently alter some attributes after feature creation with:

fid=feature.id()
attrs = { RADIUS_idx : RADIUS_value}
provider.changeAttributeValues({ fid : attrs })
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.