Skip to content
Snippets Groups Projects
Commit 29f7458f authored by Julien Michel's avatar Julien Michel
Browse files

ENH: Update PolygonClassStatistics, SampleSelection and SampleExtraction...

ENH: Update PolygonClassStatistics, SampleSelection and SampleExtraction applications so as to read possible field values and expose them as a ListView parameter
parent 770637f5
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,12 @@ namespace otb
namespace Wrapper
{
/** Utility function to negate std::isalnum */
bool IsNotAlphaNum(char c)
{
return !std::isalnum(c);
}
class PolygonClassStatistics : public Application
{
public:
......@@ -95,10 +101,9 @@ private:
AddParameter(ParameterType_OutputFilename, "out", "Output Statistics");
SetParameterDescription("out","Output file to store statistics (XML format)");
AddParameter(ParameterType_String, "field", "Field Name");
AddParameter(ParameterType_ListView, "field", "Field Name");
SetParameterDescription("field","Name of the field carrying the class name in the input vectors.");
MandatoryOff("field");
SetParameterString("field", "class");
SetListViewSingleSelectionMode("field",true);
AddParameter(ParameterType_Int, "layer", "Layer Index");
SetParameterDescription("layer", "Layer index to read in the input vector file.");
......@@ -116,14 +121,49 @@ private:
void DoUpdateParameters() ITK_OVERRIDE
{
// Nothing to do
if ( HasValue("vec") )
{
std::string vectorFile = GetParameterString("vec");
ogr::DataSource::Pointer ogrDS =
ogr::DataSource::New(vectorFile, ogr::DataSource::Modes::Read);
ogr::Layer layer = ogrDS->GetLayer(this->GetParameterInt("layer"));
ogr::Feature feature = layer.ogr().GetNextFeature();
ClearChoices("field");
for(int iField=0; iField<feature.ogr().GetFieldCount(); iField++)
{
std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef();
key = item;
std::string::iterator end = std::remove_if(key.begin(),key.end(),IsNotAlphaNum);
std::transform(key.begin(), end, key.begin(), tolower);
OGRFieldType fieldType = feature.ogr().GetFieldDefnRef(iField)->GetType();
if(fieldType == OFTInteger || fieldType == OFTInteger64)
{
std::string tmpKey="field."+key.substr(0, end - key.begin());
AddChoice(tmpKey,item);
}
}
}
}
void DoExecute() ITK_OVERRIDE
{
otb::ogr::DataSource::Pointer vectors =
otb::ogr::DataSource::New(this->GetParameterString("vec"));
std::string fieldName = this->GetParameterString("field");
// Retrieve the field name
std::vector<int> selectedCFieldIdx = GetSelectedItems("field");
if(selectedCFieldIdx.empty())
{
otbAppLogFATAL(<<"No field has been selected for data labelling!");
}
std::vector<std::string> cFieldNames = GetChoiceNames("cfield");
std::string fieldName = cFieldNames[selectedCFieldIdx.front()];
// Reproject geometries
FloatVectorImageType::Pointer inputImg = this->GetParameterImage("in");
......
......@@ -24,6 +24,11 @@ namespace otb
{
namespace Wrapper
{
/** Utility function to negate std::isalnum */
bool IsNotAlphaNum(char c)
{
return !std::isalnum(c);
}
class SampleExtraction : public Application
{
......@@ -89,12 +94,10 @@ private:
AddParameter(ParameterType_StringList, "outfield.list.names", "Output field names");
SetParameterDescription("outfield.list.names","Full list of output field names.");
AddParameter(ParameterType_String, "field", "Field Name");
SetParameterDescription("field","Name of the field carrying the class"
"name in the input vectors. This field is copied to output.");
MandatoryOff("field");
SetParameterString("field", "class");
AddParameter(ParameterType_ListView, "field", "Field Name");
SetParameterDescription("field","Name of the field carrying the class name in the input vectors.");
SetListViewSingleSelectionMode("field",true);
AddParameter(ParameterType_Int, "layer", "Layer Index");
SetParameterDescription("layer", "Layer index to read in the input vector file.");
MandatoryOff("layer");
......@@ -113,7 +116,32 @@ private:
void DoUpdateParameters()
{
// Nothing to do
if ( HasValue("vec") )
{
std::string vectorFile = GetParameterString("vec");
ogr::DataSource::Pointer ogrDS =
ogr::DataSource::New(vectorFile, ogr::DataSource::Modes::Read);
ogr::Layer layer = ogrDS->GetLayer(this->GetParameterInt("layer"));
ogr::Feature feature = layer.ogr().GetNextFeature();
ClearChoices("field");
for(int iField=0; iField<feature.ogr().GetFieldCount(); iField++)
{
std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef();
key = item;
std::string::iterator end = std::remove_if(key.begin(),key.end(),IsNotAlphaNum);
std::transform(key.begin(), end, key.begin(), tolower);
OGRFieldType fieldType = feature.ogr().GetFieldDefnRef(iField)->GetType();
if(fieldType == OFTInteger || fieldType == OFTInteger64)
{
std::string tmpKey="field."+key.substr(0, end - key.begin());
AddChoice(tmpKey,item);
}
}
}
}
void DoExecute()
......@@ -134,6 +162,17 @@ private:
output = vectors;
}
// Retrieve the field name
std::vector<int> selectedCFieldIdx = GetSelectedItems("field");
if(selectedCFieldIdx.empty())
{
otbAppLogFATAL(<<"No field has been selected for data labelling!");
}
std::vector<std::string> cFieldNames = GetChoiceNames("cfield");
std::string fieldName = cFieldNames[selectedCFieldIdx.front()];
std::vector<std::string> nameList;
std::string namePrefix("");
if (this->GetParameterString("outfield").compare("prefix") == 0)
......@@ -155,7 +194,7 @@ private:
filter->SetLayerIndex(this->GetParameterInt("layer"));
filter->SetSamplePositions(vectors);
filter->SetOutputSamples(output);
filter->SetClassFieldName(this->GetParameterString("field"));
filter->SetClassFieldName(fieldName);
filter->SetOutputFieldPrefix(namePrefix);
filter->SetOutputFieldNames(nameList);
filter->GetStreamer()->SetAutomaticAdaptativeStreaming(GetParameterInt("ram"));
......
......@@ -29,6 +29,12 @@ namespace otb
namespace Wrapper
{
/** Utility function to negate std::isalnum */
bool IsNotAlphaNum(char c)
{
return !std::isalnum(c);
}
class SampleSelection : public Application
{
public:
......@@ -194,10 +200,9 @@ private:
// Default strategy : smallest
SetParameterString("strategy","smallest");
AddParameter(ParameterType_String, "field", "Field Name");
AddParameter(ParameterType_ListView, "field", "Field Name");
SetParameterDescription("field","Name of the field carrying the class name in the input vectors.");
MandatoryOff("field");
SetParameterString("field", "class");
SetListViewSingleSelectionMode("field",true);
AddParameter(ParameterType_Int, "layer", "Layer Index");
SetParameterDescription("layer", "Layer index to read in the input vector file.");
......@@ -218,6 +223,32 @@ private:
void DoUpdateParameters()
{
if ( HasValue("vec") )
{
std::string vectorFile = GetParameterString("vec");
ogr::DataSource::Pointer ogrDS =
ogr::DataSource::New(vectorFile, ogr::DataSource::Modes::Read);
ogr::Layer layer = ogrDS->GetLayer(this->GetParameterInt("layer"));
ogr::Feature feature = layer.ogr().GetNextFeature();
ClearChoices("field");
for(int iField=0; iField<feature.ogr().GetFieldCount(); iField++)
{
std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef();
key = item;
std::string::iterator end = std::remove_if(key.begin(),key.end(),IsNotAlphaNum);
std::transform(key.begin(), end, key.begin(), tolower);
OGRFieldType fieldType = feature.ogr().GetFieldDefnRef(iField)->GetType();
if(fieldType == OFTInteger || fieldType == OFTInteger64)
{
std::string tmpKey="field."+key.substr(0, end - key.begin());
AddChoice(tmpKey,item);
}
}
}
}
void DoExecute()
......@@ -230,6 +261,17 @@ private:
// Setup ram
m_Periodic->GetStreamer()->SetAutomaticAdaptativeStreaming(GetParameterInt("ram"));
m_Random->GetStreamer()->SetAutomaticAdaptativeStreaming(GetParameterInt("ram"));
// Get field name
std::vector<int> selectedCFieldIdx = GetSelectedItems("field");
if(selectedCFieldIdx.empty())
{
otbAppLogFATAL(<<"No field has been selected for data labelling!");
}
std::vector<std::string> cFieldNames = GetChoiceNames("cfield");
std::string fieldName = cFieldNames[selectedCFieldIdx.front()];
m_ReaderStat->SetFileName(this->GetParameterString("instats"));
ClassCountMapType classCount = m_ReaderStat->GetStatisticMapByName<ClassCountMapType>("samplesPerClass");
......@@ -372,7 +414,7 @@ private:
m_Periodic->SetInput(this->GetParameterImage("in"));
m_Periodic->SetOGRData(reprojVector);
m_Periodic->SetOutputPositionContainerAndRates(outputSamples, rates);
m_Periodic->SetFieldName(this->GetParameterString("field"));
m_Periodic->SetFieldName(fieldName);
m_Periodic->SetLayerIndex(this->GetParameterInt("layer"));
m_Periodic->SetSamplerParameters(param);
if (IsParameterEnabled("mask") && HasValue("mask"))
......@@ -390,7 +432,7 @@ private:
m_Random->SetInput(this->GetParameterImage("in"));
m_Random->SetOGRData(reprojVector);
m_Random->SetOutputPositionContainerAndRates(outputSamples, rates);
m_Random->SetFieldName(this->GetParameterString("field"));
m_Random->SetFieldName(fieldName);
m_Random->SetLayerIndex(this->GetParameterInt("layer"));
if (IsParameterEnabled("mask") && HasValue("mask"))
{
......
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