-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcave_leg_fields.py
More file actions
62 lines (40 loc) · 1.84 KB
/
cave_leg_fields.py
File metadata and controls
62 lines (40 loc) · 1.84 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
#===========================================================
#===========================================================
#Add fields to cave leg layer
#(year of survey, 3D length, gradient, azimuth)
#Lin Yangchen
#30 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("YEAR", QVariant.Int),
QgsField("LENGTH_3D", QVariant.Double),
QgsField("GRADIENT", QVariant.Double),
QgsField("AZIMUTH", QVariant.Double)])
layer.updateFields()
#===========================================================
#QGIS expressions
year = QgsExpression('year(DATE1)')
length_3d = QgsExpression('length3D($geometry)')
gradient = QgsExpression('abs(z(start_point($geometry)) - z(end_point($geometry)))/$length')
azimuth = QgsExpression('degrees(azimuth(start_point($geometry), end_point($geometry)))')
#context for executing expressions
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
#===========================================================
with edit(layer):
for f in layer.getFeatures():
context.setFeature(f)
f['YEAR'] = year.evaluate(context)
f['LENGTH_3D'] = length_3d.evaluate(context)
f['GRADIENT'] = gradient.evaluate(context)
f['AZIMUTH'] = azimuth.evaluate(context)
layer.updateFeature(f)