2

My input layer has many polygons, the overlay layer of eight regions using the Processing Toolbox 'clip' tool splits the input layer in to eight scratch files using the iterate function (green circular arrow to the right of the overlay layer name).

Is there a way to have each of the resultant output layers both 1/ layer named and 2/ file saved with a name calculated from a field value from the overlay layer?

Using the Prcoessing Toolbox Clip tool

1
  • 1
    Running the tool in batch mode gives you such options.
    – Babel
    Commented Nov 29, 2022 at 7:34

1 Answer 1

7

Unfortunately, the iterator in the clip tool does not give you the option to save the files on the disk based on a field in the overlay layer. Usually, such a process can be programmed using pyqgis.

However, there is a possibility to do what you want without using pyqgis, but it requires some extra work.

  1. Create two folders in your target directory and give them the name OverlayClip and Output, for example: F:\Test\OverlayClip and F:\Test\Output

  2. Use the Split Vector Layer tool a, and select the field name - that you want your final clipped files to have - to split the overlay layer into several files based on that field and save the files in the OverlayClip folder. Uncheck Add field prefix to file names. You can use either shp or gpkg for the output files.

  3. Right-click the Clip tool and select Execute as Batch Process.

  4. In the Input Layer, select the layer that you want to clip.

  5. For the Overlay layer, if you saved the files in the step 2 as gpkg, press the Autofill and select Add all files from a directory and select the OverlayClip directory to populate the table. However, if you select shp, then select Add files by pattern, write .shp, Find Files, and select the OverlayClip directory to populate the table. You will need to delete the first row which used by default to clip by the whole layer.

  6. Under the clipped column, from the Autofill, select Calculate by expression, and write the following expression:

     replace(file_path( @OVERLAY ),'OverlayClip',format('Output\\%1',base_file_name(@OVERLAY)))
    

The above expression will replace the OverlayClip folder with the name of the Output folder, which will hold all the clipped files with the same name written in the field you want to use. It becomes F:\Test\OverlayClip\*****. The output will be in geopackage format to avoid issues with the shapefiles.

enter image description here

If you want shapefiles, then use:

     replace(file_path( @OVERLAY ),'OverlayClip',format('Output\\%1',file_name(@OVERLAY)))

enter image description here

But I recommend using geopackage format.

  1. Press Run to run the tool.
2
  • Great answer! However, you write about "using pyqgis"... you mean QGIS expressions? I don't see any pyqgis code in your answer...?
    – Babel
    Commented Nov 29, 2022 at 8:26
  • @Babel Thank you very much. I mentioned using pyqgis as a general rule for a such task without the need to save temporary files to use them for the clipping process. But I didn't intend to provide a pyqgis code. The whole idea is that the OP can do the same work using QGIS expressions without pyqgis code.
    – ahmadhanb
    Commented Nov 29, 2022 at 9:44

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.