I have a line layer (roads) and a polygon layer which represents districts/counties. I want to clip the roads according to every one different county. That means that I must repeat the procedure sixty times as the total count of districts is 60. (I must select one different district (feature of polygon) every time and then Vector -> Geoprocessing -> Clip). Is there any possible way this procedure to become automatic? I work in QGIS (and ArcGIS).

4 Answers 4


You can create a model from the Processing Toolbox which allows you to automate a number of tasks.

You can create one by going to the menubar:

Processing > Graphical Modeler...

From the Inputs tab (shown in the red box), you will need two Vector layer parameters (shown in the blue box) for both your line and polygon layers.


Then from the Algorithms tab (shown in the red box), type in "clip" to search for your Clip tool. Then select the relevant options:

Clip options

Don't forget to type a name for the model and group shown in the green box. Save it and load the Processing Toolbox interface which can be enabled from the menubar: Processing > Toolbox.

Then find your model from the toolbox which should be under Models (or you can type in the name of your model in the search box). Now when you run it, click the green round-arrow button next to the polygon input layer to iterate through each polygon feature:

Running model

When you run it, the road layer will clip onto each polygon feature and the results will be loaded.

Note: I have tested this on a simple example and it worked fine. Tested with QGIS 2.16.0-Nødebo with Processing plugin v2.12.2, Win7 64-bit..

  • 2
    Yes, it worked. Thaks a lot for your detailed instructions. Is there anaway the names of these output files (clipped .shp) to be taken of a specific field of the polygon layer? Commented Aug 2, 2016 at 15:09
  • Most welcome, glad it did! Hmm, only through the Batch Process of the modeler I believe, however this works on a layer-by-layer basis so will not iterate through each feature. I think the only way would be to create custom script and write some python logic to loop through each output, get the value from a specific field and rename the file.
    – Joseph
    Commented Aug 2, 2016 at 15:18

Python would be the best way to do this. If you perform one clip you can copy the python snippet from the Geoprocessing results and 'build' code snippets for the remaining 59 using concatenate in excel

  • 1
    What is the python snippet from the Geoprocessing Results? Do you refer in ArcGIS environment? Could you give more details? Thanks in advance. Commented Aug 2, 2016 at 14:23
  • Yep sorry, within ArcMap. If you run one then go to Geoproccesing > Results you will see your completed clip function. If you go to right click and copy Python snippet it will give you the code that was used to perform it. I usually use command prompt to generate a list of files in a directory to get the list of shapefiles (copy into excel and remove any non .shp).
    – Alan Carr
    Commented Aug 2, 2016 at 14:27

Using ArcGis, no programming needed:

1 - Apply intersect, so all your network will take a unique cod for each district.

2 - Open model builder, under menu insert chose "Iterators\Row Selections"

3 - Right click on row selection and create two Model Variable (1-Input Table 2-Group By Field)

4 - Double click on Input table and chose created shapefile from Intersect command.

5 - Double click on group by field and chose the unique key of districts.

6 - open toolbox under Data management\Feature chose copy feature drag and drop it into model space.

7 - Link your selected feature of "Row Selections" with "Copy Feature" as input feature.

8 - On output chose your geodatabase where u want to save your shapefile dont forget to rename it "....your path.gdb\yourname_%VALUE%". %VALUE% is very import to rename all your new shape file with your unique code.

Your model would look like.

enter image description here

  • I tried this but it returned only one shape file instead 62. The only one difference is that I didn't run intersect as there is a field (in roads shape file) which contains the name of district in which every road segment is located. Thanks in advance. Commented Aug 8, 2016 at 18:44
  • No worry about intersect was just for taking the district code. I just tested it and it works fine, the logic is simple "Group by code than export grouped row to new feature". Be careful on field you choose for group and be sure for the last Output it should be renamed "yourname_%VALUE%" if dont the next shape will replace the past, so iterating all district you will take just the last district couse they were raplaced.
    – riflo
    Commented Aug 12, 2016 at 13:53

To add on to Joseph's answer, in newer versions of QGIS (I'm on 3.10) to use the "Iterate" button you need to use "Vector Features" in the input instead of "Vector Layer"

Related Question: Modeling iterative processes in QGIS 3

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.