Use Menu Processing > Toolbox > Split Vector Layers
tool and use the X-field (country) in the first round. Then again split the resulting US and Canada layers again, this time using the Y-field (states). Like this, you can easily group the outputs. You could also define different directories for the output layers of the split operations.
To order the layers in alphabetic order, see this answer.
To do it in one run and to have the layer files with the name of the country and the state, before running the split, create a new field called location
in the initial layer, concatenating X
and Y
attributes with Field calculator: "X" || '_' || "Y"
- result looks like USA_Georgia
. Then split by this field.
The output filename will automatically consist of the fieldname + the attribute value and thus can easily be ordered alphabetically:
Another option, to create separate folders for each Y-value:
First split by X. Then again run the split operation in batch mode (there are easy options to select input files by patterns, based on expressions, all files from a directory etc.).
If you have a lot of input layers, make the settings of the parameters for the first row and select Autofill... to copy it to the other input layers.
For the ouput directory, you can click on Autofill... > Calculate by Expression and create an expression by concatenating the basic path as a string together with the variable INPUT
, available in the expression string builder. This variable contains the path of the input file, thus something like 'C:/Users/[your_path]/X_Canada.gpkg'
. Use an expression based on regular expressions to extract the country information (Canada
) from this:
regexp_matches (@INPUT,'(\\/X_)(.*)(.gpkg)')[1]
Settings for batch mode:
How the result looks like: a separate folder for each X-value: