Commit f7f9c007 authored by Julien Cabieces's avatar Julien Cabieces

ENH: Manage filter types for FieldParameter

parent 095d06a0
......@@ -139,6 +139,7 @@ private:
AddParameter(ParameterType_Field, "ref.vector.field", "Field name");
SetParameterDescription("ref.vector.field", "Field name containing the label values");
SetVectorData("ref.vector.field", "ref.vector.in");
SetTypeFilter("ref.vector.field", { OFTString, OFTInteger, OFTInteger64 });
SetListViewSingleSelectionMode("ref.vector.field", true);
AddParameter(ParameterType_Int, "ref.raster.nodata", "Value for nodata pixels in the reference raster");
......@@ -187,6 +188,7 @@ private:
ClearChoices("ref.vector.field");
FieldParameter::TypeFilterType typeFilter = GetTypeFilter("ref.vector.field");
for (int iField = 0; iField < feature.ogr().GetFieldCount(); iField++)
{
std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef();
......@@ -196,11 +198,13 @@ private:
OGRFieldType fieldType = feature.ogr().GetFieldDefnRef(iField)->GetType();
if (fieldType == OFTString || fieldType == OFTInteger || fieldType == OFTInteger64)
{
std::string tmpKey = "ref.vector.field." + key.substr(0, end - key.begin());
AddChoice(tmpKey, item);
}
for (auto type : typeFilter)
if (fieldType == type)
{
std::string tmpKey = "ref.vector.field." + key.substr(0, end - key.begin());
AddChoice(tmpKey, item);
break;
}
}
}
}
......
......@@ -75,6 +75,7 @@ void VectorClassifier::DoInitSpecialization()
"List of field names in the input vector data used as features for training. "
"Put the same field names as the TrainVectorClassifier application.");
SetVectorData("feat", "in");
SetTypeFilter("feat", {OFTInteger, OFTInteger64, OFTReal});
AddParameter(ParameterType_Bool, "confmap", "Confidence map");
SetParameterDescription("confmap",
......
......@@ -74,6 +74,7 @@ void VectorRegression::DoInitSpecialization()
"List of field names in the input vector data used as features for training. "
"Put the same field names as the TrainVectorRegression application.");
SetVectorData("feat", "in");
SetTypeFilter("feat", {OFTInteger, OFTInteger64, OFTReal});
AddParameter(ParameterType_OutputFilename, "out", "Output vector data file");
MandatoryOff("out");
......
......@@ -55,6 +55,7 @@ void VectorPrediction<RegressionMode>::DoUpdateParameters()
ClearChoices("feat");
FieldParameter::TypeFilterType typeFilter = GetTypeFilter("feat");
for (int iField = 0; iField < layerDefn.GetFieldCount(); iField++)
{
auto fieldDefn = layerDefn.GetFieldDefn(iField);
......@@ -63,11 +64,14 @@ void VectorPrediction<RegressionMode>::DoUpdateParameters()
key.erase(std::remove_if(key.begin(), key.end(), [](char c) { return !std::isalnum(c); }), key.end());
std::transform(key.begin(), key.end(), key.begin(), tolower);
auto fieldType = fieldDefn->GetType();
if (fieldType == OFTInteger || fieldType == OFTInteger64 || fieldType == OFTReal)
{
std::string tmpKey = "feat." + key;
AddChoice(tmpKey, item);
}
for (auto type : typeFilter)
if (fieldType == type)
{
std::string tmpKey = "feat." + key;
AddChoice(tmpKey, item);
break;
}
}
}
}
......
......@@ -26,6 +26,7 @@
#include "otbWrapperTypes.h"
#include "otbWrapperTags.h"
#include "otbWrapperParameterGroup.h"
#include "otbWrapperFieldParameter.h"
#include "otbLogger.h"
#include "otbStopwatch.h"
......@@ -569,6 +570,20 @@ public:
*/
void SetVectorData(std::string const& key, std::string const& vectorData );
/**
* Set list of allowed field types for a FieldParameter
* \in key The parameter key (must be a FieldParameter)
* \in typeFilter List of allowed types
*/
void SetTypeFilter(std::string const& key, FieldParameter::TypeFilterType const& typeFilter );
/**
* Get list of allowed field types for a FieldParameter
* \in key The parameter key (must be a FieldParameter)
* \return List of allowed types
*/
const FieldParameter::TypeFilterType& GetTypeFilter(std::string const& key );
/**
* Set raster data name from which we choose the bands for a BandParameter
* \in key The parameter key (must be a BandParameter)
......
......@@ -50,6 +50,8 @@ public:
/** RTTI support */
itkTypeMacro(FieldParameter, ListViewParameter);
typedef std::vector<OGRFieldType> TypeFilterType;
ParameterType GetType() const override
{
return ParameterType_Field;
......@@ -67,6 +69,18 @@ public:
return m_VectorData;
}
/** Set list of allowed field types */
void SetTypeFilter(const TypeFilterType& typeFilter)
{
m_TypeFilter = typeFilter;
}
/** Get list of allowed field types */
const TypeFilterType& GetTypeFilter() const
{
return m_TypeFilter;
}
protected:
/** Constructor */
FieldParameter()
......@@ -79,6 +93,7 @@ protected:
}
std::string m_VectorData;
TypeFilterType m_TypeFilter;
private:
FieldParameter(const FieldParameter&) = delete;
......
......@@ -1367,6 +1367,18 @@ void Application::SetVectorData(std::string const& key, std::string const& vecto
param->SetVectorData(vectorData);
}
void Application::SetTypeFilter(std::string const& key, FieldParameter::TypeFilterType const& typeFilter )
{
auto param = downcast_check<FieldParameter>(GetParameterByKey(key));
param->SetTypeFilter(typeFilter);
}
const FieldParameter::TypeFilterType& Application::GetTypeFilter(std::string const& key )
{
auto param = downcast_check<FieldParameter>(GetParameterByKey(key));
return param->GetTypeFilter();
}
void Application::SetRasterData(std::string const& key, std::string const& rasterData )
{
auto param = downcast_check<BandParameter>(GetParameterByKey(key));
......
......@@ -272,8 +272,26 @@ int main(int argc, char* argv[])
{
FieldParameter *f_param = dynamic_cast<FieldParameter*>(param.GetPointer());
enum {
STRING = 1 << 0,
NUMERIC = 1 << 1
};
int filterType = 0;
for (auto type : f_param->GetTypeFilter())
{
if (type == OFTString)
filterType |= STRING;
else if (type == OFTInteger || type == OFTInteger64 || type == OFTReal)
filterType |= NUMERIC;
}
dFile << "|None|" << f_param->GetVectorData()
<< "|QgsProcessingParameterField.Any|False";
<< "|"
<< (filterType == STRING ? "QgsProcessingParameterField.String" :
filterType == NUMERIC ? "QgsProcessingParameterField.Numeric" :
"QgsProcessingParameterField.Any")
<< "|False";
}
else if (type == ParameterType_Band)
{
......
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