-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddprototypebase.cpp
More file actions
412 lines (395 loc) · 18.6 KB
/
Copy pathaddprototypebase.cpp
File metadata and controls
412 lines (395 loc) · 18.6 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "addprototypebase.h"
#include "ui_addprototypebase.h"
#include <QTextCodec>
AddPrototypeBase::AddPrototypeBase(addPrototypeBaseMode mode,SoLIMProject *proj,QWidget *parent) :
mode(mode), QDialog(parent),
ui(new Ui::AddPrototypeBase)
{
ui->setupUi(this);
project = proj;
addedLayer = 0;
ui->progressBar->setVisible(false);
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(2);
ui->covariate_tableWidget->clear();
ui->covariate_tableWidget->setColumnCount(3);
ui->covariate_tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("Filename"));
ui->covariate_tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("Covariate"));
ui->covariate_tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("Categorical?"));
ui->deleteCovariate_btn->setDisabled(true);
ui->PropType_btn->setEnabled(false);
categoricalProps = QStringList();
QTableWidgetItem *item_tmp;
for(size_t i = 0; i<proj->filenames.size();i++){
if(proj->filenames[i].empty()) continue;
ui->covariate_tableWidget->insertRow(ui->covariate_tableWidget->rowCount());
item_tmp = new QTableWidgetItem(proj->filenames[i].c_str());
item_tmp->setFlags(item_tmp->flags()^Qt::ItemIsEditable);
ui->covariate_tableWidget->setItem(ui->covariate_tableWidget->rowCount()-1, 0, item_tmp);
item_tmp = new QTableWidgetItem(proj->layernames[i].c_str());
item_tmp->setFlags(item_tmp->flags()^Qt::ItemIsEditable);
ui->covariate_tableWidget->setItem(ui->covariate_tableWidget->rowCount()-1, 1, item_tmp);
QCheckBox *categoriacl_cb = new QCheckBox();
categoriacl_cb->setChecked(false);
if(proj->layertypes[i]=="CATEGORICAL") categoriacl_cb->setChecked(true);
ui->covariate_tableWidget->setCellWidget(ui->covariate_tableWidget->rowCount()-1,
2,
categoriacl_cb);
}
if(mode==AddPrototypeBase::SAMPLE){
ui->radioButton_poly->setVisible(false);
ui->radioButton_soiltype->setVisible(false);
ui->btn_hint->setVisible(false);
ui->label_singlemode->setVisible(false);
ui->checkBox->setVisible(false);
}
else if(mode==AddPrototypeBase::MAP){
ui->radioButton_poly->setChecked(true);
ui->label_2->setText("Select soil ID field:");
ui->label->setText("Soil Map File");
ui->label_3->setVisible(false);
ui->yFiled_comboBox->setVisible(false);
setWindowTitle("Get Prototype base from map");
ui->PropType_btn->setVisible(false);
}
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
}
AddPrototypeBase::~AddPrototypeBase()
{
delete ui;
}
void AddPrototypeBase::on_addCovariate_btn_clicked()
{
SimpleDialog addGisData(SimpleDialog::ADDGISDATA,project,this);
addGisData.exec();
if(addGisData.filename.isEmpty()) return;
for(int i = 0;i<ui->covariate_tableWidget->rowCount();i++){
if(ui->covariate_tableWidget->item(i,0)->text()==addGisData.filename){
QMessageBox warning;
warning.setText("This file already exists in GIS data.");
warning.exec();
return;
}
if(ui->covariate_tableWidget->item(i,1)->text()==addGisData.covariate){
QMessageBox warning;
warning.setText("This covariate already exists in covariates. Please rename the covariate.");
warning.exec();
return;
}
}
ui->covariate_tableWidget->insertRow(ui->covariate_tableWidget->rowCount());
QTableWidgetItem *item_tmp;
item_tmp = new QTableWidgetItem(addGisData.filename);
item_tmp->setFlags(item_tmp->flags()^Qt::ItemIsEditable);
ui->covariate_tableWidget->setItem(ui->covariate_tableWidget->rowCount()-1, 0, item_tmp);
item_tmp = new QTableWidgetItem(addGisData.covariate);
item_tmp->setFlags(item_tmp->flags()^Qt::ItemIsEditable);
ui->covariate_tableWidget->setItem(ui->covariate_tableWidget->rowCount()-1, 1, item_tmp);
QCheckBox *categoriacl_cb = new QCheckBox();
categoriacl_cb->setChecked(false);
if(addGisData.datatype=="CATEGORICAL") categoriacl_cb->setChecked(true);
ui->covariate_tableWidget->setCellWidget(ui->covariate_tableWidget->rowCount()-1,
2,
categoriacl_cb);
}
void AddPrototypeBase::on_deleteCovariate_btn_clicked()
{
ui->covariate_tableWidget->removeRow(ui->covariate_tableWidget->currentRow());
}
void AddPrototypeBase::on_browseSampleFile_btn_clicked()
{
QString filename;
if(mode==AddPrototypeBase::SAMPLE){
filename = QFileDialog::getOpenFileName(this,
tr("Open samples file"),
project->workingDir,
tr("Sample file(*.csv *.txt)"));
if(filename.isEmpty()) return;
ui->sampleFile_lineEdit->setText(filename);
project->workingDir=QFileInfo(filename).absoluteDir().absolutePath();
QTextCodec *code = QTextCodec::codecForName("UTF-8");
//std::string filename_str = code->fromUnicode(filename.toStdString().c_str()).data();
QString filename1 = QString::fromStdString(code->fromUnicode(filename).data());
QFile file(filename1);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
warn("Cannot open sample file");
ui->sampleFile_lineEdit->clear();
return;
}
QTextStream txtInput(&file);
string line= txtInput.readLine().toStdString();
vector<string> names;
solim::ParseStr(line, ',', names);
QStringList columnNames;
for(size_t i = 0;i<names.size();i++){
columnNames.append(names[i].c_str());
}
propnames = columnNames;
categoricalProps.clear();
ui->PropType_btn->setEnabled(true);
ui->xFiled_comboBox->addItems(columnNames);
ui->yFiled_comboBox->addItems(columnNames);
for (size_t i = 0;i<names.size();i++){
if(names[i]=="x"||names[i]=="X"){
ui->xFiled_comboBox->setCurrentText(names[i].c_str());
}
if(names[i]=="y"||names[i]=="Y"){
ui->yFiled_comboBox->setCurrentText(names[i].c_str());
}
}
} else if (mode==AddPrototypeBase::MAP){
filename = QFileDialog::getOpenFileName(this,
tr("Open samples file"),
project->workingDir,
tr("Sample file(*.shp)"));
if(filename.isEmpty()) return;
ui->sampleFile_lineEdit->setText(filename);
GDALAllRegister();
GDALDataset *poDS;
vector<string> soilIDs;
poDS = (GDALDataset*)GDALOpenEx(filename.toStdString().c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL);
if (poDS == NULL)
{
warn("File open failed.");
ui->sampleFile_lineEdit->clear();
return;
}
OGRLayer *poLayer;
poLayer = poDS->GetLayer(0);
// check if shapefile type is polygon
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
if (poFDefn->GetGeomType() != wkbPolygon) {
warn("Feature type is not polygon type. Cannot be used for data mining.");
ui->sampleFile_lineEdit->clear();return;
}
QStringList fieldnames;
for(int i = 0; i< poFDefn->GetFieldCount(); ++i) {
fieldnames.push_back(poFDefn->GetFieldDefn(i)->GetNameRef());
}
ui->xFiled_comboBox->addItems(fieldnames);
propnames = fieldnames;
categoricalProps.clear();
ui->PropType_btn->setEnabled(true);
}
int first = filename.lastIndexOf('/');
if (first == -1){
first = filename.lastIndexOf('\\');
}
int end = filename.lastIndexOf('.');
QString prototypeBaseName = filename.mid(first+1,end-first-1);
if(ui->lineEdit_basename->text().isEmpty())
ui->lineEdit_basename->setText(prototypeBaseName);
}
void AddPrototypeBase::on_ok_btn_clicked()
{
string sampleFile = ui->sampleFile_lineEdit->text().toStdString();
if(sampleFile.empty()||ui->covariate_tableWidget->rowCount()==0) {
ui->progressBar->setVisible(false);
QMessageBox warning;
warning.setText("Please put in sample file and/or covariate layers!");
warning.setStandardButtons(QMessageBox::Ok);
warning.exec();
return;
}
string prototypeBaseName = ui->lineEdit_basename->text().toStdString();
for(size_t i = 0; i< project->prototypeBaseNames.size();i++){
if(prototypeBaseName==project->prototypeBaseNames[i]){
QMessageBox warning;
warning.setText("This base name exists already. Please change prototype base name!");
warning.setStandardButtons(QMessageBox::Ok);
warning.exec();
ui->progressBar->setVisible(false);
return;
}
}
ui->ok_btn->setEnabled(false);
ui->progressBar->setVisible(true);
ui->progressBar->setValue(0);
vector<string> envFileNames;
vector<string> layernames;
vector<string> datatypes;
for(int i = 0; i<ui->covariate_tableWidget->rowCount();i++){
envFileNames.push_back(ui->covariate_tableWidget->item(i,0)->text().toStdString());
layernames.push_back(ui->covariate_tableWidget->item(i,1)->text().toStdString());
QCheckBox *type = (QCheckBox*) ui->covariate_tableWidget->cellWidget(i,2);
if(type->isChecked()==true){
datatypes.push_back("CATEGORICAL");
} else {
datatypes.push_back("CONTINUOUS");
}
}
solim::EnvDataset *eds = new solim::EnvDataset(envFileNames,datatypes,layernames);
if(eds->Layers.size()<envFileNames.size()){
QMessageBox inconsistentWarn;
inconsistentWarn.setText("File size does not match. Not all layers added.");
inconsistentWarn.exec();
vector<string> newEnvFileNames;
vector<string> newLayernames;
vector<string> newDatatypes;
for(size_t i = 0; i<eds->Layers.size();i++){
for(size_t j = 0; j< envFileNames.size();j++){
if(layernames[j]==eds->Layers[i]->LayerName){
newEnvFileNames.push_back(envFileNames[j]);
newLayernames.push_back(layernames[j]);
newDatatypes.push_back(datatypes[j]);
}
}
}
envFileNames = newEnvFileNames;
layernames = newLayernames;
datatypes = newDatatypes;
}
// update filename in envFilenames
for(size_t i = 0; i<eds->Layers.size(); i++){
for(size_t j = 0; j<envFileNames.size(); j++){
if(eds->Layers.at(i)->LayerName==layernames[j]){
if(eds->Layers.at(i)->baseRef->getFilename()!=envFileNames[j])
envFileNames[j] = eds->Layers.at(i)->baseRef->getFilename();
}
}
}
// update covariates and filename in project
for(size_t i = 0; i<envFileNames.size(); i++){
bool exist = false;
for(size_t j = 0; j<project->layernames.size(); j++){
if(layernames[i]==project->layernames[j]){
project->filenames[j] = envFileNames[i];
project->layertypes[j] = datatypes[i];
exist = true;
}
}
if(!exist){
project->filenames.push_back(envFileNames[i]);
project->layernames.push_back(layernames[i]);
project->layertypes.push_back(datatypes[i]);
addedLayer++;
}
}
if(mode == AddPrototypeBase::SAMPLE){
ui->progressBar->setValue(1);
//ui->progressBar->setRange(0,0);
std::vector<std::string> cate_props_vec;
for(int i=0;i<categoricalProps.size();i++){
cate_props_vec.push_back(categoricalProps.at(i).toStdString());
}
vector<Prototype>* prototypes = Prototype::getPrototypesFromSample(sampleFile,eds, prototypeBaseName,
ui->xFiled_comboBox->currentText().toStdString(),
ui->yFiled_comboBox->currentText().toStdString(),
cate_props_vec);
if(prototypes==nullptr) warn("Sample file open failed!");
//project->prototypeBaseNames.push_back(prototypeBaseName);
int protoNum=prototypes->size();
if(protoNum>0){
project->prototypeBaseNames.push_back(prototypeBaseName);
project->prototypeBaseTypes.push_back("SAMPLE");
vector<string> ids;
for(int i =0; i<protoNum; i++){
ids.push_back(prototypes->at(i).prototypeID);
}
std::sort(ids.begin(),ids.end());
for(int i = 0; i<protoNum;i++){
for(vector<Prototype>::iterator it=prototypes->begin();it!=prototypes->end();it++){
if((*it).prototypeID==ids[i])
project->prototypes.insert(project->prototypes.end(),it,it+1);
}
}
//project->prototypes.insert(project->prototypes.end(),prototypes->begin(),prototypes->end());
} else {
QMessageBox warning;
warning.setText("Samples are not within the range of covariate coordinates. Please check the coordinates of samples or covariates.");
warning.exec();
}
ui->progressBar->setValue(2);
} else if(mode == AddPrototypeBase::MAP){
if(ui->radioButton_soiltype->isChecked()){
vector<Prototype>* prototypes = Prototype::getPrototypesFromMining_soilType(sampleFile,eds,
ui->xFiled_comboBox->currentText().toStdString(),
prototypeBaseName,ui->progressBar);
int protoNum=prototypes->size();
if(protoNum>0){
project->prototypeBaseNames.push_back(prototypeBaseName);
project->prototypeBaseTypes.push_back("MAP");
vector<string> ids;
for(int i =0; i<protoNum; i++){
ids.push_back(prototypes->at(i).prototypeID);
}
std::sort(ids.begin(),ids.end());
for(int i = 0; i<protoNum;i++){
for(vector<Prototype>::iterator it=prototypes->begin();it!=prototypes->end();it++){
if((*it).prototypeID==ids[i])
project->prototypes.insert(project->prototypes.end(),it,it+1);
}
}
//project->prototypes.insert(project->prototypes.end(),prototypes->begin(),prototypes->end());
}
} else {// if(ui->radioButton_poly->isChecked()){
try{
vector<Prototype>* prototypes = Prototype::getPrototypesFromMining_polygon(sampleFile,eds,
ui->xFiled_comboBox->currentText().toStdString(),
prototypeBaseName,ui->progressBar);
int protoNum=prototypes->size();
if(protoNum>0){
project->prototypeBaseNames.push_back(prototypeBaseName);
project->prototypeBaseTypes.push_back("MAP");
vector<string> ids;
for(int i =0; i<protoNum; i++){
ids.push_back(prototypes->at(i).prototypeID);
}
std::sort(ids.begin(),ids.end());
for(int i = 0; i<protoNum;i++){
for(vector<Prototype>::iterator it=prototypes->begin();it!=prototypes->end();it++){
if((*it).prototypeID==ids[i])
project->prototypes.insert(project->prototypes.end(),it,it+1);
}
}
//project->prototypes.insert(project->prototypes.end(),prototypes->begin(),prototypes->end());
}
} catch(invalid_argument msg){
//warn(msg);
}
}
}
close();
}
void AddPrototypeBase::on_btn_hint_clicked()
{
QMessageBox msg;
msg.setTextFormat(Qt::RichText);
msg.setTextInteractionFlags(Qt::TextBrowserInteraction);
msg.setText("<b>Soil Type mode</b>: "
"<ul><li>When each polygon has <b>only one dominant soil type</b>: For each soil type, mining rules from overall distribution of covariate values from all polygon in the soil type"
" <a href=\"https://www.tandfonline.com/doi/abs/10.1080/13658810310001596049\">(Qi and Zhu, 2003)</a>.</li>"
"<li>When each polygon contains <b>one or more soil types</b>: To be continued</li></ul>"
"<b>Polygon mode</b>:"
"<ul><li>When each polygon has <b>only one dominant soil type</b>: For each soil type, mining rules from every polygon, and then merge the rules from polygons to generate the rule for the soil type"
" <a href=\"https://www.sciencedirect.com/science/article/pii/S2095311918619380\">(Cheng et al., 2019)</a>.</li>"
"<li>When each polygon contains <b>one or more soil types</b>: To be continued</li></ul>");
msg.setStandardButtons(QMessageBox::Ok);
//ui->label_hint->setOpenExternalLinks(true);
msg.exec();
}
void AddPrototypeBase::on_covariate_tableWidget_itemSelectionChanged()
{
QList<QTableWidgetItem *> selected_items = ui->covariate_tableWidget->selectedItems();
if(selected_items.size()>0){
vector<int> selected_rows;
QList<QTableWidgetItem *>::iterator i;
for (i = selected_items.begin(); i != selected_items.end(); ++i){
selected_rows.push_back((*i)->row());
}
sort( selected_rows.begin(), selected_rows.end() );
selected_rows.erase( unique( selected_rows.begin(), selected_rows.end() ), selected_rows.end() );
if(selected_rows.size()==1)
ui->deleteCovariate_btn->setEnabled(true);
else ui->deleteCovariate_btn->setDisabled(true);
} else ui->deleteCovariate_btn->setDisabled(true);
}
void AddPrototypeBase::on_PropType_btn_clicked()
{
QString selected = "";
if(categoricalProps.size()>0) selected = categoricalProps.join(';');
itemSelectionWindow editDialog(itemSelectionWindow::CATEGORICALPROPERTYSELECTION,propnames,selected,this);
editDialog.exec();
categoricalProps = editDialog.selectedNames.split(";");
}