1

INTRODUCTION

The code below makes a selection and then saves to an 'Output' file (shapefile?) each time through the "FINAL ITERATION LOOP". Each 'Output' file will contain all selected civic address points (from the "Input" shapefile) on one of the streets assigned to employee Joe Blow (Joe Blow's streets come from the CSV file "FSS_Streets_Test_Sample" and are appended to the "joeblow" list).


QUESTION

But what if I wanted only a single output shapefile (or layer)? One with ALL civic address points on ALL streets in the 'joeblow' list?

Perhaps the trick is to give "selectbyattribute" multiple street names as the final parameter? Is that possible? If not, how do I make multiple selections and essentially concatenate them one after the other until all (z) civic address points on all streets belonging to "Joe Blow" (in the 'joeblow' list) have been highlighted before then saving them in a (single) new shapefile?

  • Tip - most of the script is just 'laying the ground work' such as building lists (e.g. unique list of all streets ("streets"), and streets assigned to "Joe Blow" ("joeblow"). Perhaps you'll prefer to skip to the end, where the two "qgis" tools are executed.

CODE

from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface
import processing

##Input=vector
##Input_Field=field Input

# Assign .CSV of streets assigned to employees, "FSS_Streets_Test_Sample", to variable 'vl' 
vl = QgsMapLayerRegistry.instance().mapLayersByName('FSS_Streets_Test_Sample')[0]

# Set vl as the "Active layer"
iface.setActiveLayer(vl)

# Instantiate 'streets' list
streets = []
# Get List of Unique streets from 'STRNAME' column in 'vl'
for f in vl.getFeatures():
    if f['STRNAME'] not in streets:
            streets.append(f['STRNAME'])

# Instantiate list of streets assigned to "Joe Blow"
joeblow = []
# fill that list with streets belonging to "Joe Blow" as indicated in 'vl'
for f in vl.getFeatures():
    if f['FSS'] == 'Joe Blow':
        joeblow.append(f['STRNAME'])

#Count number of streets belonging to "Joe Blow"
z = len(joeblow)

# FINAL ITERATION LOOP: Iterate through streets belonging to "Joe Blow", each time selecting from the "Input" shapefile, all fields (civic addresses) where the "Input_Field" (STRNAME), equals ("0"), particular street(s) belonging to 'joeblow' (street(s) within the 'joeblow' list)
for k in range(0,z):
    processing.runalg('qgis:selectbyattribute', Input, Input_Field, 0, joeblow[k])
    processing.runalg('qgis:saveselectedfeatures', Input, 'Output')  

# End Script

Notes:

Inputs given to GUI when script is run...

The 'Input' file is a shapefile of civic address points (along streets obviously).

The 'Input_Field' is a column called "STRNAME" within the 'Input' shapefile

The CSV file - "FSS_Streets_Test_Sample" is in the Layers Table of Contents of my Map Project. It basically has 2 columns: employee name ("FSS"), and street name (also called "STRNAME").


Incidentally when the script finishes executing...

a) Civic addresses relating to the last street (but not all streets) in the "joeblow" list are now highlighted on the 'Input' shapefile layer.

b) The GUI disappears without throwing any error messages in the GUI's "Log" Tab (although there are messages in the various tabs of the "Log Messages" window.

c) But... I'm not seeing any 'Output' files (temp layer or shapefile) generated by the "qgis.saveselectedfeatures" tool.

1
  • Could you append FIDs to a list then do one final select by attributes to select anything where the FID is in your list? Commented Oct 29, 2015 at 15:26

1 Answer 1

1

The solution I ended up using was to replace "selectbyattribute" "selectbyexpression" and place it in a loop, each time iterating through my list of streets and adjusting my "expression" accordingly.

processing.runalg('qgis:selectbyexpression', Input, expression, 1)

Setting "1" as the third attribute adds to the current selection.

Your Answer

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

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