-
Notifications
You must be signed in to change notification settings - Fork 156
/
ProjectConf.cpp
266 lines (236 loc) · 8.33 KB
/
ProjectConf.cpp
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
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include "DataViewer/DataSource.h"
#include "ProjectConf.h"
#include "GdaConst.h"
#include "GenUtils.h"
#include "GdaException.h"
using boost::property_tree::ptree;
//------------------------------------------------------------------------------
// LayerConfiguration member functions
//------------------------------------------------------------------------------
LayerConfiguration::~LayerConfiguration()
{
if (datasource) delete datasource; datasource= 0;
if (variable_order) delete variable_order;
if (custom_classifs) delete custom_classifs;
if (spatial_weights) delete spatial_weights;
}
LayerConfiguration::LayerConfiguration(wxString _layer_name)
: layer_name(_layer_name), datasource(0), variable_order(0), custom_classifs(0),
spatial_weights(0), default_vars(0)
{
}
LayerConfiguration::LayerConfiguration(const ptree& xml_tree,
const wxString& proj_path)
: datasource(0), variable_order(0), custom_classifs(0),
spatial_weights(0), default_vars(0)
{
ReadPtree(xml_tree, proj_path);
}
LayerConfiguration::LayerConfiguration(wxString layerName,
IDataSource* ds)
: layer_name(layerName), datasource(ds)
{
variable_order = new VarOrderPtree();
custom_classifs = new CustomClassifPtree();
spatial_weights = new WeightsManPtree();
default_vars = new DefaultVarsPtree();
}
LayerConfiguration::LayerConfiguration(wxString layerName,
IDataSource* ds,
VarOrderPtree* vo,
CustomClassifPtree* cc,
WeightsManPtree* sp_wts,
DefaultVarsPtree* dvs)
: layer_name(layerName), datasource(ds), variable_order(vo),
custom_classifs(cc), spatial_weights(sp_wts), default_vars(dvs)
{
}
void LayerConfiguration::UpdateDataSource(IDataSource* new_datasource)
{
if ( datasource ) {
delete datasource;
datasource = NULL;
}
datasource = new_datasource;
}
void LayerConfiguration::ReadPtree(const ptree& pt,
const wxString& proj_path)
{
wxString tmp(pt.get("title", "").c_str(), wxConvUTF8);
layer_title = tmp;
// create DataSource instance from <datasource>...
const ptree& subtree = pt.get_child("datasource");
std::string type_str = subtree.get<std::string>("type");
datasource = IDataSource::CreateDataSource(type_str, subtree, proj_path);
wxString tmp1(pt.get("layername", "").c_str(), wxConvUTF8);
layer_name = tmp1;
// create VarOrderPtree instance from <variable_order>...
if (!variable_order) variable_order = new VarOrderPtree(pt, proj_path);
// create CustomClassifPtree instance from <custom_classifications>...
if (!custom_classifs) custom_classifs = new CustomClassifPtree(pt,
proj_path);
// create WeightsManPtree instance from <spatial_weights>...
if (!spatial_weights) spatial_weights = new WeightsManPtree(pt, proj_path);
// create DefaultVarsPtree instance from <default_vars>...
if (!default_vars) default_vars = new DefaultVarsPtree(pt, proj_path);
}
void LayerConfiguration::WritePtree(ptree& pt,
const wxString& proj_path)
{
ptree& subtree = pt.put("datasource", "");
datasource->WritePtree(subtree, proj_path);
pt.put("layername", layer_name);
pt.put("title", layer_title);
if (variable_order) variable_order->WritePtree(pt, proj_path);
if (custom_classifs) custom_classifs->WritePtree(pt, proj_path);
if (spatial_weights) spatial_weights->WritePtree(pt, proj_path);
if (default_vars) default_vars->WritePtree(pt, proj_path);
}
LayerConfiguration* LayerConfiguration::Clone()
{
LayerConfiguration* new_layer_conf = new LayerConfiguration(layer_name);
if (datasource)
new_layer_conf->SetDataSource(datasource->Clone());
if (variable_order)
new_layer_conf->SetVariableOrder(variable_order->Clone());
if (custom_classifs)
new_layer_conf->SetCustomClassifs(custom_classifs->Clone());
if (spatial_weights)
new_layer_conf->SetSpatialWeights(spatial_weights->Clone());
if (default_vars)
new_layer_conf->SetDefaultVars(default_vars->Clone());
return new_layer_conf;
}
void LayerConfiguration::SetDataSource(IDataSource *new_datasource)
{
if (datasource) {
delete datasource;
datasource = NULL;
}
datasource = new_datasource;
}
void LayerConfiguration::SetVariableOrder(VarOrderPtree *new_variable_order)
{
if (variable_order) {
delete variable_order;
variable_order = NULL;
}
variable_order = new_variable_order;
}
void
LayerConfiguration::SetCustomClassifs(CustomClassifPtree *new_custom_classifs)
{
if (custom_classifs) {
delete custom_classifs;
custom_classifs = NULL;
}
custom_classifs = new_custom_classifs;
}
void LayerConfiguration::SetSpatialWeights(WeightsManPtree *new_spatial_weights)
{
if (spatial_weights) {
delete spatial_weights;
spatial_weights = NULL;
}
spatial_weights = new_spatial_weights;
}
void LayerConfiguration::SetDefaultVars(DefaultVarsPtree *new_default_vars)
{
if (default_vars) {
delete default_vars;
default_vars = NULL;
}
default_vars = new_default_vars;
}
//------------------------------------------------------------------------------
// ProjectConfiguration member functions
//------------------------------------------------------------------------------
ProjectConfiguration::~ProjectConfiguration()
{
for (size_t i=0, sz=layer_confs.size(); i<sz; ++i) {
if (layer_confs[i]) delete layer_confs[i];
}
}
ProjectConfiguration::ProjectConfiguration(const wxString& proj_path)
{
project_fpath = proj_path;
ptree xml_tree;
try{
#ifdef __WIN32__
std::ifstream in_file(project_fpath.wc_str());
read_xml(in_file, xml_tree);
#else
read_xml(std::string(GET_ENCODED_FILENAME(project_fpath)), xml_tree);
#endif
}catch(const boost::property_tree::xml_parser::xml_parser_error& e){
throw GdaException("XML parsing error.");
}
ReadPtree(xml_tree, proj_path);
}
ProjectConfiguration::ProjectConfiguration(wxString prj_title,
LayerConfiguration* layer_conf)
{
project_title = prj_title;
layer_confs.push_back(layer_conf);
}
void ProjectConfiguration::ReadPtree(const ptree& pt,
const wxString& proj_path)
{
wxString tmp(pt.get("project.title", "").c_str(), wxConvUTF8);
project_title = tmp;
const ptree& subtree = pt.get_child("project.layers.layer");
LayerConfiguration* layer_conf = new LayerConfiguration(subtree,
proj_path);
layer_confs.push_back(layer_conf);
}
void ProjectConfiguration::Save(wxString saveFileName)
{
ptree pt;
WritePtree(pt, saveFileName);
//boost::property_tree::xml_writer_settings<char> settings(' ', 4);
boost::property_tree::xml_writer_settings<std::string> settings(' ', 4);
try {
#ifdef __WIN32__
std::ofstream out_file(saveFileName.wc_str());
write_xml(out_file, pt, settings);
out_file.close();
#else
write_xml(std::string(GET_ENCODED_FILENAME(saveFileName)), pt,
std::locale(), settings);
#endif
} catch (const boost::property_tree::ptree_error& e) {
throw GdaException(e.what());
}
}
void ProjectConfiguration::WritePtree(ptree& pt,
const wxString& proj_path)
{
pt.put("project.title", project_title);
ptree& subtree = pt.put("project.layers.layer","");
layer_confs[0]->WritePtree(subtree, proj_path);
}
ProjectConfiguration* ProjectConfiguration::Clone()
{
LayerConfiguration* layer_conf = GetLayerConfiguration();
return new ProjectConfiguration(project_title, layer_conf->Clone());
}