3

I have a hard time managing a huge feature dataset. I have a gpkg containing about 10k polygons. Now I'd like to order them by two attributes to get a useful qgis browser structure. Let me try to display this graphically:

enter image description here

So what I am trying to do is arranging all the features depending on two attributes in the QGIS browser. I thought about splitting them by attributes, but that does not help me get an order into this data and the output would be just a lot of shapefiles without a folder structure. Arranging them manually in the browser window with layer groups would cost me months.

7
  • Does this answer your question? Categorization with multiple values- QGIS 2.18 Commented Feb 5, 2023 at 9:21
  • 1
    As far as I know you cannot create a group analog in layer symbology. Commented Feb 5, 2023 at 9:23
  • Thanks for the link! The solution provided there is a trade-off. However, not beeing able to group it is unlucky. Commented Feb 5, 2023 at 9:41
  • You try to achieve multiple layers from one layer - your table in the image- and then to arrange them in the described layertree, right?
    – eurojam
    Commented Feb 5, 2023 at 9:46
  • Yes, exactly. That's what I'm trying to do. Commented Feb 5, 2023 at 9:47

1 Answer 1

2

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:

enter image description here


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: enter image description here

How the result looks like: a separate folder for each X-value: enter image description hereenter image description here

5
  • Thanks for your answer. The problem I see here is that the ouput is thousands of seperate vector layer in one folder(at least that's the problem for the second splitting run) and I'd like to preserve the structure shown in the example layer tree. Can I specify the creation of output folders which name contains for example attribute"Y" in the output directory of the split vector layer tool? Commented Feb 5, 2023 at 10:07
  • Sorry, I'm not sure what you mean by you get "thousands of seperate vector layers" - I thoght that was exactly what you want to get? Splitting up one layer in several more, based on attributes? And why 1000s? US has 50 states, so you will get 50 layers by splitting US by state? Did I misunderstand your question? Can you provide sample data for testing?
    – Babel
    Commented Feb 5, 2023 at 10:10
  • The layer I have is a bit more complex. I just used the US and Canada citys as a simpler example to describe my problem. In reality I have a dataset of cadastral units for an entire state. The first split vector layer step would produce a hundred shapefiles, but the next one multiple thousands. Commented Feb 5, 2023 at 10:14
  • See edited answer - I hope that helps for what you want to do.
    – Babel
    Commented Feb 5, 2023 at 10:46
  • 1
    Thanks for your help, this is awesome! Commented Feb 5, 2023 at 11:23

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.