-
Notifications
You must be signed in to change notification settings - Fork 156
/
DefaultVarsPtree.cpp
151 lines (136 loc) · 3.89 KB
/
DefaultVarsPtree.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
/**
* 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 <set>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include "GdaException.h"
#include "logger.h"
#include "DefaultVarsPtree.h"
wxString DefaultVar::ToStr() const
{
wxString s;
s << "Default Var Info:\n";
s << " name: " << name << "\n";
if (!time_id.IsEmpty()) s << " time_id: " << time_id << "\n";
return s;
}
DefaultVarsPtree::DefaultVarsPtree()
{
}
DefaultVarsPtree::DefaultVarsPtree(const DefaultVarsPtree& o)
{
std::list<DefaultVar>::const_iterator it;
for (it = o.default_vars_list.begin(); it != o.default_vars_list.end(); ++it) {
default_vars_list.push_back( *it );
}
}
DefaultVarsPtree::DefaultVarsPtree(const boost::property_tree::ptree& pt,
const wxString& proj_path)
{
ReadPtree(pt, proj_path);
}
DefaultVarsPtree::~DefaultVarsPtree()
{
}
DefaultVarsPtree* DefaultVarsPtree::Clone()
{
return new DefaultVarsPtree(*this);
}
void DefaultVarsPtree::ReadPtree(const boost::property_tree::ptree& pt,
const wxString& proj_path)
{
LOG_MSG("Entering DefaultVarsPtree::ReadPtree");
using boost::property_tree::ptree;
default_vars_list.clear();
try {
try {
pt.get_child("default_vars");
}
catch (boost::property_tree::ptree_bad_path& e) {
// spatial_weights is optional
return;
}
// iterate over each child of default_vars
BOOST_FOREACH(const ptree::value_type &v,
pt.get_child("default_vars")) {
wxString key = v.first.data();
if (key == "default_var") {
DefaultVar dv;
BOOST_FOREACH(const ptree::value_type &v, v.second) {
wxString key = v.first.data();
if (key == "name") {
wxString s = v.second.data();
dv.name = s;
} else if (key == "time_id") {
wxString s = v.second.data();
dv.time_id = s;
} else {
wxString msg("Warning: unrecognized key: ");
msg << key;
}
}
default_vars_list.push_back(dv);
} else {
wxString msg("Warning: unrecognized key: ");
msg << key;
}
}
} catch (std::exception &e) {
throw GdaException(e.what());
}
LOG_MSG("Exiting DefaultVarsPtree::ReadPtree");
}
void DefaultVarsPtree::WritePtree(boost::property_tree::ptree& pt,
const wxString& proj_path)
{
using boost::property_tree::ptree;
try {
ptree& sub = pt.put("default_vars", "");
// Write each spatial weights meta info definition
BOOST_FOREACH(const DefaultVar& v, default_vars_list) {
ptree& ssub = sub.add("default_var", "");
ssub.put("name", v.name);
if (!v.time_id.IsEmpty()) ssub.put("time_id", v.time_id);
}
} catch (std::exception &e) {
throw GdaException(e.what());
}
}
const std::list<DefaultVar>& DefaultVarsPtree::GetDefaultVarList() const
{
return default_vars_list;
}
void DefaultVarsPtree::SetDefaultVarList(const std::vector<wxString>& names,
const std::vector<wxString>& time_ids)
{
default_vars_list.clear();
if (names.size() != time_ids.size()) return;
for (int i=0, sz=names.size(); i<sz; ++i) {
DefaultVar v;
v.name = names[i];
v.time_id = time_ids[i];
default_vars_list.push_back(v);
}
}
wxString DefaultVarsPtree::ToStr() const
{
wxString s;
BOOST_FOREACH(const DefaultVar& w, default_vars_list) s << w.ToStr();
return s;
}