I have a script that I had created using ModelBuilder, but our field personnel use QGIS 2.0. Below is a snippet of the code. I am wanting to know if there is an equivalent using pyqgis so that I can add it to my plugin.

# Process: Make Asset Feature Layer
arcpy.MakeFeatureLayer_management(asset_wps, Asset_Selection, "\"ASSET_TYPE\" = 'Secondary Ped' OR \"ASSET_TYPE\" = 'XFMR'")

# Process: Make PC Feature Layer
arcpy.MakeFeatureLayer_management(Proposed_Conductor, PC_Selection, "\"CONDUCTOR_\" = 'Underground Secondary' OR \"CONDUCTOR_\" = 'Underground Service'")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Asset_Selection, "INTERSECT", PC_Selection, "1.5", "NEW_SELECTION")

matchcount = int(arcpy.GetCount_management(Output).getOutput(0)) 
print('E1012: {0}').format(matchcount)

So what I am doing here is just making a selection from two features and then checking where they intersect with that selection and finally getting a count.

How can I replace MakeFeatureLayer and SelectLayerByLocation using PyQGIS?


The following is a way to do what you want :

## define datasource
asset_wps = QgsVectorLayer("path/your/asset/datasource", 'Assets', 'ogr')
proposed_conductor = QgsVectorLayer("path/your/conductor/datasource", 'Proposed Conductor', 'ogr')
## your "where" clauses
where_clause1 = "ASSET_TYPE = 'Secondary Ped' OR ASSET_TYPE = 'XFMR'"
where_clause2 = "CONDUCTOR_ = 'Underground Secondary' OR CONDUCTOR_ = 'Underground Service'"

matchcount = 0
for f1 in asset_wps.getFeatures(QgsFeatureRequest().setFilterExpression(where_clause1)):
    for f2 in proposed_conductor.getFeatures(QgsFeatureRequest().setFilterExpression(where_clause2)):
        ## here get if feature 1 intersects with feature 2
        if f1.geometry().intersects(f2.geometry()):
            matchcount += 1

print('E1012: {0}').format(matchcount)

you can also use setSubsetString directly on QgsVectorLayer instead of QgsFeatureRequest (in this case I'll use double QgsGeometry::distance(QgsGeometry &geom) instead of the intersects function):

asset_wps.setSubsetString("ASSET_TYPE = 'Secondary Ped' OR ASSET_TYPE = 'XFMR'")
proposed_conductor.setSubsetString("CONDUCTOR_ = 'Underground Secondary' OR CONDUCTOR_ = 'Underground Service'")

matchcount = 0
for f1 in asset_wps.getFeatures():
    for f2 in proposed_conductor.getFeatures():
        if f1.geometry().distance(f2.geometry()) <= 1.5:
            matchcount += 1

print('E1012: {0}').format(matchcount)
  • I went with the later suggestion and it worked great. One thing I did add though, was to set the SubsetString back to '' at the end of the code. I found out with my plugin, that the query was applied to my layers I had in the drawing and stayed that way until it was cleared out. Asset.setSubsetString('') ProposedConductor.setSubsetString('') Dec 2 '14 at 13:53

For making in-memory features check out:

Memory Provider

For select layer by location check out Sextante selection methods:


Your Answer

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

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