-
Gaetano Raffaele authorede01621ac
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import ogr
import sys
import subprocess
import platform
import numpy as np
def getFeaturesFields(shp,flds_pref):
ds = ogr.Open(shp, 0)
ly = ds.GetLayer(0)
flds = []
ldfn = ly.GetLayerDefn()
for n in range(ldfn.GetFieldCount()):
fn = ldfn.GetFieldDefn(n).name
if fn.startswith(tuple(flds_pref)):
flds.append(fn)
ds = None
return flds
def roughFix(shp,flds):
ds = ogr.Open(shp,1)
ly = ds.GetLayer(0)
arr = np.empty([ly.GetFeatureCount(), len(flds)])
i = 0
for f in ly:
j = 0
for fld in flds:
arr[i,j] = f.GetFieldAsDouble(fld)
j += 1
i += 1
#R-like rough fix
arr[np.where(arr==-9999.0)] = np.nan
mns = np.tile(np.nanmean(arr,axis=0),[ly.GetFeatureCount(),1])
arr[np.isnan(arr)] = mns[np.isnan(arr)]
ly.ResetReading()
i = 0
for f in ly:
j = 0
for fld in flds:
f.SetField(fld,arr[i, j])
ly.SetFeature(f)
j += 1
i += 1
ds = None
return
def training(shp,code,model_fld,params,feat,feat_mode = 'list'):
# Platform dependent parameters
if platform.system() == 'Linux':
sh = False
elif platform.system() == 'Windows':
sh = True
else:
sys.exit("Platform not supported!")
if '-classifier' in params:
classifier = params[params.index('-classifier') + 1]
else:
classifier = 'libsvm'
model_file = model_fld + '/' + classifier + '_' + code + '.model'
confmat_file = model_fld + '/' + classifier + '_' + code + '.confmat.txt'
stat_file = model_fld + '/GT_stats.xml'
if feat_mode == 'prefix':
flds = getFeaturesFields(shp, feat)
elif feat_mode == 'list':
flds = feat
else:
sys.exit('ERROR: mode ' + feat_mode + ' not valid.')
if platform.system() == 'Linux':
cmd = ['otbcli_ComputeVectorFeaturesStatistics','-io.vd',shp,'-io.stats',stat_file,'-feat'] + flds
subprocess.call(cmd,shell=sh)
elif platform.system() == 'Windows':
import otbApplication
app = otbApplication.Registry.CreateApplication('ComputeVectorFeaturesStatistics')
app.SetParameterStringList('io.vd', [shp])
app.SetParameterString('io.stats', model_fld + '/GT_stats.xml')
app.UpdateParameters()
app.SetParameterStringList('feat',flds)
app.ExecuteAndWriteOutput()
else:
sys.exit("Platform not supported!")
if platform.system() == 'Linux':
cmd = ['otbcli_TrainVectorClassifier', '-io.vd', shp, '-io.stats', model_fld + '/GT_stats.xml', '-io.confmatout', confmat_file, '-cfield', code, '-io.out', model_file, '-feat'] + flds + params
subprocess.call(cmd,shell=sh)
elif platform.system() == 'Windows':
import otbApplication
app = otbApplication.Registry.CreateApplication('TrainVectorClassifier')
app.SetParameterStringList('io.vd', [shp])
app.SetParameterString('io.stats', model_fld + '/GT_stats.xml')
app.SetParameterString('io.confmatout', confmat_file)
app.SetParameterString('io.out', model_file)
app.UpdateParameters()
app.SetParameterStringList('cfield', [code])
app.SetParameterStringList('feat', flds)
# Parse classification parameters string
# WARNING: works for all classifier with single value parameters
# (surely hangs on <string list> classification parameter types - e.g. -classifier.ann.sizes)
cl_param_keys = params[0::2]
cl_param_vals = params[1::2]
for prk,prv in zip(cl_param_keys,cl_param_vals):
app.SetParameterString(prk[1:],prv)
app.UpdateParameters()
app.ExecuteAndWriteOutput()
else:
sys.exit("Platform not supported!")
return flds
def classify(shp_list,code,stat_file,model_file,feat,feat_mode = 'list'):
# Platform dependent parameters
if platform.system() == 'Linux':
sh = False
elif platform.system() == 'Windows':
sh = True
else:
sys.exit("Platform not supported!")
if feat_mode == 'prefix':
flds = getFeaturesFields(shp_list[0], feat)
elif feat_mode == 'list':
flds = feat
else:
sys.exit('ERROR: mode ' + feat_mode + ' not valid.')
for shp in shp_list:
#roughFix(shp, flds)
if platform.system() == 'Linux':
cmd = ['otbcli_VectorClassifier','-in',shp,'-instat',stat_file,'-model',model_file,'-cfield',code,'-feat'] + flds
subprocess.call(cmd,shell=sh)
elif platform.system() == 'Windows':
import otbApplication
app = otbApplication.Registry.CreateApplication('VectorClassifier')
app.SetParameterString('in',shp)
app.SetParameterString('instat', stat_file)
app.SetParameterString('model', model_file)
app.SetParameterString('cfield', code)
app.UpdateParameters()
app.SetParameterStringList('feat',flds)
app.UpdateParameters()
app.ExecuteAndWriteOutput()
else:
sys.exit('Platform not supported!')
return