# Calculating attibute for a layer objects based on objects of another layer

I have a point layer. The points have float attribute "cost". I also have a line layer with a simple line between every two points from the point layer.

I want to calculate a new attribute for the line layer using a such formula: new attribute for line layer = cost(from point1) + cost(from point2)

How can I make a geometry expression for it?

• What is your desired output? What does your input look like, how many points it has? What should be done with the `"cost"`-attribute? Commented Jan 14 at 17:53
• Cost is a field of a point layer. It's the distance from a point to the polygon boundary. I calculated it through Field calculator. Then I created a line between every pair of points and now I want to calculate line weight based on cost field. I have a lot of points, 2 thousands or more. And there are many more lines between them. In short I try to build a graph and calculate manhetton distance for its edges. May be I'm not doing it correctly Commented Jan 14 at 18:07

You can use QGIS expressions for this, where `point` is the name of the point layer:

``````array_sum(
overlay_nearest (
'point',"cost",limit:=2
)
)
``````

The expression used to label the line:

Variant for more complex situations

This is the standard case. If, however, you have more than two points on your line like in the following image, you get wrong results: see the middle line. It takes the two points at the left 3+8=11. To avoid that, you only want to sum up points that overlap the start- and endpoints of the line.

For this, the epression is a bit more complex:

``````array_sum(
with_variable(
'cv',
'array_max(
array_foreach (
overlay_nearest (''point'',\$id, limit:=-1),
case
when
within (
xx_point (\$geometry),
buffer (
geometry (
get_feature_by_id (''point'', @element)
),
0.01
)
)
then
attribute (
get_feature_by_id (
''point'',
@element
),
''cost''
)
end
)
)',
array_foreach (
array('start','end'),
eval (replace (@cv, 'xx', @element))
)
)
)
``````
• I'm sorry. I explained my problem incorrectly. The more correct question is How can I refer to the point layer attribute to calculate a new line layer attribute? Commented Jan 14 at 17:51
• I calculated distance of an every point from the boundaries of the polygon(attribute "cost" by your method) and now I want to сalculate weight of lines between two points to build a graph. And then I'm planning to use Dejkstra algorythm for such a graph Commented Jan 14 at 17:56