-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_field_values.py
113 lines (107 loc) · 3.41 KB
/
read_field_values.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
lyr = iface.activeLayer()
print (lyr.name())
#idx = lyr.dataProvider().fieldNameIndex('P_DSM')
#values = lyr.uniqueValues(idx)
#print(values)
#for v in values:
# #print(v.type())
##for uv in values:
# print (v)
# pippo = v.split("/")
# print(pippo[4])
#print(values)
uniquevalues = []
print(uniquevalues)
uniqueprovider = lyr.dataProvider()
fields = uniqueprovider.fields()
id = fields.indexFromName('P_DSM')
uniquevalues = list(uniqueprovider.uniqueValues( id ))
#print(len(uniquevalues))
pluto = []
for uv in uniquevalues:
#print (uv)
#pippo = r'{}'.format(uv)
#print (pippo)
# pippo = str(uv)
#print (pippo)
#pippo2 = str(uv).replace("\", )
pippo2 = str(uv).split("\\")
#print (pippo2)
if len(pippo2) > 4:
pluto.append(pippo2[5] + '-' + pippo2[6])
print (pluto)
#fids = []
for f in lyr.getFeatures():
#print(type(f["P_DTM"]))
if f["P_DTM"] == NULL :
print('no DTM')
elif '2008_2010_Lidar_TEST' in f["P_DTM"]:
if 'Contratto_140' in f["P_DTM"]:
#print('trovati')
# #fids.append(f.id())
## try:
lyr.select(f.id())
lyr.startEditing()
for sf in lyr.getSelectedFeatures():
print(sf_id)
print(field_id)
if sf["P_CHM"] != NULL:
print(sf["P_CHM"])
chm_pathfile = sf["P_CHM"] + '\\' + sf["N_CHM"]
print(chm_pathfile)
lyr_chm = QgsRasterLayer(chm_pathfile, sf["N_CHM"])
QgsProject.instance().addMapLayers([lyr_chm])
else:
entries = []
sf_id = sf.id()
field_id = sf.fields().indexFromName('P_CHM')
dsm_pathfile = sf["P_DSM"] + '\\' + sf["N_DSM"]
dtm_pathfile = sf["P_DTM"] + '\\' + sf["N_DTM"]
lyr_dsm = QgsRasterLayer(dsm_pathfile, sf["N_DSM"])
lyr_dtm = QgsRasterLayer(dtm_pathfile, sf["N_DTM"])
#QgsProject.instance().addMapLayers([lyr_dsm])
#QgsProject.instance().addMapLayers([lyr_dtm])
chm_name = sf["N_DTM"].split(".")
print(chm_name[0])
if 'DTM' in chm_name[0]:
chm_file = chm_name[0].replace('DTM', 'CHM')
print(chm_file)
elif 'dtm' in chm_name[0]:
chm_file = chm_name[0].replace('dtm', 'chm')
elif 'ground' in chm_name[0]:
chm_file = chm_name[0].replace('ground', 'chm')
else:
chm_file = chm_name[0] + '_CHM'
lyr.changeAttributeValue(sf_id, field_id, "pippo")
lyr.commitChanges()
#print(chm_name[0])
# ras1 = QgsRasterCalculatorEntry()
# ras1.ref = '{}@1'.format(sf["N_DSM"])
# ras1.raster = lyr_dsm
# ras1.bandNumber = 1
# entries.append(ras1)
# print(ras1)
#
# ras2 = QgsRasterCalculatorEntry()
# ras2.ref = '{}@1'.format(sf["N_DTM"])
# ras2.raster = lyr_dtm
# ras2.bandNumber = 1
# entries.append(ras2)
#
# print(ras2)
# print(entries)
#
# calc = QgsRasterCalculator('{}@1 - {}@1'.format(sf["N_DSM"], sf["N_DTM"]), 'C:/Users/roberta/Desktop/{}_CHM.tif'.format(sf["N_DTM"]), 'GTiff', lyr_dsm.extent(), lyr_dsm.width(), lyr_dsm.height(), entries)
# calc.processCalculation()
#else:
#print('NO')
#print(fids)
#lyr.select(fids)
# for p in pippo:
# print(p[4])
#fields = [field.name() for field in lyr.fields()]
#print (fields)
#for f in lyr.getSelectedFeatures():
#print (f["tavola"])
#if f.fields == 'tavola':
#print ('è una tavola')