| 457 | } |
| 458 | |
| 459 | void MSTMapHelper::SaveToWeightsFile(Project* project) |
| 460 | { |
| 461 | // create MST first if needed |
| 462 | if (this->Create(project) == false) { |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | // Get ID variable for spatial weights file |
| 467 | SelectWeightsIdDialog selid_dlg(NULL, project); |
| 468 | if (selid_dlg.ShowModal() != wxID_OK) { |
| 469 | return; |
| 470 | } |
| 471 | TableInterface* table_int = project->GetTableInt(); |
| 472 | wxString id = selid_dlg.GetIDVariable(); |
| 473 | if (id.IsEmpty()) return; |
| 474 | |
| 475 | int col = table_int->FindColId(id); |
| 476 | if (col < 0) return; |
| 477 | |
| 478 | std::vector<wxString> ids; |
| 479 | table_int->GetColData(col, 0, ids); |
| 480 | |
| 481 | // prompt user to select output file |
| 482 | wxString filter = "GWT|*.gwt"; |
| 483 | wxFileDialog dialog(NULL, _("Save Spanning Tree to a Weights File"), |
| 484 | wxEmptyString, wxEmptyString, filter, |
| 485 | wxFD_SAVE | wxFD_OVERWRITE_PROMPT); |
| 486 | if (dialog.ShowModal() != wxID_OK) { |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | // create Spatial weights file |
| 491 | wxFileName fname = wxFileName(dialog.GetPath()); |
| 492 | wxString new_main_dir = fname.GetPathWithSep(); |
| 493 | wxString new_main_name = fname.GetName(); |
| 494 | wxString new_txt = new_main_dir + new_main_name+".gwt"; |
| 495 | wxTextFile file(new_txt); |
| 496 | file.Create(new_txt); |
| 497 | file.Open(new_txt); |
| 498 | file.Clear(); |
| 499 | |
| 500 | wxString header; |
| 501 | header << "0 " << project->GetNumRecords() << " "; |
| 502 | header << "\"" << project->GetProjectTitle() << "\" "; |
| 503 | header << id; |
| 504 | file.AddLine(header); |
| 505 | |
| 506 | for (int i=0; i<mst_edges.size(); i++) { |
| 507 | int f_idx = mst_edges[i].first; |
| 508 | int t_idx = mst_edges[i].second; |
| 509 | |
| 510 | if (f_idx == t_idx) { |
| 511 | continue; |
| 512 | } |
| 513 | |
| 514 | double cost = t_idx > f_idx ? dist_matrix[f_idx][t_idx - f_idx-1] : |
| 515 | dist_matrix[t_idx][f_idx - t_idx-1]; |
| 516 | wxString line1; |
no test coverage detected