0

I like to semiautomate my workflow.

I got always the same kind of geopackage with polygons and heat usages. I want to calculate the area of the polygons thus create a new field, calculate and write the area their and divide this by the heat usage in a second new cell.

I do not want a new file but create the new fields in the existing one.

I imagine it kind of this way

When I run it the following message pops up. "Could not creat layer building ... Creating of data source failed".

I found "attach to layer" but here I need to state where the geopackage layer is again. I like to speed up the process.

enter image description here

Is there another way?

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 20 at 16:33
2
  • 2
    I dont think it is possible using Field Calculator. You can use pyqgis though Commented Jan 21 at 6:05
  • So your problem is overwriting the input layer? I have models that overwrite a layer in a geopackage with newly created layer - using an expression to select the correct gpkg and layer Commented Jan 21 at 8:10

1 Answer 1

2

As @Bera mentioned, this is difficult to do using only the Model Builder / Field Calculator.

With PyQGIS it is quite easy though:

# Get the active layer
layer = iface.activeLayer()
# Start editing the layer
layer.startEditing()
# Add new fields
layer.addAttribute(QgsField('area', QVariant.Double))
layer.addAttribute(QgsField('area_over_heat', QVariant.Double))
# Update fields
layer.updateFields()
# Get the index of the new fields and heat_usage (adapt name according to your field name in the following)
area_idx = layer.fields().indexOf('area')
div_idx = layer.fields().indexOf('area_over_heat')
field1_idx = layer.fields().indexOf('heat_usage') # add more fields for other calculations
# Calculate area & area_over_heat for each feature
for feature in layer.getFeatures():
 # Calculate area (in the layer's CRS units)
 area = feature.geometry().area()
 
 # Get field1 value
 field1_value = feature.attributes()[field1_idx]
 
 # Calculate division (handle potential division by zero)
 if field1_value != 0:
 div = area / field1_value
 else:
 div = None # or whatever value you want to use for division by zero
 
 # Update the feature
 layer.changeAttributeValue(feature.id(), area_idx, area)
 layer.changeAttributeValue(feature.id(), div_idx, div)
# Commit changes to your layer
layer.commitChanges()
Taras
35.8k5 gold badges77 silver badges151 bronze badges
answered Jan 21 at 9:27
1
  • okay, I have no experience with pygis but I will try. thanks! Commented Jan 21 at 9:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.