1

I'm a beginner in PyQGIS and need help to figure out how to automate a process in QGIS.

I want to write a script that enables me to loop inside a polygon shapefile (shown in brown) and use extent of each polygon to clip another shapefile (shown in yellowish).

Below is what I figured out so far:

    loc_brown= 'D:/Python_Calcs/SHP_PR/brown.shp'
    loc_yellow = 'D:/Python_Calcs/SHP_PR/yellow.shp'

    layer_brown = QgsVectorLayer(loc_brown,'','ogr') 
    layer_yellow = QgsVectorLayer(loc_yellow,'','ogr') 

            for feat in layer_brown.getFeatures():
                    attrs = feat.attributes()
                    print(attrs[0])
                    clip = processing.run("native:clip",'INPUT':layer_yellow,'OVERLAY':????,'OUTPUT':"D:/Python_Calcs/tmp/"+"clip_"+str(attrs[0])+".shp"})

Vector_To_Use_For_Clip_Extent Vector_To_Clip

  • Would be easier to Intersect then Split Vector Layer – BERA Jun 23 at 9:11
2

it should be something like this one, it selects every feature before running the algorithm:

loc_brown= 'D:/Python_Calcs/SHP_PR/brown.shp'
loc_yellow = 'D:/Python_Calcs/SHP_PR/yellow.shp'

layer_brown = QgsVectorLayer(loc_brown,'','ogr') 
layer_yellow = QgsVectorLayer(loc_yellow,'','ogr') 

for feat in layer_brown.getFeatures():
    attrs = feat.attributes()
    print(attrs[0])
    alg_params = {'INPUT': layer_yellow,
            'OVERLAY' : layer_brown,
            'OUTPUT': 'D:/Python_Calcs/tmp/'+'clip_'+str(attrs[0])+'.shp'}    
    layer_brown.selectByIds([feat.id()]) 
    result = processing.run("native:clip", alg_params)
| improve this answer | |

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.