-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonConfigLoader.cs
266 lines (246 loc) · 12.1 KB
/
JsonConfigLoader.cs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (C) Her Majesty the Queen in Right of Canada,
// as represented by the Minister of Natural Resources Canada
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CBMSIT.Mapping;
using Newtonsoft.Json.Linq;
namespace StandardImportToolPlugin
{
public class JsonConfigLoader
{
public Sitplugin Load(string json)
{
Sitplugin sitplugin = null;
JObject obj = JObject.Parse(json);
string outputPath = System.IO.Path.GetFullPath((string)obj["output_path"]);
string archive_index_db_path = null;
if (obj["archive_index_db_path"] != null)
{
archive_index_db_path = System.IO.Path.GetFullPath((string)obj["archive_index_db_path"]);
}
var mappingConfig = obj["mapping_config"];
if(mappingConfig == null)
{
throw new Exception("missing mapping_config section");
}
bool initialize_mapping = mappingConfig["initialize_mapping"] == null ?
false : (bool)mappingConfig["initialize_mapping"];
CBMSIT.UserData.UserDataSet userData = null;
if (obj["import_config"] != null)
{
var importConfig = obj["import_config"];
if (importConfig["path"] != null)
{
userData = Sitplugin.ParseSITData(
path: System.IO.Path.GetFullPath((string)importConfig["path"]),
AgeClassTableName: (string)importConfig["ageclass_table_name"],
ClassifiersTableName: (string)importConfig["classifiers_table_name"],
DisturbanceEventsTableName: (string)importConfig["disturbance_events_table_name"],
DisturbanceTypesTableName: (string)importConfig["disturbance_types_table_name"],
InventoryTableName: (string)importConfig["inventory_table_name"],
TransitionRulesTableName: (string)importConfig["transition_rules_table_name"],
YieldTableName: (string)importConfig["yield_table_name"]);
}
else if (importConfig["ageclass_path"] != null)
{
userData = Sitplugin.ParseSITDataText(
ageClassPath: System.IO.Path.GetFullPath((string)importConfig["ageclass_path"]),
classifiersPath: System.IO.Path.GetFullPath((string)importConfig["classifiers_path"]),
disturbanceEventsPath: System.IO.Path.GetFullPath((string)importConfig["disturbance_events_path"]),
disturbanceTypesPath: System.IO.Path.GetFullPath((string)importConfig["disturbance_types_path"]),
inventoryPath: System.IO.Path.GetFullPath((string)importConfig["inventory_path"]),
transitionRulesPath: System.IO.Path.GetFullPath((string)importConfig["transition_rules_path"]),
yieldPath: System.IO.Path.GetFullPath((string)importConfig["yield_path"]));
}
else
{
throw new Exception("error in import_config section");
}
sitplugin = new Sitplugin(outputPath, initialize_mapping, userData,
archive_index_database_path: archive_index_db_path);
MapSpatialUnits(sitplugin, mappingConfig["spatial_units"]);
MapSpecies(sitplugin, mappingConfig["species"]);
MapNonForest(sitplugin, mappingConfig["nonforest"]);
MapDisturbanceTypes(sitplugin, mappingConfig["disturbance_types"]);
}
else if(obj["data"] != null)
{
sitplugin = new Sitplugin(outputPath, initialize_mapping,
archive_index_database_path: archive_index_db_path);
sitplugin.AddAgeClasses(
(int)obj["data"]["age_class"]["age_class_size"],
(int)obj["data"]["age_class"]["num_age_classes"]);
foreach (var c in obj["data"]["classifiers"])
{
sitplugin.AddClassifier((string)c);
}
MapSpatialUnits(sitplugin, mappingConfig["spatial_units"]);
MapSpecies(sitplugin, mappingConfig["species"]);
MapNonForest(sitplugin, mappingConfig["nonforest"]);
MapDisturbanceTypes(sitplugin, mappingConfig["disturbance_types"]);
LoadConfigDataObjects(sitplugin, obj["data"]);
}
else
{
throw new Exception("expected one of 'import_config', or 'data' in configuration");
}
return sitplugin;
}
private static T Unpack<T>(JToken t, string key, T fallback)
{
return t[key] == null ? fallback : t[key].Value<T>();
}
private static void LoadConfigDataObjects(Sitplugin plugin, JToken dataConfig)
{
var parseClassifierSet = new Func<JToken, string>((t) =>
{
return String.Join(",", t.Select(a => (string)a));
});
foreach(var i in dataConfig["inventory"])
{
plugin.AddInventory(
classifierSet: parseClassifierSet(i["classifier_set"]),
area: (double)i["area"],
age: (int)i["age"],
spatialReference: Unpack(i, "spatial_reference", -1),
delay: Unpack(i, "delay", 0),
unfcccLandClass: Unpack(i, "unfccc_land_class", 0),
HistoricDisturbance: Unpack(i, "historic_disturbance", (string)null),
MostRecentDisturbance: Unpack(i, "last_pass_disturbance", (string)null));
}
foreach(var i in dataConfig["disturbance_events"])
{
plugin.AddDisturbanceEvent(
classifierSet: parseClassifierSet(i["classifier_set"]),
disturbanceType: (string)i["disturbance_type"],
timestep: (int)i["time_step"],
target: (double)i["target"],
targetType: (string)i["target_type"],
sort: (string)i["sort"],
spatialReference: Unpack(i, "spatial_reference", -1),
ageMin: Unpack(i, "age_min", -1),
ageMax: Unpack(i, "age_max", -1),
efficiency: Unpack(i, "efficiency", 1.0),
eligibility: i["eligibility"]?.Select(a => (double)a).ToArray());
}
foreach (var i in dataConfig["yield"])
{
plugin.AddYield(
classifierSet: parseClassifierSet(i["classifier_set"]),
leadingSpeciesClassifierValue: (string)i["leading_species_classifier_value"],
values: i["values"].Select(a => (double)a).ToList());
}
if (dataConfig["transition_rules"] != null)
{
foreach (var i in dataConfig["transition_rules"])
{
plugin.AddTransitionRule(
classifierSetSource: parseClassifierSet(i["classifier_set_source"]),
classifierSetTarget: parseClassifierSet(i["classifier_set_target"]),
disturbanceType: (string)i["disturbance_type"],
percent: Unpack(i, "percent", 100.0),
spatialReference: Unpack(i, "spatial_reference", -1),
ageMin: Unpack(i, "age_min", -1),
ageMax: Unpack(i, "age_max", -1),
resetAge: Unpack(i, "reset_age", -1),
regenDelay: Unpack(i, "regen_delay", 0));
}
}
}
private static void MapDisturbanceTypes(Sitplugin sitplugin, JToken mappingConfig)
{
if (mappingConfig != null && mappingConfig.HasValues)
{
if (mappingConfig["disturbance_type_mapping"] != null)
{
foreach (var item in mappingConfig["disturbance_type_mapping"])
{
sitplugin.MapDisturbanceType(
(string)item["user_dist_type"],
(string)item["default_dist_type"]);
}
}
}
}
private static void MapSpecies(Sitplugin sitplugin, JToken mappingConfig)
{
sitplugin.SetSpeciesClassifier((string)mappingConfig["species_classifier"]);
if (mappingConfig["species_mapping"] != null)
{
foreach (var item in mappingConfig["species_mapping"])
{
sitplugin.MapSpecies(
(string)item["user_species"],
(string)item["default_species"]);
}
}
}
private static void MapNonForest(Sitplugin sitplugin, JToken mappingConfig)
{
if(mappingConfig == null || !mappingConfig.HasValues)
{
return;
}
sitplugin.SetNonForestClassifier((string)mappingConfig["nonforest_classifier"]);
foreach (var item in mappingConfig["nonforest_mapping"])
{
var default_nonforest_type = (string)item["default_nonforest_type"];
if (default_nonforest_type == null){
//allow null here so that users are not forced
//to look up and use the localized "Forest Only" magic string
default_nonforest_type = Sitplugin.ForestOnly;
}
sitplugin.MapNonForest((string)item["user_nonforest_type"], default_nonforest_type);
}
}
private static void MapSpatialUnits(Sitplugin sitplugin, JToken mappingConfig)
{
if (!Enum.TryParse((string)mappingConfig["mapping_mode"], out SpatialUnitClassifierMode mode))
{
throw new ArgumentException("expected one of the SpatialUnitClassifierMode enum values");
}
switch (mode)
{
case SpatialUnitClassifierMode.JoinedAdminEcoClassifier:
sitplugin.SetSPUMapping((string)mappingConfig["spu_classifier"]);
foreach (var item in mappingConfig["spu_mapping"])
{
sitplugin.MapSpatialUnit(
(string)item["user_spatial_unit"],
(string)item["default_spatial_unit"]["admin_boundary"],
(string)item["default_spatial_unit"]["eco_boundary"]);
}
break;
case SpatialUnitClassifierMode.SeperateAdminEcoClassifiers:
sitplugin.SetAdminEcoMapping(
(string)mappingConfig["admin_classifier"],
(string)mappingConfig["eco_classifier"]);
if (mappingConfig["admin_mapping"] != null)
{
foreach (var item in mappingConfig["admin_mapping"])
{
sitplugin.MapAdminBoundary(
(string)item["user_admin_boundary"],
(string)item["default_admin_boundary"]);
}
}
if (mappingConfig["eco_mapping"] != null)
{
foreach (var item in mappingConfig["eco_mapping"])
{
sitplugin.MapEcoBoundary(
(string)item["user_eco_boundary"],
(string)item["default_eco_boundary"]);
}
}
break;
case SpatialUnitClassifierMode.SingleDefaultSpatialUnit:
sitplugin.SetSingleSpatialUnit((int)mappingConfig["default_spuid"]);
break;
}
}
}
}