gctl/lib/io/dsv_io.cpp

781 lines
21 KiB
C++
Raw Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some tcription of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
2024-12-15 21:14:12 +08:00
#include "dsv_io.h"
2024-09-10 15:45:07 +08:00
2024-12-15 21:14:12 +08:00
gctl::dsv_io::dsv_io()
2024-09-10 15:45:07 +08:00
{
2024-12-15 21:14:12 +08:00
file_ = "";
2024-09-10 15:45:07 +08:00
att_sym_ = '#';
2024-11-21 12:43:10 +08:00
tag_sym_ = '!';
2024-09-10 15:45:07 +08:00
deli_sym_ = ' ';
head_num_ = 0;
2024-12-12 16:55:06 +08:00
row_num_ = 0;
col_num_ = 0;
2024-09-10 15:45:07 +08:00
}
2024-12-15 21:14:12 +08:00
gctl::dsv_io::~dsv_io()
2024-09-10 15:45:07 +08:00
{
clear();
}
2024-12-15 21:14:12 +08:00
gctl::dsv_io::dsv_io(std::string filename, std::string file_exten, table_headtype_e t)
2024-09-10 15:45:07 +08:00
{
2024-12-15 21:14:12 +08:00
file_ = "";
2024-09-10 15:45:07 +08:00
att_sym_ = '#';
2024-11-21 12:43:10 +08:00
tag_sym_ = '!';
2024-09-10 15:45:07 +08:00
deli_sym_ = ' ';
head_num_ = 0;
2024-12-12 16:55:06 +08:00
row_num_ = 0;
col_num_ = 0;
2024-12-16 09:31:46 +08:00
if (file_exten == ".csv") load_csv(filename, t);
else load_text(filename, file_exten, t);
2024-09-10 15:45:07 +08:00
}
2024-12-15 21:14:12 +08:00
void gctl::dsv_io::clear()
2024-09-10 15:45:07 +08:00
{
2024-12-15 21:14:12 +08:00
file_ = "";
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';
head_num_ = 0;
row_num_ = 0;
col_num_ = 0;
2024-12-16 09:31:46 +08:00
2024-11-04 16:03:39 +08:00
destroy_vector(heads_);
destroy_vector(annotates_);
destroy_vector(tags_);
destroy_vector(table_);
2024-09-10 15:45:07 +08:00
return;
}
2024-12-31 09:29:54 +08:00
void gctl::dsv_io::get_row_names(std::vector<std::string> &names)
{
names.resize(row_num_);
for (size_t i = 1; i < row_num_; i++)
{
2025-01-01 22:43:59 +08:00
names[i - 1] = table_[i][0].str_;
2024-12-31 09:29:54 +08:00
}
return;
}
void gctl::dsv_io::get_column_names(std::vector<std::string> &names)
{
names.resize(col_num_);
for (size_t i = 1; i < col_num_; i++)
{
2025-01-01 22:43:59 +08:00
names[i - 1] = table_[0][i].str_;
2024-12-31 09:29:54 +08:00
}
return;
}
void gctl::dsv_io::set_row_names(const std::vector<std::string> &names)
{
for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++)
{
2025-01-01 22:43:59 +08:00
table_[i][0].str_ = names[i - 1];
2024-12-31 09:29:54 +08:00
}
table_[0][0].str_ = "row_name";
return;
}
void gctl::dsv_io::set_column_names(const std::vector<std::string> &names)
{
for (size_t i = 1; i <= std::min(col_num_, (int) names.size()); i++)
{
2025-01-01 22:43:59 +08:00
table_[0][i].str_ = names[i - 1];
2024-12-31 09:29:54 +08:00
}
return;
}
2025-01-02 09:40:27 +08:00
void gctl::dsv_io::set_row_type(cell_type_e t, int idx)
{
if (idx > row_num_ || idx <= 0)
{
throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
}
for (size_t i = 0; i <= col_num_; i++)
{
table_[idx][i].type_ = t;
}
}
void gctl::dsv_io::set_row_type(cell_type_e t, std::string name)
{
set_row_type(t, name_index(name, true));
}
void gctl::dsv_io::set_column_type(cell_type_e t, int idx)
{
if (idx > col_num_ || idx <= 0)
{
throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
}
for (size_t i = 0; i <= row_num_; i++)
{
table_[i][idx].type_ = t;
}
return;
}
void gctl::dsv_io::set_column_type(cell_type_e t, std::string name)
{
set_column_type(t, name_index(name, false));
}
2024-12-15 21:14:12 +08:00
void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table_headtype_e t)
2024-09-10 15:45:07 +08:00
{
std::ifstream infile;
open_infile(infile, filename, file_exten);
2024-12-30 22:09:39 +08:00
file_ = filename + file_exten;
2024-12-25 19:06:27 +08:00
int h = 0;
2024-09-10 15:45:07 +08:00
std::string tmp_line;
2024-12-16 09:31:46 +08:00
std::vector<std::string> lines;
2024-09-10 15:45:07 +08:00
while (std::getline(infile, tmp_line))
{
2024-12-25 19:06:27 +08:00
if (tmp_line.empty()) continue; // 跳过空行 空行不会并记入头信息计数中
2024-11-21 12:43:10 +08:00
else if (tmp_line[0] == att_sym_) // 注释行或者标记行 # #!
2024-09-10 15:45:07 +08:00
{
2024-11-21 12:43:10 +08:00
if (tmp_line[1] == tag_sym_) // #!
{
tmp_line = tmp_line.substr(2); // 去掉前两个字符
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
tags_.push_back(tmp_line);
continue;
}
// #
tmp_line = tmp_line.substr(1); // 去掉第一个字符
2024-09-10 15:45:07 +08:00
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
annotates_.push_back(tmp_line);
}
2024-12-25 19:06:27 +08:00
else if (h < head_num_) //读入到头信息中
{
heads_.push_back(tmp_line);
h++;
}
2024-12-16 09:31:46 +08:00
else lines.push_back(tmp_line);
2024-09-10 15:45:07 +08:00
}
infile.close();
2024-12-12 23:23:55 +08:00
2024-12-30 22:09:39 +08:00
table_.resize(lines.size());
2024-11-04 16:03:39 +08:00
2024-12-30 22:09:39 +08:00
int cn, cn_max = 0;
2024-12-12 16:55:06 +08:00
std::vector<std::string> tmp_cols;
2024-12-30 22:09:39 +08:00
for (size_t i = 0; i < lines.size(); i++)
2024-12-12 16:55:06 +08:00
{
tmp_cols.clear();
2024-12-16 09:31:46 +08:00
parse_string_to_vector(lines[i], deli_sym_, tmp_cols);
2024-12-12 16:55:06 +08:00
2024-12-16 09:31:46 +08:00
// 动态调整列数
2024-12-12 16:55:06 +08:00
cn = tmp_cols.size();
2024-12-30 22:09:39 +08:00
cn_max = std::max(cn, cn_max);
2024-12-12 16:55:06 +08:00
table_[i].resize(tmp_cols.size());
for (size_t j = 0; j < tmp_cols.size(); j++)
2024-11-04 16:03:39 +08:00
{
2025-01-02 09:40:27 +08:00
table_[i][j].value(tmp_cols[j]);
2024-12-12 16:55:06 +08:00
}
}
2024-11-04 16:03:39 +08:00
2024-12-16 09:31:46 +08:00
// 补齐可能的空格
2024-12-17 09:59:06 +08:00
table_cell empty_cell;
2024-12-30 22:09:39 +08:00
for (size_t i = 0; i < table_.size(); i++)
2024-12-12 16:55:06 +08:00
{
cn = table_[i].size();
2024-12-30 22:09:39 +08:00
for (size_t j = cn; j < cn_max; j++)
2024-12-12 16:55:06 +08:00
{
table_[i].push_back(empty_cell);
2024-11-04 16:03:39 +08:00
}
}
2024-12-13 10:11:47 +08:00
2024-12-30 22:09:39 +08:00
std::vector<table_cell> empty_line;
2024-12-31 09:29:54 +08:00
if (t == NoHead) // 无表头 需要补齐空白的列头和行头
2024-12-28 09:42:02 +08:00
{
2024-12-30 22:09:39 +08:00
row_num_ = table_.size();
col_num_ = table_[0].size();
table_.emplace(table_.begin(), empty_line);
table_[0].resize(col_num_);
for (size_t i = 0; i < table_.size(); i++)
2024-12-28 09:42:02 +08:00
{
2024-12-30 22:09:39 +08:00
table_[i].emplace(table_[i].begin(), empty_cell);
2024-12-28 09:42:02 +08:00
}
}
2024-12-31 09:29:54 +08:00
if (t == ColumnHead) // 有列头 需要补齐空白的行头
2024-12-30 22:09:39 +08:00
{
row_num_ = table_.size() - 1;
col_num_ = table_[0].size();
for (size_t i = 0; i < table_.size(); i++)
{
table_[i].emplace(table_[i].begin(), empty_cell);
}
}
2024-12-31 09:29:54 +08:00
if (t == RowHead) // 有行头 需要补齐空白的列头
2024-12-30 22:09:39 +08:00
{
row_num_ = table_.size();
col_num_ = table_[0].size() - 1;
table_.emplace(table_.begin(), empty_line);
table_[0].resize(col_num_ + 1);
}
2024-12-31 09:29:54 +08:00
if (t == BothHead) // 有行头和列头
2024-12-30 22:09:39 +08:00
{
row_num_ = table_.size() - 1;
col_num_ = table_[0].size() - 1;
}
2024-12-16 09:31:46 +08:00
destroy_vector(lines);
2024-12-13 10:11:47 +08:00
return;
}
2024-12-15 21:14:12 +08:00
void gctl::dsv_io::load_csv(std::string filename, table_headtype_e t)
2024-12-13 10:11:47 +08:00
{
set_delimeter(',');
load_text(filename, ".csv", t);
2024-09-10 15:45:07 +08:00
return;
}
2024-12-16 09:31:46 +08:00
void gctl::dsv_io::save_text(std::string filename, std::string file_exten)
2024-09-10 15:45:07 +08:00
{
std::ofstream outfile;
open_outfile(outfile, filename, file_exten);
for (int i = 0; i < heads_.size(); i++)
{
outfile << heads_[i] << std::endl;
}
for (int i = 0; i < tags_.size(); i++)
{
2024-11-21 12:43:10 +08:00
outfile << "#! " << tags_[i] << std::endl;
2024-09-10 15:45:07 +08:00
}
for (int i = 0; i < annotates_.size(); i++)
{
outfile << "# " << annotates_[i] << std::endl;
}
2024-12-31 09:29:54 +08:00
// 单独处理第一行 即列头
2024-12-30 22:09:39 +08:00
bool line_st = false;
if (table_[0][0].out_ok_ && table_[0][0].str_ != "")
{
outfile << table_[0][0].str_;
line_st = true;
}
for (int j = 1; j <= col_num_; j++)
{
if (line_st && table_[0][j].out_ok_ && table_[0][j].str_ != "") outfile << deli_sym_ << table_[0][j].str_; // line started
else if (table_[0][j].out_ok_ && table_[0][j].str_ != "") // line not started
{
outfile << table_[0][j].str_;
line_st = true; // start line
}
}
if (line_st) outfile << std::endl;
2024-12-31 09:29:54 +08:00
// 处理余下的行
2024-12-30 22:09:39 +08:00
for (int i = 1; i <= row_num_; i++)
2024-09-10 15:45:07 +08:00
{
2024-12-28 09:42:02 +08:00
line_st = false;
2024-12-31 09:29:54 +08:00
// 单独处理第一列 即行头
2024-12-30 22:09:39 +08:00
if (table_[i][0].out_ok_ && table_[i][0].str_ != "")
2024-12-28 09:42:02 +08:00
{
outfile << table_[i][0].str_;
line_st = true;
}
2024-12-30 22:09:39 +08:00
for (int j = 1; j <= col_num_; j++)
2024-11-04 16:03:39 +08:00
{
2024-12-30 22:09:39 +08:00
if (line_st && table_[i][j].out_ok_) outfile << deli_sym_ << table_[i][j].str_; // line started
else if (table_[i][j].out_ok_) // line not started
2024-12-28 09:42:02 +08:00
{
outfile << table_[i][j].str_;
line_st = true; // start line
}
2024-11-04 16:03:39 +08:00
}
2024-12-12 16:55:06 +08:00
outfile << std::endl;
2024-11-04 16:03:39 +08:00
}
2024-12-30 22:09:39 +08:00
2024-12-12 16:55:06 +08:00
outfile.close();
return;
}
2024-12-16 09:31:46 +08:00
void gctl::dsv_io::save_csv(std::string filename)
2024-12-13 10:11:47 +08:00
{
set_delimeter(',');
2024-12-16 09:31:46 +08:00
save_text(filename, ".csv");
2024-12-13 10:11:47 +08:00
return;
}
2024-12-15 21:14:12 +08:00
void gctl::dsv_io::init_table(int row, int col, table_headtype_e t)
2024-12-12 16:55:06 +08:00
{
row_num_ = row;
col_num_ = col;
2024-12-31 09:29:54 +08:00
// 初始的列头和行头均为空白
2024-12-30 22:09:39 +08:00
table_.resize(row_num_ + 1);
for (size_t i = 0; i < row_num_ + 1; i++)
2024-11-04 16:03:39 +08:00
{
2024-12-30 22:09:39 +08:00
table_[i].resize(col_num_ + 1);
2024-12-16 09:31:46 +08:00
}
2024-12-13 10:11:47 +08:00
return;
}
2024-12-31 09:29:54 +08:00
void gctl::dsv_io::info(table_headtype_e t)
2024-12-13 10:11:47 +08:00
{
std::clog << "File: " << file_ << "\n------------\n";
std::clog << "Head(s): " << head_num_ << "\n";
std::clog << "Annotation(s): " << annotates_.size() << "\n";
std::clog << "Tag(s): " << tags_.size() << "\n";
2024-12-31 09:29:54 +08:00
std::clog << "------------\n";
2024-12-13 10:11:47 +08:00
2024-12-31 09:29:54 +08:00
if (t == ColumnHead || t == BothHead)
2024-12-13 10:11:47 +08:00
{
2024-12-31 09:29:54 +08:00
std::clog << "Columns:\n";
for (size_t i = 1; i <= col_num_; i++)
2024-12-13 10:11:47 +08:00
{
2024-12-31 09:29:54 +08:00
if (table_[0][i].str_ != "")
{
2025-01-02 09:40:27 +08:00
std::clog << table_[0][i].str_ << " | ";
if (table_[1][i].type_ == String) std::clog << "String | ";
if (table_[1][i].type_ == Int) std::clog << "Int | ";
if (table_[1][i].type_ == Float) std::clog << "Float | ";
std::clog << table_[1][i].str_ << " -> " << table_[row_num_][i].str_;
2024-12-31 09:29:54 +08:00
}
else
{
2025-01-02 09:40:27 +08:00
std::clog << "C" + std::to_string(i) << " | ";
if (table_[1][i].type_ == String) std::clog << "String | ";
if (table_[1][i].type_ == Int) std::clog << "Int | ";
if (table_[1][i].type_ == Float) std::clog << "Float | ";
std::clog << table_[1][i].str_ << " -> " << table_[row_num_][i].str_;
2024-12-31 09:29:54 +08:00
}
if (!table_[0][i].out_ok_) std::clog << " (No output)";
std::clog << std::endl;
2024-12-13 10:11:47 +08:00
}
2024-12-31 09:29:54 +08:00
std::clog << "============\n";
}
if (t == RowHead || t == BothHead)
{
std::clog << "Rows:\n";
for (size_t i = 1; i <= row_num_; i++)
2024-12-13 10:11:47 +08:00
{
2024-12-31 09:29:54 +08:00
if (table_[i][0].str_ != "")
{
2025-01-02 09:40:27 +08:00
std::clog << table_[i][0].str_ << " | ";
if (table_[i][1].type_ == String) std::clog << "String | ";
if (table_[i][1].type_ == Int) std::clog << "Int | ";
if (table_[i][1].type_ == Float) std::clog << "Float | ";
std::clog << table_[i][1].str_ << " -> " << table_[i][col_num_].str_;
2024-12-31 09:29:54 +08:00
}
else
{
2025-01-02 09:40:27 +08:00
std::clog << "R" + std::to_string(i) << " | ";
if (table_[i][1].type_ == String) std::clog << "String | ";
if (table_[i][1].type_ == Int) std::clog << "Int | ";
if (table_[i][1].type_ == Float) std::clog << "Float | ";
std::clog << table_[i][1].str_ << " -> " << table_[i][col_num_].str_;
2024-12-31 09:29:54 +08:00
}
2024-12-28 13:47:25 +08:00
2024-12-31 09:29:54 +08:00
if (!table_[i][0].out_ok_) std::clog << " (No output)";
std::clog << std::endl;
}
std::clog << "============\n";
2024-12-13 10:11:47 +08:00
}
return;
2024-12-15 11:22:25 +08:00
}
2024-12-17 09:59:06 +08:00
int gctl::dsv_io::name_index(std::string name, bool iter_row)
2024-12-16 09:31:46 +08:00
{
2024-12-31 09:29:54 +08:00
// 拾取行号或列号 格式为R<id>和C<id>
2024-12-30 22:09:39 +08:00
std::smatch ret;
std::regex patr("R(\\d*)"), patc("C(\\d*)");
if (regex_search(name, ret, patr))
{
int r = atoi(std::string(ret[1]).c_str());
if (r >= 1 && r <= row_num_) return r;
else return -1;
}
if (regex_search(name, ret, patc))
{
int c = atoi(std::string(ret[1]).c_str());
if (c >= 1 && c <= col_num_) return c;
else return -1;
}
2024-12-17 09:59:06 +08:00
if (iter_row)
{
2024-12-30 22:09:39 +08:00
for (size_t i = 1; i <= row_num_; i++)
2024-12-17 09:59:06 +08:00
{
if (table_[i][0].str_ == name) return i;
}
2024-12-28 13:47:25 +08:00
return -1;
2024-12-17 09:59:06 +08:00
}
else
{
2024-12-30 22:09:39 +08:00
for (size_t i = 1; i <= col_num_; i++)
2024-12-17 09:59:06 +08:00
{
if (table_[0][i].str_ == name) return i;
}
2024-12-28 13:47:25 +08:00
return -1;
2024-12-17 09:59:06 +08:00
}
2024-12-16 09:31:46 +08:00
}
2024-12-28 13:47:25 +08:00
void gctl::dsv_io::column_output(int idx, switch_type_e s)
2024-12-28 09:42:02 +08:00
{
2024-12-30 22:09:39 +08:00
if (idx > col_num_ || idx <= 0)
2024-12-28 09:42:02 +08:00
{
throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
}
2024-12-30 22:09:39 +08:00
for (size_t i = 0; i <= row_num_; i++)
2024-12-28 09:42:02 +08:00
{
2024-12-30 22:09:39 +08:00
if (s == Enable) table_[i][idx].out_ok_ = true;
else table_[i][idx].out_ok_ = false;
2024-12-28 09:42:02 +08:00
}
return;
}
2024-12-28 13:47:25 +08:00
void gctl::dsv_io::column_output(std::string name, switch_type_e s)
2024-12-28 09:42:02 +08:00
{
2024-12-28 13:47:25 +08:00
column_output(name_index(name), s);
2024-12-28 09:42:02 +08:00
return;
}
2024-12-28 13:47:25 +08:00
void gctl::dsv_io::row_output(int idx, switch_type_e s)
2024-12-28 09:42:02 +08:00
{
2024-12-30 22:09:39 +08:00
if (idx > row_num_ || idx <= 0)
2024-12-28 09:42:02 +08:00
{
throw std::runtime_error("[gctl::dsv_io] Invalid row index.");
}
2024-12-30 22:09:39 +08:00
for (size_t i = 0; i <= col_num_; i++)
2024-12-28 09:42:02 +08:00
{
2024-12-30 22:09:39 +08:00
if (s == Enable) table_[idx][i].out_ok_ = true;
else table_[idx][i].out_ok_ = false;
2024-12-28 09:42:02 +08:00
}
return;
}
2024-12-28 13:47:25 +08:00
void gctl::dsv_io::row_output(std::string name, switch_type_e s)
2024-12-28 09:42:02 +08:00
{
2024-12-28 13:47:25 +08:00
row_output(name_index(name), s);
2024-12-28 09:42:02 +08:00
return;
}
2025-01-01 17:14:19 +08:00
void gctl::dsv_io::add_column(std::string name)
{
table_cell empty_cell;
for (size_t i = 0; i < table_.size(); i++)
{
table_[i].push_back(empty_cell);
}
table_[0].back().str_ = name;
col_num_++;
return;
}
void gctl::dsv_io::add_column(int idx, std::string name)
{
table_cell empty_cell;
if (idx <= 0)
{
for (size_t i = 0; i < table_.size(); i++)
{
table_[i].push_back(empty_cell);
}
table_[0].back().str_ = name;
col_num_++;
}
else
{
for (size_t i = 0; i < table_.size(); i++)
{
table_[i].insert(table_[i].begin() + idx, empty_cell);
}
table_[0][idx].str_ = name;
col_num_++;
}
return;
}
void gctl::dsv_io::add_column(std::string id_name, std::string name)
{
add_column(name_index(id_name), name);
return;
}
2025-01-01 22:43:59 +08:00
void gctl::dsv_io::add_row(std::string name)
{
std::vector<table_cell> empty_line;
table_.push_back(empty_line);
table_.back().resize(col_num_ + 1);
table_.back().front().str_ = name;
row_num_++;
return;
}
void gctl::dsv_io::add_row(int idx, std::string name)
{
std::vector<table_cell> empty_line;
if (idx <= 0)
{
table_.push_back(empty_line);
table_.back().resize(col_num_ + 1);
table_.back().front().str_ = name;
row_num_++;
}
else
{
table_.insert(table_.begin() + idx, empty_line);
table_[idx].resize(col_num_ + 1);
table_[idx].front().str_ = name;
row_num_++;
}
return;
}
void gctl::dsv_io::add_row(std::string id_name, std::string name)
{
add_row(name_index(id_name), name);
return;
}
2024-12-17 09:59:06 +08:00
gctl::geodsv_io::geodsv_io(){}
gctl::geodsv_io::~geodsv_io(){}
2024-12-16 09:31:46 +08:00
gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_headtype_e t)
{
file_ = "";
att_sym_ = '#';
tag_sym_ = '!';
deli_sym_ = ' ';
head_num_ = 0;
row_num_ = 0;
col_num_ = 0;
if (file_exten == ".csv") load_csv(filename, t);
else load_text(filename, file_exten, t);
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::fill_column_point2dc(const array<point2dc> &data, int xid, int yid, int p)
2024-12-15 11:22:25 +08:00
{
2024-12-30 22:09:39 +08:00
if (xid > col_num_ || yid > col_num_ || xid == yid || xid <= 0 || yid <= 0)
2024-12-15 11:22:25 +08:00
{
2024-12-17 09:59:06 +08:00
throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
2024-12-15 11:22:25 +08:00
}
std::stringstream ss;
std::string s;
2024-12-30 22:09:39 +08:00
for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++)
2024-12-15 11:22:25 +08:00
{
ss.clear();
2024-12-30 22:09:39 +08:00
ss << data[i - 1].x;
2024-12-15 11:22:25 +08:00
ss >> s;
2024-12-30 22:09:39 +08:00
table_[i][xid].str_ = s;
2024-12-15 11:22:25 +08:00
ss.clear();
2024-12-30 22:09:39 +08:00
ss << data[i - 1].y;
2024-12-15 11:22:25 +08:00
ss >> s;
2024-12-30 22:09:39 +08:00
table_[i][yid].str_ = s;
2024-12-15 11:22:25 +08:00
}
return;
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::fill_column_point2dc(const array<point2dc> &data, std::string xname, std::string yname, int p)
2024-12-17 09:59:06 +08:00
{
2025-01-01 17:14:19 +08:00
fill_column_point2dc(data, name_index(xname, false), name_index(yname, false), p);
2024-12-17 09:59:06 +08:00
return;
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::fill_column_point3dc(const array<point3dc> &data, int xid, int yid, int zid, int p)
2024-12-15 11:22:25 +08:00
{
2024-12-30 22:09:39 +08:00
if (xid > col_num_ || yid > col_num_ || zid > col_num_ || xid == yid || yid == zid || xid == zid
|| xid <= 0 || yid <= 0 || zid <= 0)
2024-12-15 11:22:25 +08:00
{
2024-12-17 09:59:06 +08:00
throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
2024-12-15 11:22:25 +08:00
}
std::stringstream ss;
std::string s;
2024-12-30 22:09:39 +08:00
for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++)
2024-12-15 11:22:25 +08:00
{
ss.clear();
2024-12-30 22:09:39 +08:00
ss << data[i - 1].x;
2024-12-15 11:22:25 +08:00
ss >> s;
2024-12-30 22:09:39 +08:00
table_[i][xid].str_ = s;
2024-12-15 11:22:25 +08:00
ss.clear();
2024-12-30 22:09:39 +08:00
ss << data[i - 1].y;
2024-12-15 11:22:25 +08:00
ss >> s;
2024-12-30 22:09:39 +08:00
table_[i][yid].str_ = s;
2024-12-15 11:22:25 +08:00
ss.clear();
2024-12-30 22:09:39 +08:00
ss << data[i - 1].z;
2024-12-15 11:22:25 +08:00
ss >> s;
2024-12-30 22:09:39 +08:00
table_[i][zid].str_ = s;
2024-12-15 11:22:25 +08:00
}
return;
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::fill_column_point3dc(const array<point3dc> &data, std::string xname, std::string yname, std::string zname, int p)
2024-12-17 09:59:06 +08:00
{
2025-01-01 17:14:19 +08:00
fill_column_point3dc(data, name_index(xname, false), name_index(yname, false), name_index(zname, false), p);
2024-12-17 09:59:06 +08:00
return;
}
2025-01-01 22:43:59 +08:00
void gctl::geodsv_io::fill_column_point3ds(const array<point3ds> &data, int rid, int pid, int tid, int p)
{
if (rid > col_num_ || pid > col_num_ || tid > col_num_ || rid == pid || pid == tid || tid == rid
|| rid <= 0 || pid <= 0 || tid <= 0)
{
throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
}
std::stringstream ss;
std::string s;
for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++)
{
ss.clear();
ss << data[i - 1].rad;
ss >> s;
table_[i][rid].str_ = s;
ss.clear();
ss << data[i - 1].lon;
ss >> s;
table_[i][pid].str_ = s;
ss.clear();
ss << data[i - 1].lat;
ss >> s;
table_[i][tid].str_ = s;
}
return;
}
void gctl::geodsv_io::fill_column_point3ds(const array<point3ds> &data, std::string rname, std::string pname, std::string tname, int p)
{
fill_column_point3ds(data, name_index(rname, false), name_index(pname, false), name_index(tname, false), p);
return;
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::get_column_point2dc(array<point2dc> &data, int xid, int yid)
2024-12-15 11:22:25 +08:00
{
2024-12-30 22:09:39 +08:00
if (xid > col_num_ || yid > col_num_ || xid == yid || xid <= 0 || yid <= 0)
2024-12-15 11:22:25 +08:00
{
2024-12-17 09:59:06 +08:00
throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
2024-12-15 11:22:25 +08:00
}
2024-12-30 22:09:39 +08:00
data.resize(row_num_);
for (size_t i = 1; i <= row_num_; i++)
2024-12-15 11:22:25 +08:00
{
2024-12-30 22:09:39 +08:00
data[i - 1].x = table_[i][xid].value<double>();
data[i - 1].y = table_[i][yid].value<double>();
2024-12-15 11:22:25 +08:00
}
return;
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::get_column_point2dc(array<point2dc> &data, std::string xname, std::string yname)
2024-12-17 09:59:06 +08:00
{
2025-01-01 17:14:19 +08:00
get_column_point2dc(data, name_index(xname, false), name_index(yname, false));
2024-12-17 09:59:06 +08:00
return;
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::get_column_point3dc(array<point3dc> &data, int xid, int yid, int zid)
2024-12-15 11:22:25 +08:00
{
2024-12-30 22:09:39 +08:00
if (xid > col_num_ || yid > col_num_ || zid > col_num_ || xid == yid || yid == zid || xid == zid
|| xid <= 0 || yid <= 0 || zid <= 0)
2024-12-15 11:22:25 +08:00
{
2024-12-17 09:59:06 +08:00
throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
2024-12-15 11:22:25 +08:00
}
2024-12-30 22:09:39 +08:00
data.resize(row_num_);
for (size_t i = 1; i <= row_num_; i++)
2024-12-15 11:22:25 +08:00
{
2024-12-30 22:09:39 +08:00
data[i - 1].x = table_[i][xid].value<double>();
data[i - 1].y = table_[i][yid].value<double>();
data[i - 1].z = table_[i][zid].value<double>();
2024-12-15 11:22:25 +08:00
}
return;
2024-12-17 09:59:06 +08:00
}
2025-01-01 17:14:19 +08:00
void gctl::geodsv_io::get_column_point3dc(array<point3dc> &data, std::string xname, std::string yname, std::string zname)
2024-12-17 09:59:06 +08:00
{
2025-01-01 17:14:19 +08:00
get_column_point3dc(data, name_index(xname, false), name_index(yname, false), name_index(zname, false));
2024-12-17 09:59:06 +08:00
return;
2025-01-01 22:43:59 +08:00
}
void gctl::geodsv_io::get_column_point3ds(array<point3ds> &data, int rid, int pid, int tid)
{
if (rid > col_num_ || pid > col_num_ || tid > col_num_ || rid == pid || pid == tid || tid == rid
|| rid <= 0 || pid <= 0 || tid <= 0)
{
throw std::runtime_error("[gctl::geodsv_io] Invalid column index.");
}
data.resize(row_num_);
for (size_t i = 1; i <= row_num_; i++)
{
data[i - 1].rad = table_[i][rid].value<double>();
data[i - 1].lon = table_[i][pid].value<double>();
data[i - 1].lat = table_[i][tid].value<double>();
}
return;
}
void gctl::geodsv_io::get_column_point3ds(array<point3ds> &data, std::string rname, std::string pname, std::string tname)
{
get_column_point3ds(data, name_index(rname, false), name_index(pname, false), name_index(tname, false));
return;
2024-09-10 15:45:07 +08:00
}