2

I have a polyline layer with unique value ("loop A"; "loop B", etc.) distributed between 2 fields just like this:

ID Field 1 Field 2
1 Loop A NULL
2 Loop A Loop B
3 Loop C Loop A

I would like to aggregate (or dissolve as I am not sure of the difference) every entities with "Loop A" as a value either in Field 1 or in Field 2 then every entities with "Loop B" , "Loop C", etc.

This could mean superimposed entities.

How can I do this?

3
  • 1
    What do you mean when you say "aggregate"? Do you mean "dissolve" or "collect into a multipart geometry" or something else? Maybe show some sample data. May 25, 2023 at 13:01
  • You are right, dissolve might be the right term. If 3 entities have 'Road name A' in field 1 or in field 2, after processing i would like only 1 entity
    – Arthur
    May 25, 2023 at 14:49
  • 1
    And what happens in field 2 if 'Road name A' is in field 1? Is field 2 NULL, or can it be some other value? May 25, 2023 at 22:28

4 Answers 4

1

If you use Select by expression from the Processing Toolbox, you can select all features with 'Road name A' in "Field 1" or "Field 2" with:

"Field 1" = 'Road name A' or "Field 2" = 'Road name A'

Replace the field names and value as appropriate.

Then run Dissolve using the Selected features only checkbox.

2
  • You could use Menu Processing > Toolbox > Aggregate for this and use the expression concat("Field1", "Field1") as as Group by expression: docs.qgis.org/latest/en/docs/user_manual/processing_algs/qgis/…
    – Babel
    May 26, 2023 at 15:09
  • @tom-brennan Thank you this might work if processes multiple times, but i would like to do this for every single value.
    – Arthur
    Jun 6, 2023 at 12:10
1

A fairly crude approach that appears to achieve your revised question is:

  1. Select the non-NULL features for Field 1 ("Field1" is not NULL)
  2. Use Refactor Fields to delete Field 2, and rename Field 1 to Field for the Selected features only, creating a new layer
  3. Repeat steps (1) and (2) for Field 2
  4. Use Merge Vector Layers on the two refactored layers from steps 1-3
  5. Dissolve the merged layer, using Field as the Dissolve Field

Using your example above, the merged layer attributes are as follows:

enter image description here

And the final result is 3 features, with Loop A having 3 parts, and Loop B/Loop C having 1 part each (I used num_geometries to count the parts):

enter image description here

1

Basic idea: run model in batch mode

The easiest way I see is to create a model and then run it in batch mode, to get one output for each input (letter A, B and C). I first show the basic steps how to create the model, in a second paragraph I show a more refined model that only leaves the features you want as a result.

You can download the final model here to use it or adapt it to your neeeds. Run the model in batch mode by inserting you inputs like A, B, C etc. (see step 2 with screenshots for details how to run the model in batch mode).

Screenshot (here with polygons; runs the same with lines or points): labels with attribute values of field1 and field2 of the initial layer; in color output polygons of the model like follows: blue polygons: containing A; red line pattern: containing B; yellow outlined: containing C; all three layers contain just one (multipart) feature = merged/aggregated all input polygons containing the respective text string:

enter image description here

Basic steps to create model

  1. Create the model with two inputs (text_input and vector_input) and one algorithm Aggregate. Settings in Aggregate: for Input layer set mode to Using model input and select the Vector_input. For Group by expression, for Value use array_contains (array ("field1","field2"),@text_input) where @text_input is a variable QGIS creates automatically based on the name of Text_input:

    enter image description here

  2. Save and run the model in Batch mode, add as many rows as you have values and fill in the values (A, B, C) in text_input:

    enter image description here

Refined model

Find the model linked at the beginning of this answer.

Using aggregate, you'll get one output feature for the input features that contain the attribute and another feature for those that do not contain the attribute. We want to keep only the first feature.

For this, we add a Select by Expression algorithm with the expression set to Pre-calculated value and the content 'regexp_match (concat,''(.' || @text_input || '.)'')>0'. This selects the feature that contains the text based on which we want to aggregate.

Now based on this selection, add an Extract selected features algorithm with inuput layer set to Algorithm Output and selecting the Select by expression output (see screenshot).

enter image description here

0

Use virtual layers. Use the following SQL query to define your virtual layer, remember to change some values to fit with your layer name or field names.
The first part of the query (WITH) creates a table with all the different values that appear in the desired fields (Field 1 and Field 2), then for every different value it runs a subquery aggregating every geometry that contains the current value (e.g Loop A) with the ST_Union function.

WITH
    "u_values"("uniques") AS (
    SELECT "Field 1"
    FROM "layer" -- set here your layer name
    UNION
    SELECT "Field 2"
    FROM "layer" -- set here your layer name
    /*
    insert here blocks in the same structure as:
    
    SELECT "Field N"
    FROM "layer" name of the layer -- set here your layer name
    
    replace Field N with the name of the field to include
    separate each block with the UNION keyword
    */
    )
SELECT "uniques", (SELECT st_union("geometry") -- set here the geometry column name if necesary
    FROM "layer" -- set here your layer name
    WHERE "Field 1" = "u_values"."uniques"
    OR "Field 2" = "u_values"."uniques"
    /*
    insert here one block for every included field, in the way:
    
    OR "Field N" = "u_values"."unique"
    
    Replace Field_N with the name of the field
    */
) AS 'geom'
FROM "u_values"

This query will generate a feature for each different value of "Loop N" if you want to separate features that don't touch, then run the Multipart to singleparts QGIS processing algorithm.

4
  • Hi, thank you for this very detailed method. I managed to create a virtual layer with my own fields and layer names. But struggle to make geometry from those entities i.postimg.cc/pr4Dqg7Y/Capture-virtuallayer-1.jpg
    – Arthur
    Jun 8, 2023 at 7:22
  • @Arthur, It's working, all you need to do is specify the geometry column for the output virtual layer. When you click the create virtual layer you will se this dialog, in the lower part you can set the geometry column.
    – Mayo
    Jun 8, 2023 at 12:41
  • i tried it, but either with "geom" or "geometry" as column name neither work i.postimg.cc/SK4ZH30p/Capture-virtuallayer-2.jpg
    – Arthur
    Jun 8, 2023 at 15:46
  • @Arthur, the name must be "geom", did you click again in the add button? other wise changes wont be done. I tested an it worked for me.
    – Mayo
    Jun 12, 2023 at 12:43

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.