Skip to content
Snippets Groups Projects
Commit c1205e1d authored by Luc Hermitte's avatar Luc Hermitte
Browse files

ENH: OTB-134/OGR -> Field::SetValue(list of strings)

parent f100346b
No related branches found
No related tags found
No related merge requests found
......@@ -25,7 +25,6 @@
#include "otbOGRFieldWrapper.h"
#include <cassert>
#include <vector>
#include <algorithm>
#include <boost/mpl/map.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/pair.hpp>
......@@ -125,8 +124,7 @@ template
static FinalReturnType call(OGRFeature &f, int index)
{
char ** sl = f.GetFieldAsStringList(index);
char ** last = std::find(sl, (char**)0, (char*)0);
FinalReturnType res(sl, last);
FinalReturnType res(sl, sl+CSLCount(sl));
return res;
}
};
......@@ -236,6 +234,38 @@ template
}
};
/**\ingroup GeometryInternals
* \class StringListMemberSetterPtr
* Type for hosting member-function pointers to string-list field setters.
* \tparam ActualParamType type of the field according to OTB wrappers (default
* <tt> = T</tt>)
*
* \internal
* This override is required because of the particular nature of the
* <tt>char**</tt> type chosen by OGR API.
* \since OTB v 3.14.0
*/
template
< typename ActualParamType
> class StringListMemberSetterPtr
{
public:
static void call(OGRFeature &f, int index, ActualParamType const& container)
{
const int nb = boost::size(container) + 1;
std::vector<char const*> v; v.reserve(nb);
for (typename ActualParamType::const_iterator b = container.begin(), e = container.end()
; b != e
; ++b)
{
v.push_back(b->c_str());
}
v.push_back(0);
assert(CSLCount(const_cast <char**>(&v[0])) == boost::size(container));
f.SetField(index, const_cast <char**>(&v[0]));
}
};
/**\ingroup GeometryInternals
* \class MemberContainerSetterPtr
* Type for hosting simple member-function pointers to list-field setters.
......@@ -294,7 +324,7 @@ typedef map
, pair<int_<OFTReal>, MemberSetterPtr<double, &OGRFeature::SetField> >
, pair<int_<OFTRealList>, MemberContainerSetterPtr<double, &OGRFeature::SetField> >
, pair<int_<OFTString>, MemberSetterPtr<char const*, &OGRFeature::SetField/*, std::string*/> >
// , pair<int_<OFTStringList>, MemberSetterPtr<char const*, &OGRFeature::SetField, std::string> >
, pair<int_<OFTStringList>, StringListMemberSetterPtr<std::vector<std::string> > >
> FieldSetters_Map;
/**\ingroup GeometryInternals
......
......@@ -261,35 +261,33 @@ BOOST_AUTO_TEST_CASE(Add_n_Read_Fields)
v0.push_back(12);
f3.SetValue(v0);
std::vector<int> v2 = f3.GetValue<std::vector<int> >();
BOOST_CHECK_EQUAL_COLLECTIONS(v0.begin(),v0.end(),v2.begin(),v2.end());
std::vector<int> w0 = f3.GetValue<std::vector<int> >();
BOOST_CHECK_EQUAL_COLLECTIONS(v0.begin(),v0.end(),w0.begin(),w0.end());
}
// ----[ list of doubles
{
ogr::Field f4 = g0["OFTRealList"];
std::vector<double> v0;
v0.push_back(42);
v0.push_back(12);
f4.SetValue(v0);
std::vector<double> v1;
v1.push_back(42);
v1.push_back(12);
f4.SetValue(v1);
std::vector<double> v2 = f4.GetValue<std::vector<double> >();
BOOST_CHECK_EQUAL_COLLECTIONS(v0.begin(),v0.end(),v2.begin(),v2.end());
std::vector<double> w1 = f4.GetValue<std::vector<double> >();
BOOST_CHECK_EQUAL_COLLECTIONS(v1.begin(),v1.end(),w1.begin(),w1.end());
}
// ----[ list of string
#if 0 // not ready yet
{
ogr::Field f5 = g0["OFTStringList"];
std::vector<std::string> v0;
v0.push_back("42");
v0.push_back("12");
f5.SetValue(v0);
std::vector<std::string> v2;
v2.push_back("42");
v2.push_back("12");
f5.SetValue(v2);
std::vector<std::string> v2 = f5.GetValue<std::vector<std::string> >();
BOOST_CHECK_EQUAL_COLLECTIONS(v0.begin(),v0.end(),v2.begin(),v2.end());
std::vector<std::string> w2 = f5.GetValue<std::vector<std::string> >();
BOOST_CHECK_EQUAL_COLLECTIONS(v2.begin(),v2.end(),w2.begin(),w2.end());
}
#endif
}
BOOST_AUTO_TEST_CASE(OGRDataSource_new_shp_with_features)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment