I have a point layer with two fields named "id"
and "price"
, and a polygon layer with a field named "code_zone"
.
I need to get, for each polygon, the "id"
of the point that has the highest value of "price"
, and this maximum value of the "price"
.
This is the beginning of my code:
for feature in layer_point.getFeatures():
point_price = feature.attribute('price')
point_geom = feature.geometry()
for feat in layer_polygon.getFeatures():
polygon_codeZone = feat.attribute('code_zone')
polygon_maxValue = feat.attribute('max_value')
polygon_geom = feat.geometry()
if polygon_geom.contains(point_geom):
...
I tried with maximumValue()
, but I get only the maximum value of the whole point layer and not the maximum value per polygon. What I am doing wrong?
As soon as I get these maximum values, I could fill them in my polygon layer with polygon_layer.changeAttributeValue()
in the "max_value"
field.
Best Answer
The user of a SQL query is appropriate for your problem.
I created 2 tables: one with 2 polygons and another with 5 points.
Then I go to the database manager > Virtual layers > and open a SQL query window
I execute the following request :
Result of the temporary table all_price on the left and the final result on the right. If you don't understand the request, I can give you more explanations.