Alexandria  2.14.1
Please provide a description of the project.
CatalogFromTable.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #include <vector>
28 #include "Table/ColumnInfo.h"
29 #include "Table/CastVisitor.h"
30 
31 using namespace std;
32 namespace Euclid {
33 namespace SourceCatalog {
34 
35 class CastSourceIdVisitor: public boost::static_visitor<Source::id_type> {
36  template <typename From>
37  static constexpr bool is_integer() {
39  }
40 
41 public:
43 
44  Source::id_type operator() (const std::string &from) const {
45  return from;
46  }
47 
48  template <typename From>
49  Source::id_type operator() (const From &from, typename std::enable_if<is_integer<From>()>::type* = 0) const {
50  return Source::id_type(static_cast<int64_t>(from));
51  }
52 
53  template <typename From>
54  Source::id_type operator() (const From &, typename std::enable_if<!is_integer<From>()>::type* = 0) const {
55  throw Elements::Exception() << "Only std::string and int64_t are supported types for a source ID, got "
56  << typeid(From).name() << " instead";
57  }
58 };
59 
60 CatalogFromTable::CatalogFromTable(
62  const string& source_id_column_name,
63  std::vector<std::shared_ptr<AttributeFromRow>> attribute_from_row_ptr_vector) {
64 
65  unique_ptr<size_t> source_id_index_ptr = column_info_ptr->find(source_id_column_name);
66  if (source_id_index_ptr == nullptr) {
67  throw Elements::Exception() << "Column info does not have the column " << source_id_column_name;
68  }
69  m_source_id_index = *(source_id_index_ptr);
70 
71  m_attribute_from_row_ptr_vector = std::move(
72  attribute_from_row_ptr_vector);
73 }
74 
75 CatalogFromTable::~CatalogFromTable() {
76  // @todo Auto-generated destructor stub
77 }
78 
79 Euclid::SourceCatalog::Catalog CatalogFromTable::createCatalog(
80  const Euclid::Table::Table& input_table) {
81 
82  vector<Source> source_vector;
83 
84  // Figure out the type of the first row, and then assume all following
85  // must be of the same
86  CastSourceIdVisitor castVisitor;
87 
88  for (auto row : input_table) {
89 
90  auto source_id = boost::apply_visitor(castVisitor, row[m_source_id_index]);
91 
92  vector<shared_ptr<Attribute>> attribute_ptr_vector;
93 
94  for (auto& attribute_from_table_ptr : m_attribute_from_row_ptr_vector) {
95  attribute_ptr_vector.push_back(
96  attribute_from_table_ptr->createAttribute(row));
97  }
98 
99  source_vector.push_back(Source { source_id, move(attribute_ptr_vector) });
100  }
101 
102  return Catalog { source_vector };
103 }
104 
105 } // namespace SourceCatalog
106 } // end of namespace Euclid
boost::variant< int64_t, std::string > id_type
Definition: Source.h:51
STL namespace.
std::unique_ptr< std::size_t > find(const std::string &name) const
Returns the index of a column, given the name of it, or nullptr if there is no column with this name.
Definition: ColumnInfo.cpp:70
STL class.
T push_back(T... args)
T move(T... args)
Represents a table.
Definition: Table.h:49
The Source class includes all information related to a sky source.
Definition: Source.h:48
STL class.
STL class.
Catalog contains a container of sources.
Definition: Catalog.h:47