-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcave_polygon_fields.py
More file actions
70 lines (47 loc) · 1.99 KB
/
cave_polygon_fields.py
File metadata and controls
70 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#===========================================================
#===========================================================
#Add fields to cave polygon layer
#(floor, ceiling, height, area, volume)
#Lin Yangchen
#29 June 2024
#===========================================================
#===========================================================
#make sure the correct layer is selected in QGIS
#for distance accuracy, CRS should be EPSG:32650
#===========================================================
#iface for interacting with QGIS environment
#access the active layer
layer = iface.activeLayer()
#===========================================================
#add new fields
#types: String, Int, Double
layer.dataProvider().addAttributes([QgsField("FLOOR", QVariant.Double),
QgsField("CEILING", QVariant.Double),
QgsField("HEIGHT", QVariant.Double),
QgsField("AREA", QVariant.Double),
QgsField("VOLUME", QVariant.Double)])
layer.updateFields()
#===========================================================
#QGIS expressions
floor = QgsExpression('ELEVATION - MEAN_DOWN')
ceiling = QgsExpression('ELEVATION + MEAN_UP')
height = QgsExpression('MEAN_DOWN + MEAN_UP')
area = QgsExpression('$area')
volume = QgsExpression('AREA * HEIGHT')
#context for executing expressions
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
#===========================================================
with edit(layer):
for f in layer.getFeatures():
context.setFeature(f)
f['FLOOR'] = floor.evaluate(context)
f['CEILING'] = ceiling.evaluate(context)
f['HEIGHT'] = height.evaluate(context)
f['AREA'] = area.evaluate(context)
layer.updateFeature(f)
with edit(layer):
for f in layer.getFeatures():
context.setFeature(f)
f['VOLUME'] = volume.evaluate(context)
layer.updateFeature(f)