I am trying to create a script that will take selected features from one layer and search for the specified attributes related to a column name. in my case 'id'. What I am having trouble doing right now is writing the script to tell qgis to take the selected 'id''s and write them to a new attribute table under a specified field. Below is my current script.

from qgis.gui import QgsMapTool
print 'imported'
print 'Attribute table shown'

# define variables
layer = iface.activeLayer()
feature = layer.getFeatures()
selection = layer.selectedFeatures()
# prints the number of attributes the user selected.
print len(selection)

# for loop to identify trail_stuctures from attribute table
for feature in selection:
    print feature['id']
output_file = open('c:/Users/Pulse3/Desktop/plugin.txt', 'w')

# Writing selection id's to a text file stored on the desktop
for selection in layer.selectedFeatures():
  line = '%s\n' % (selection['id'])
  unicode_line = line.encode('utf-8')

The above script is only one of the two scripts. Right now the user has to click on the layer through the layer panel. manually go into the attribute table, and use the select feature tool to select a series of lines. ( which are my spans.) Only the selected lines are used in this script.

Ultimatly id like the script to be able to look at seleced features in one layer and Write the 'id' into a standalone table. then switch to another layer and look at the user selected features of another layer and do the same thing.


Two layers:



Both layers will have a selection done based on a user selected features with the mouse.

Both layers will also need to be able to take the returned attribute values list and write it into two different field within a seperate stand alone table.

Essentially I am designing fiber internet drop maps and we may have service points which are houses. that each have a unique ID number and then the spans which are lines off the main backbone fiber line that also have unique id's. both of these features are in their own layers.

I am trying to write a script that allows me to extract just the Id from the service point and then the user selected spans and fills in new geometry attribute table with the same service point id for each of the spans that feed the service point and then the span id for each of the span ids that service the service points.

Is there a way to do this in QGIS?

  • This is a bit unclear. What is an "active layer" in a python script? Python scripts can just get any layer from the loaded layers and do things with it. What are these span IDs and terminal IDs? They mean nothing to us. Paragraph 4 talks about "auto populate" which is not clear and mentions two scripts. Where did this second script come from? What does that do? Try and explain clearly and concisely (which means cut all the irrelevant bits), with examples, your input data and your desired output data. As it is this question is unanswerable. – Spacedman Jun 15 '16 at 17:11
  • I revised my question. for my purpose I needed to be able to select features on the map canvas and to do so manually i had to have a layer active meaning the layer is underlined in the layers panel. I dont know how to bypass this as I am familiar with Arcpy for Arc map even more and could write the code for what i want done in an hours time. Ive only ever worked with QGIS for about the last 3 weeks so the python syntax is totally different in how layers are treated and objects are classified. Any input would be helpful. – arspragu Jun 20 '16 at 12:07
  • 1
    Welcome to gis.stackexchange! Please edit the title of your question to include enough information for future visitors to be able to find this thread when looking for the same problem. – underdark Sep 2 '16 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.