Commit 4445ec6c authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH: add keywordlist import and export method in GCP and GCPParam

parent 850ce475
......@@ -69,8 +69,15 @@ public:
void Print(std::ostream& os) const;
std::string ToJSON(bool multiline=false) const;
/** Keywordlist export */
void ToKeywordlist(MetaData::Keywordlist & kwl, const std::string & prefix) const;
/** Keywordlist import */
static GCP FromKeywordlist(const MetaData::Keywordlist & kwl, const std::string & prefix);
};
namespace Projection
{
......@@ -86,6 +93,12 @@ struct OTBMetadata_EXPORT GCPParam
// JSON export
std::string ToJSON(bool multiline=false) const;
/** Keywordlist export */
void ToKeywordlist(MetaData::Keywordlist & kwl, const std::string & prefix) const;
/** Keywordlist import */
void FromKeywordlist(const MetaData::Keywordlist & kwl, const std::string & prefix);
};
/** \struct RPCParam
......
......@@ -22,6 +22,53 @@
#include <iostream>
namespace
{
constexpr int STRING_PRECISION = 20;
// the precision of std::to_string is limited to 6 digits
template <typename T>
std::string to_string_with_precision(const T value)
{
std::ostringstream out;
out.precision(STRING_PRECISION);
out << std::fixed << value;
return out.str();
}
template<class T>
void KeywordlistToVector(std::vector<T> & vector,
const otb::MetaData::Keywordlist & kwl,
const std::string & prefix)
{
vector.clear();
const auto size = std::stoi(kwl.at(prefix + ".number"));
for (int i = 0; i < size; i++)
{
auto t = T::FromKeywordlist(kwl, prefix + "_" + to_string_with_precision(i) + ".");
vector.push_back(t);
}
}
template <class T>
void VectorToKeywordList(otb::MetaData::Keywordlist & kwl,
const std::vector<T> & input,
const std::string & prefix)
{
int i = 0;
for (const auto & elem: input)
{
elem.ToKeywordlist(kwl, prefix + "_" + to_string_with_precision(i) + ".");
i++;
}
kwl.insert({prefix + ".number" , to_string_with_precision(i)});
}
}
namespace otb
{
......@@ -58,6 +105,36 @@ std::string GCP::ToJSON(bool multiline) const
return oss.str();
}
void GCP::ToKeywordlist(MetaData::Keywordlist & kwl, const std::string & prefix) const
{
kwl.insert({prefix + "Id", m_Id});
kwl.insert({prefix + "Info", m_Info});
kwl.insert({prefix + "Row", to_string_with_precision(m_GCPRow)});
kwl.insert({prefix + "Col", to_string_with_precision(m_GCPCol)});
kwl.insert({prefix + "X", to_string_with_precision(m_GCPX)});
kwl.insert({prefix + "Y", to_string_with_precision(m_GCPY)});
kwl.insert({prefix + "Z", to_string_with_precision(m_GCPZ)});
}
GCP GCP::FromKeywordlist(const MetaData::Keywordlist & kwl, const std::string & prefix)
{
//Info is optional in GCPs, the key might not be in the keywordlist
std::string info;
auto infoFound = kwl.find(prefix + "Info");
if (infoFound != kwl.end())
{
info = infoFound->second;
}
return GCP(kwl.at(prefix + "Id"),
info,
std::stod(kwl.at(prefix + "Row")),
std::stod(kwl.at(prefix + "Col")),
std::stod(kwl.at(prefix + "X")),
std::stod(kwl.at(prefix + "Y")),
std::stod(kwl.at(prefix + "Z")));
}
namespace Projection
{
std::string GCPParam::ToJSON(bool multiline) const
......@@ -77,6 +154,18 @@ std::string GCPParam::ToJSON(bool multiline) const
return oss.str();
}
void GCPParam::ToKeywordlist(MetaData::Keywordlist & kwl, const std::string & prefix) const
{
kwl.insert({prefix + "GCPProjection", GCPProjection});
VectorToKeywordList(kwl, GCPs, prefix + "GCP");
}
void GCPParam::FromKeywordlist(const MetaData::Keywordlist & kwl, const std::string & prefix)
{
GCPProjection = kwl.at(prefix + "GCPProjection");
KeywordlistToVector(GCPs, kwl, prefix + "GCP");
}
std::string RPCParam::ToJSON(bool multiline) const
{
std::ostringstream oss;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment