Skip to main content
deleted 6 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

The solution

array_sum(
    array_foreach( 
        string_to_array ( "list of polygons" , ','),
        attribute (get_feature (@layer, 'name', @element), 'area')
        )
    )

Explanation

The error in your expression is that you use the funtionfunction area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon's geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute called area.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this element - like line 2 for feature B in your example. Use get_feature() for that.

  2. Than for this feature, get the value of the attribute named area, using the function attribute().

As you remarked in your comment, be aware where to use double quotes " and where to use single quotes '. Best see help for QGIS expression functions. A field name in single quotes returns the name of the field, a field name in double quotes returns the value stored in that field for the current feature.

The solution

array_sum(
    array_foreach( 
        string_to_array ( "list of polygons" , ','),
        attribute (get_feature (@layer, 'name', @element), 'area')
    )
)

Explanation

The error in your expression is that you use the funtion area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon's geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute called area.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this element - like line 2 for feature B in your example. Use get_feature() for that.

  2. Than for this feature, get the value of the attribute named area, using the function attribute().

As you remarked in your comment, be aware where to use double quotes " and where to use single quotes '. Best see help for QGIS expression functions. A field name in single quotes returns the name of the field, a field name in double quotes returns the value stored in that field for the current feature.

The solution

array_sum(
    array_foreach( 
        string_to_array("list of polygons", ','),
        attribute(get_feature (@layer, 'name', @element), 'area')
        )
    )

Explanation

The error in your expression is that you use the function area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon's geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute called area.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this element - like line 2 for feature B in your example. Use get_feature() for that.

  2. Than for this feature, get the value of the attribute named area, using the function attribute().

As you remarked in your comment, be aware where to use double quotes " and where to use single quotes '. Best see help for QGIS expression functions. A field name in single quotes returns the name of the field, a field name in double quotes returns the value stored in that field for the current feature.

added 272 characters in body
Source Link
Babel
  • 74.9k
  • 15
  • 87
  • 227

The solution

array_sum(
    array_foreach( 
        string_to_array ( "list of polygons" , ','),
        attribute (get_feature (@layer, 'name', @element), 'area')
    )
)

Explanation

The error in your expression is that you use the funtion area() with the argument @element, but @element@element returns the namename of a polygon, whereas area() expects a polygon-geometrypolygon's geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute called area.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this featureelement - like line 2 for feature B in your example. Use get_feature() for that.

  2. ThatThan for this feature, get get the value of the attribute named area, using the function attribute().

As you remarked in your comment, be aware where to use double quotes " and where to use single quotes '. Best see help for QGIS expression functions. A field name in single quotes returns the name of the field, a field name in double quotes returns the value stored in that field for the current feature.

The solution

array_sum(
    array_foreach( 
        string_to_array ( "list of polygons" , ','),
        attribute (get_feature (@layer, 'name', @element), 'area')
    )
)

Explanation

The error in your expression is that you use the funtion area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon-geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this feature - like line 2 for feature B in your example. Use get_feature() for that.

  2. That for this feature, get get the value of the attribute named area, using the function attribute().

The solution

array_sum(
    array_foreach( 
        string_to_array ( "list of polygons" , ','),
        attribute (get_feature (@layer, 'name', @element), 'area')
    )
)

Explanation

The error in your expression is that you use the funtion area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon's geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute called area.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this element - like line 2 for feature B in your example. Use get_feature() for that.

  2. Than for this feature, get the value of the attribute named area, using the function attribute().

As you remarked in your comment, be aware where to use double quotes " and where to use single quotes '. Best see help for QGIS expression functions. A field name in single quotes returns the name of the field, a field name in double quotes returns the value stored in that field for the current feature.

added 738 characters in body
Source Link
Babel
  • 74.9k
  • 15
  • 87
  • 227

Use this expression:

The solution

array_sum(
    array_foreach(
        string_to_array(
        string_to_array ( "list of "list_of_polygons"polygons" ,
            ','
        ),
        attribute(
            get_feature( 
               get_feature (@layer,
                'name',
                @element
            ),
            'area'
        )
    )
)

Explanation

The error in your expression is that you use the funtion area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon-geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this feature - like line 2 for feature B in your example. Use get_feature() for that.

  2. That for this feature, get get the value of the attribute named area, using the function attribute().

Use this expression:

array_sum(
    array_foreach(
        string_to_array(
            "list_of_polygons" ,
            ','
        ),
        attribute(
            get_feature( 
                @layer,
                'name',
                @element
            ),
            'area'
        )
    )
)

The solution

array_sum(
    array_foreach( 
        string_to_array ( "list of polygons" , ','),
        attribute (get_feature (@layer, 'name', @element), 'area')
    )
)

Explanation

The error in your expression is that you use the funtion area() with the argument @element, but @element returns the name of a polygon, whereas area() expects a polygon-geometry as input.

The functions area() as well as $area calculate the area from the geometry. You, however, just want to add values that already exist in an attribute.

How to implement

  1. So inside the array_foreach() function, you get the name of one of the polygons. You then must identify the feature (line in your attribute table) of this feature - like line 2 for feature B in your example. Use get_feature() for that.

  2. That for this feature, get get the value of the attribute named area, using the function attribute().

Source Link
Babel
  • 74.9k
  • 15
  • 87
  • 227
Loading