From 50bca421f3d749907c5eeacaff24984f335a06fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <cedric.traizet@c-s.fr> Date: Thu, 1 Aug 2019 14:19:18 +0200 Subject: [PATCH] STY: apply clang format --- .../app/otbVectorClassifier.cxx | 102 ++++----- .../app/otbVectorRegression.cxx | 60 ++--- .../include/otbVectorPrediction.h | 43 ++-- .../include/otbVectorPrediction.hxx | 212 +++++++++--------- 4 files changed, 204 insertions(+), 213 deletions(-) diff --git a/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx b/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx index 92f4e90f54..4308f19ac6 100644 --- a/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx +++ b/Modules/Applications/AppClassification/app/otbVectorClassifier.cxx @@ -27,67 +27,73 @@ namespace Wrapper using VectorClassifier = VectorPrediction<false>; -template<> -void -VectorClassifier -::DoInitSpecialization() +template <> +void VectorClassifier::DoInitSpecialization() { SetName("VectorClassifier"); SetDescription("Performs a classification of the input vector data according to a model file."); SetDocAuthors("OTB-Team"); - SetDocLongDescription("This application performs a vector data classification " - "based on a model file produced by the TrainVectorClassifier application." - "Features of the vector data output will contain the class labels decided by the classifier " - "(maximal class label = 65535). \n" - "There are two modes: \n" + SetDocLongDescription( + "This application performs a vector data classification " + "based on a model file produced by the TrainVectorClassifier application." + "Features of the vector data output will contain the class labels decided by the classifier " + "(maximal class label = 65535). \n" + "There are two modes: \n" "1) Update mode: add of the 'cfield' field containing the predicted class in the input file. \n" "2) Write mode: copies the existing fields of the input file to the output file " - " and add the 'cfield' field containing the predicted class. \n" - "If you have declared the output file, the write mode applies. " - "Otherwise, the input file update mode will be applied."); + " and add the 'cfield' field containing the predicted class. \n" + "If you have declared the output file, the write mode applies. " + "Otherwise, the input file update mode will be applied."); SetDocLimitations("Shapefiles are supported, but the SQLite format is only supported in update mode."); SetDocSeeAlso("TrainVectorClassifier"); AddDocTag(Tags::Learning); - + AddParameter(ParameterType_InputFilename, "in", "Name of the input vector data"); - SetParameterDescription("in","The input vector data file to classify."); + SetParameterDescription("in", "The input vector data file to classify."); AddParameter(ParameterType_InputFilename, "instat", "Statistics file"); - SetParameterDescription("instat", "A XML file containing mean and standard deviation to center" - "and reduce samples before classification, produced by ComputeImagesStatistics application."); + SetParameterDescription("instat", + "A XML file containing mean and standard deviation to center" + "and reduce samples before classification, produced by ComputeImagesStatistics application."); MandatoryOff("instat"); AddParameter(ParameterType_InputFilename, "model", "Model file"); SetParameterDescription("model", "Model file produced by TrainVectorClassifier application."); - - AddParameter(ParameterType_String,"cfield","Output field"); - SetParameterDescription("cfield","Field containing the predicted class." - "Only geometries with this field available will be taken into account.\n" - "The field is added either in the input file (if 'out' off) or in the output file.\n" - "Caution, the 'cfield' must not exist in the input file if you are updating the file."); - SetParameterString("cfield","predicted"); - + + AddParameter(ParameterType_String, "cfield", "Output field"); + SetParameterDescription("cfield", + "Field containing the predicted class." + "Only geometries with this field available will be taken into account.\n" + "The field is added either in the input file (if 'out' off) or in the output file.\n" + "Caution, the 'cfield' must not exist in the input file if you are updating the file."); + SetParameterString("cfield", "predicted"); + AddParameter(ParameterType_ListView, "feat", "Field names to be calculated"); - SetParameterDescription("feat","List of field names in the input vector data used as features for training. " - "Put the same field names as the TrainVectorClassifier application."); - - AddParameter(ParameterType_Bool, "confmap", "Confidence map"); - SetParameterDescription( "confmap", "Confidence map of the produced classification. The confidence index depends on the model: \n\n" - "* LibSVM: difference between the two highest probabilities (needs a model with probability estimates, so that classes probabilities can be computed for each sample)\n" - "* Boost: sum of votes\n" - "* DecisionTree: (not supported)\n" - "* KNearestNeighbors: number of neighbors with the same label\n" - "* NeuralNetwork: difference between the two highest responses\n" - "* NormalBayes: (not supported)\n" - "* RandomForest: Confidence (proportion of votes for the majority class). Margin (normalized difference of the votes of the 2 majority classes) is not available for now.\n" - "* SVM: distance to margin (only works for 2-class models)\n"); + SetParameterDescription("feat", + "List of field names in the input vector data used as features for training. " + "Put the same field names as the TrainVectorClassifier application."); + + AddParameter(ParameterType_Bool, "confmap", "Confidence map"); + SetParameterDescription("confmap", + "Confidence map of the produced classification. The confidence index depends on the model: \n\n" + "* LibSVM: difference between the two highest probabilities (needs a model with probability estimates, so that classes probabilities " + "can be computed for each sample)\n" + "* Boost: sum of votes\n" + "* DecisionTree: (not supported)\n" + "* KNearestNeighbors: number of neighbors with the same label\n" + "* NeuralNetwork: difference between the two highest responses\n" + "* NormalBayes: (not supported)\n" + "* RandomForest: Confidence (proportion of votes for the majority class). Margin (normalized difference of the votes of the 2 " + "majority classes) is not available for now.\n" + "* SVM: distance to margin (only works for 2-class models)\n"); AddParameter(ParameterType_OutputFilename, "out", "Output vector data file"); MandatoryOff("out"); - SetParameterDescription("out","Output vector data file storing sample values (OGR format)." - "If not given, the input vector data file is updated."); + SetParameterDescription("out", + "Output vector data file storing sample values (OGR format)." + "If not given, the input vector data file is updated."); // Doc example parameter settings SetDocExampleParameterValue("in", "vectorData.shp"); @@ -96,26 +102,22 @@ VectorClassifier SetDocExampleParameterValue("out", "vectorDataLabeledVector.shp"); SetDocExampleParameterValue("feat", "perimeter area width"); SetDocExampleParameterValue("cfield", "predicted"); - + SetOfficialDocLink(); } -template<> -bool -VectorClassifier -::shouldComputeConfidenceMap() const +template <> +bool VectorClassifier::shouldComputeConfidenceMap() const { - bool computeConfidenceMap(GetParameterInt("confmap") && m_Model->HasConfidenceIndex() ); + bool computeConfidenceMap(GetParameterInt("confmap") && m_Model->HasConfidenceIndex()); if (!m_Model->HasConfidenceIndex() && GetParameterInt("confmap")) - { + { otbAppLogWARNING("Confidence map requested but the classifier doesn't support it!"); - } - + } + return computeConfidenceMap; } - - } } diff --git a/Modules/Applications/AppClassification/app/otbVectorRegression.cxx b/Modules/Applications/AppClassification/app/otbVectorRegression.cxx index 9b655fe01e..d1b8a02f66 100644 --- a/Modules/Applications/AppClassification/app/otbVectorRegression.cxx +++ b/Modules/Applications/AppClassification/app/otbVectorRegression.cxx @@ -27,56 +27,59 @@ namespace Wrapper using VectorRegression = VectorPrediction<true>; -template<> -void -VectorRegression -::DoInitSpecialization() +template <> +void VectorRegression::DoInitSpecialization() { SetName("VectorRegression"); SetDescription("Performs regression on the input vector data according to a model file."); SetDocAuthors("OTB-Team"); - SetDocLongDescription("This application performs a vector data regression " - "based on a model file produced by the TrainVectorRegression application." - "Features of the vector data output will contain the values predicted by the classifier. \n" - "There are two modes: \n" + SetDocLongDescription( + "This application performs a vector data regression " + "based on a model file produced by the TrainVectorRegression application." + "Features of the vector data output will contain the values predicted by the classifier. \n" + "There are two modes: \n" "1) Update mode: add of the 'cfield' field containing the predicted value in the input file. \n" "2) Write mode: copies the existing fields of the input file to the output file " - " and add the 'cfield' field containing the predicted value. \n" - "If you have declared the output file, the write mode applies. " - "Otherwise, the input file update mode will be applied."); + " and add the 'cfield' field containing the predicted value. \n" + "If you have declared the output file, the write mode applies. " + "Otherwise, the input file update mode will be applied."); SetDocLimitations("Shapefiles are supported, but the SQLite format is only supported in update mode."); SetDocSeeAlso("TrainVectorRegression"); AddDocTag(Tags::Learning); AddParameter(ParameterType_InputFilename, "in", "Name of the input vector data"); - SetParameterDescription("in","The input vector data file to classify."); + SetParameterDescription("in", "The input vector data file to classify."); AddParameter(ParameterType_InputFilename, "instat", "Statistics file"); - SetParameterDescription("instat", "A XML file containing mean and standard deviation to center" - "and reduce samples before classification, produced by ComputeImagesStatistics application."); + SetParameterDescription("instat", + "A XML file containing mean and standard deviation to center" + "and reduce samples before classification, produced by ComputeImagesStatistics application."); MandatoryOff("instat"); AddParameter(ParameterType_InputFilename, "model", "Model file"); SetParameterDescription("model", "Model file produced by TrainVectorRegression application."); - AddParameter(ParameterType_String,"cfield","Output field"); - SetParameterDescription("cfield","Field containing the predicted value." - "Only geometries with this field available will be taken into account.\n" - "The field is added either in the input file (if 'out' off) or in the output file.\n" - "Caution, the 'cfield' must not exist in the input file if you are updating the file."); - SetParameterString("cfield","predicted"); - + AddParameter(ParameterType_String, "cfield", "Output field"); + SetParameterDescription("cfield", + "Field containing the predicted value." + "Only geometries with this field available will be taken into account.\n" + "The field is added either in the input file (if 'out' off) or in the output file.\n" + "Caution, the 'cfield' must not exist in the input file if you are updating the file."); + SetParameterString("cfield", "predicted"); + AddParameter(ParameterType_ListView, "feat", "Field names to be calculated"); - SetParameterDescription("feat","List of field names in the input vector data used as features for training. " - "Put the same field names as the TrainVectorRegression application."); + SetParameterDescription("feat", + "List of field names in the input vector data used as features for training. " + "Put the same field names as the TrainVectorRegression application."); AddParameter(ParameterType_OutputFilename, "out", "Output vector data file"); MandatoryOff("out"); - SetParameterDescription("out","Output vector data file storing sample values (OGR format)." - "If not given, the input vector data file is updated."); + SetParameterDescription("out", + "Output vector data file storing sample values (OGR format)." + "If not given, the input vector data file is updated."); MandatoryOff("out"); // Doc example parameter settings @@ -91,14 +94,11 @@ VectorRegression } // Confidence map computation is not support for regression. -template<> -bool -VectorRegression -::shouldComputeConfidenceMap() const +template <> +bool VectorRegression::shouldComputeConfidenceMap() const { return false; } - } } diff --git a/Modules/Applications/AppClassification/include/otbVectorPrediction.h b/Modules/Applications/AppClassification/include/otbVectorPrediction.h index 637ee4ba91..12cea01487 100644 --- a/Modules/Applications/AppClassification/include/otbVectorPrediction.h +++ b/Modules/Applications/AppClassification/include/otbVectorPrediction.h @@ -59,52 +59,53 @@ public: itkTypeMacro(Self, Application) - /** Filters typedef */ - typedef float ValueType; - // Label type is float for regression and unsigned int for classification - typedef typename std::conditional<RegressionMode, float, unsigned int>::type LabelType; - - typedef itk::FixedArray<LabelType,1> LabelSampleType; - typedef itk::Statistics::ListSample<LabelSampleType> LabelListSampleType; - - typedef otb::MachineLearningModel<ValueType,LabelType> MachineLearningModelType; - typedef otb::MachineLearningModelFactory<ValueType, LabelType> MachineLearningModelFactoryType; - typedef typename MachineLearningModelType::Pointer ModelPointerType; - typedef typename MachineLearningModelType::ConfidenceListSampleType ConfidenceListSampleType; + /** Filters typedef */ + typedef float ValueType; + // Label type is float for regression and unsigned int for classification + typedef typename std::conditional<RegressionMode, float, unsigned int>::type LabelType; + + typedef itk::FixedArray<LabelType, 1> LabelSampleType; + typedef itk::Statistics::ListSample<LabelSampleType> LabelListSampleType; + + typedef otb::MachineLearningModel<ValueType, LabelType> MachineLearningModelType; + typedef otb::MachineLearningModelFactory<ValueType, LabelType> MachineLearningModelFactoryType; + typedef typename MachineLearningModelType::Pointer ModelPointerType; + typedef typename MachineLearningModelType::ConfidenceListSampleType ConfidenceListSampleType; /** Statistics Filters typedef */ - typedef itk::VariableLengthVector<ValueType> MeasurementType; - typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; + typedef itk::VariableLengthVector<ValueType> MeasurementType; + typedef otb::StatisticsXMLFileReader<MeasurementType> StatisticsReader; - typedef itk::VariableLengthVector<ValueType> InputSampleType; - typedef itk::Statistics::ListSample<InputSampleType> ListSampleType; + typedef itk::VariableLengthVector<ValueType> InputSampleType; + typedef itk::Statistics::ListSample<InputSampleType> ListSampleType; typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType; ~VectorPrediction() override - { + { MachineLearningModelFactoryType::CleanFactories(); - } + } private: /** Utility function to negate std::isalnum */ static bool IsNotAlphaNum(char c) { - return !std::isalnum(c); + return !std::isalnum(c); } - + void DoInit() override; + /** Method defining the parameters used in the application and their documentation, specialized for RegressionMode=1 and RegrssionMode=0 */ void DoInitSpecialization(); void DoUpdateParameters() override; void DoExecute() override; + /** Method returning whether the confidence map should be computed, depending on the regression mode and input parameters */ bool shouldComputeConfidenceMap() const; ModelPointerType m_Model; }; - } } diff --git a/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx b/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx index 73435e8fda..1310fdfb29 100644 --- a/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx +++ b/Modules/Applications/AppClassification/include/otbVectorPrediction.hxx @@ -29,12 +29,10 @@ namespace Wrapper { template <bool RegressionMode> -void -VectorPrediction <RegressionMode> -::DoInit() +void VectorPrediction<RegressionMode>::DoInit() { DoInitSpecialization(); - + // Assert that the all needed parameters have ben definied in DoInitSpecialization assert(GetParameterByKey("in") != nullptr); assert(GetParameterByKey("instat") != nullptr); @@ -45,67 +43,63 @@ VectorPrediction <RegressionMode> } template <bool RegressionMode> -void -VectorPrediction <RegressionMode> -::DoUpdateParameters() +void VectorPrediction<RegressionMode>::DoUpdateParameters() { - if ( HasValue("in") ) + if (HasValue("in")) { std::string shapefile = GetParameterString("in"); otb::ogr::DataSource::Pointer ogrDS; - ogrDS = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); - otb::ogr::Layer layer = ogrDS->GetLayer(0); - OGRFeatureDefn &layerDefn = layer.GetLayerDefn(); + ogrDS = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); + otb::ogr::Layer layer = ogrDS->GetLayer(0); + OGRFeatureDefn& layerDefn = layer.GetLayerDefn(); ClearChoices("feat"); - for(int iField=0; iField< layerDefn.GetFieldCount(); iField++) + for (int iField = 0; iField < layerDefn.GetFieldCount(); iField++) { std::string item = layerDefn.GetFieldDefn(iField)->GetNameRef(); std::string key(item); - key.erase( std::remove_if(key.begin(),key.end(),IsNotAlphaNum), key.end()); + key.erase(std::remove_if(key.begin(), key.end(), IsNotAlphaNum), key.end()); std::transform(key.begin(), key.end(), key.begin(), tolower); OGRFieldType fieldType = layerDefn.GetFieldDefn(iField)->GetType(); - if(fieldType == OFTInteger || fieldType == OFTInteger64 || fieldType == OFTReal) - { - std::string tmpKey="feat."+key; - AddChoice(tmpKey,item); - } + if (fieldType == OFTInteger || fieldType == OFTInteger64 || fieldType == OFTReal) + { + std::string tmpKey = "feat." + key; + AddChoice(tmpKey, item); + } } } } template <bool RegressionMode> -void -VectorPrediction <RegressionMode> -::DoExecute() +void VectorPrediction<RegressionMode>::DoExecute() { clock_t tic = clock(); std::string shapefile = GetParameterString("in"); otb::ogr::DataSource::Pointer source = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); - otb::ogr::Layer layer = source->GetLayer(0); + otb::ogr::Layer layer = source->GetLayer(0); typename ListSampleType::Pointer input = ListSampleType::New(); const int nbFeatures = GetSelectedItems("feat").size(); input->SetMeasurementVectorSize(nbFeatures); - otb::ogr::Layer::const_iterator it = layer.cbegin(); + otb::ogr::Layer::const_iterator it = layer.cbegin(); otb::ogr::Layer::const_iterator itEnd = layer.cend(); - for( ; it!=itEnd ; ++it) - { + for (; it != itEnd; ++it) + { MeasurementType mv; mv.SetSize(nbFeatures); - for(int idx=0; idx < nbFeatures; ++idx) - { + for (int idx = 0; idx < nbFeatures; ++idx) + { // Beware that itemIndex differs from ogr layer field index unsigned int itemIndex = GetSelectedItems("feat")[idx]; - std::string fieldName = GetChoiceNames( "feat" )[itemIndex]; + std::string fieldName = GetChoiceNames("feat")[itemIndex]; switch ((*it)[fieldName].GetType()) { case OFTInteger: @@ -120,30 +114,28 @@ VectorPrediction <RegressionMode> default: itkExceptionMacro(<< "incorrect field type: " << (*it)[fieldName].GetType() << "."); } - - - } - input->PushBack(mv); } + input->PushBack(mv); + } // Statistics for shift/scale MeasurementType meanMeasurementVector; MeasurementType stddevMeasurementVector; if (HasValue("instat") && IsParameterEnabled("instat")) - { + { typename StatisticsReader::Pointer statisticsReader = StatisticsReader::New(); - std::string XMLfile = GetParameterString("instat"); + std::string XMLfile = GetParameterString("instat"); statisticsReader->SetFileName(XMLfile); - meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean"); + meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean"); stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev"); - } + } else - { + { meanMeasurementVector.SetSize(nbFeatures); meanMeasurementVector.Fill(0.); stddevMeasurementVector.SetSize(nbFeatures); stddevMeasurementVector.Fill(1.); - } + } typename ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New(); trainingShiftScaleFilter->SetInput(input); @@ -154,13 +146,12 @@ VectorPrediction <RegressionMode> otbAppLogINFO("standard deviation used: " << stddevMeasurementVector); otbAppLogINFO("Loading model"); - m_Model = MachineLearningModelFactoryType::CreateMachineLearningModel(GetParameterString("model"), - MachineLearningModelFactoryType::ReadMode); + m_Model = MachineLearningModelFactoryType::CreateMachineLearningModel(GetParameterString("model"), MachineLearningModelFactoryType::ReadMode); if (m_Model.IsNull()) - { + { otbAppLogFATAL(<< "Error when loading model " << GetParameterString("model") << " : unsupported model type"); - } + } m_Model->SetRegressionMode(RegressionMode); @@ -175,154 +166,151 @@ VectorPrediction <RegressionMode> typename LabelListSampleType::Pointer target; if (computeConfidenceMap) - { + { quality = ConfidenceListSampleType::New(); - target = m_Model->PredictBatch(listSample, quality); - } - else - { + target = m_Model->PredictBatch(listSample, quality); + } + else + { target = m_Model->PredictBatch(listSample); - } + } ogr::DataSource::Pointer output; - ogr::DataSource::Pointer buffer = ogr::DataSource::New(); - bool updateMode = false; + ogr::DataSource::Pointer buffer = ogr::DataSource::New(); + bool updateMode = false; if (IsParameterEnabled("out") && HasValue("out")) - { + { // Create new OGRDataSource - output = ogr::DataSource::New(GetParameterString("out"), ogr::DataSource::Modes::Overwrite); - otb::ogr::Layer newLayer = output->CreateLayer( - GetParameterString("out"), - const_cast<OGRSpatialReference*>(layer.GetSpatialRef()), - layer.GetGeomType()); + output = ogr::DataSource::New(GetParameterString("out"), ogr::DataSource::Modes::Overwrite); + otb::ogr::Layer newLayer = output->CreateLayer(GetParameterString("out"), const_cast<OGRSpatialReference*>(layer.GetSpatialRef()), layer.GetGeomType()); // Copy existing fields - OGRFeatureDefn &inLayerDefn = layer.GetLayerDefn(); - for (int k=0 ; k<inLayerDefn.GetFieldCount() ; k++) - { + OGRFeatureDefn& inLayerDefn = layer.GetLayerDefn(); + for (int k = 0; k < inLayerDefn.GetFieldCount(); k++) + { OGRFieldDefn fieldDefn(inLayerDefn.GetFieldDefn(k)); newLayer.CreateField(fieldDefn); - } } + } else - { + { // Update mode updateMode = true; otbAppLogINFO("Update input vector data."); // fill temporary buffer for the transfer otb::ogr::Layer inputLayer = layer; - layer = buffer->CopyLayer(inputLayer, std::string("Buffer")); + layer = buffer->CopyLayer(inputLayer, std::string("Buffer")); // close input data source source->Clear(); // Re-open input data source in update mode output = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Update_LayerUpdate); - } + } otb::ogr::Layer outLayer = output->GetLayer(0); OGRErr errStart = outLayer.ogr().StartTransaction(); if (errStart != OGRERR_NONE) - { + { itkExceptionMacro(<< "Unable to start transaction for OGR layer " << outLayer.ogr().GetName() << "."); - } + } - OGRFeatureDefn &layerDefn = layer.GetLayerDefn(); + OGRFeatureDefn& layerDefn = layer.GetLayerDefn(); // Add the field of prediction in the output layer if field not exist - + OGRFieldType labelType; - if (RegressionMode==true) + if (RegressionMode == true) labelType = OFTReal; else labelType = OFTInteger; - + int idx = layerDefn.GetFieldIndex(GetParameterString("cfield").c_str()); if (idx >= 0) { if (layerDefn.GetFieldDefn(idx)->GetType() != labelType) - itkExceptionMacro("Field name "<< GetParameterString("cfield") << " already exists with a different type!"); + itkExceptionMacro("Field name " << GetParameterString("cfield") << " already exists with a different type!"); } else { - OGRFieldDefn predictedField(GetParameterString("cfield").c_str(), labelType); + OGRFieldDefn predictedField(GetParameterString("cfield").c_str(), labelType); ogr::FieldDefn predictedFieldDef(predictedField); outLayer.CreateField(predictedFieldDef); } - + // Add confidence field in the output layer std::string confFieldName("confidence"); if (computeConfidenceMap) - { + { idx = layerDefn.GetFieldIndex(confFieldName.c_str()); if (idx >= 0) - { + { if (layerDefn.GetFieldDefn(idx)->GetType() != OFTReal) - itkExceptionMacro("Field name "<< confFieldName << " already exists with a different type!"); - } + itkExceptionMacro("Field name " << confFieldName << " already exists with a different type!"); + } else - { + { OGRFieldDefn confidenceField(confFieldName.c_str(), OFTReal); confidenceField.SetWidth(confidenceField.GetWidth()); confidenceField.SetPrecision(confidenceField.GetPrecision()); ogr::FieldDefn confFieldDefn(confidenceField); outLayer.CreateField(confFieldDefn); - } } + } // Fill output layer - unsigned int count=0; - std::string classfieldname = GetParameterString("cfield"); - it = layer.cbegin(); - itEnd = layer.cend(); - for( ; it!=itEnd ; ++it, ++count) - { + unsigned int count = 0; + std::string classfieldname = GetParameterString("cfield"); + it = layer.cbegin(); + itEnd = layer.cend(); + for (; it != itEnd; ++it, ++count) + { ogr::Feature dstFeature(outLayer.GetLayerDefn()); - dstFeature.SetFrom( *it , TRUE); + dstFeature.SetFrom(*it, TRUE); dstFeature.SetFID(it->GetFID()); switch (dstFeature[classfieldname].GetType()) - { - case OFTInteger: - dstFeature[classfieldname].SetValue<int>(target->GetMeasurementVector(count)[0]); - break; - case OFTInteger64: - dstFeature[classfieldname].SetValue<int>(target->GetMeasurementVector(count)[0]); - break; - case OFTReal: - dstFeature[classfieldname].SetValue<double>(target->GetMeasurementVector(count)[0]); - break; - case OFTString: - dstFeature[classfieldname].SetValue<std::string>(std::to_string(target->GetMeasurementVector(count)[0])); - break; - default: - itkExceptionMacro(<< "incorrect field type: " << dstFeature[classfieldname].GetType() << "."); - } + { + case OFTInteger: + dstFeature[classfieldname].SetValue<int>(target->GetMeasurementVector(count)[0]); + break; + case OFTInteger64: + dstFeature[classfieldname].SetValue<int>(target->GetMeasurementVector(count)[0]); + break; + case OFTReal: + dstFeature[classfieldname].SetValue<double>(target->GetMeasurementVector(count)[0]); + break; + case OFTString: + dstFeature[classfieldname].SetValue<std::string>(std::to_string(target->GetMeasurementVector(count)[0])); + break; + default: + itkExceptionMacro(<< "incorrect field type: " << dstFeature[classfieldname].GetType() << "."); + } if (computeConfidenceMap) dstFeature[confFieldName].SetValue<double>(quality->GetMeasurementVector(count)[0]); if (updateMode) - { + { outLayer.SetFeature(dstFeature); - } + } else - { + { outLayer.CreateFeature(dstFeature); - } } + } - if(outLayer.ogr().TestCapability("Transactions")) - { + if (outLayer.ogr().TestCapability("Transactions")) + { const OGRErr errCommitX = outLayer.ogr().CommitTransaction(); if (errCommitX != OGRERR_NONE) - { + { itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << outLayer.ogr().GetName() << "."); - } } + } output->SyncToDisk(); clock_t toc = clock(); - otbAppLogINFO( "Elapsed: "<< ((double)(toc - tic) / CLOCKS_PER_SEC)<<" seconds."); + otbAppLogINFO("Elapsed: " << ((double)(toc - tic) / CLOCKS_PER_SEC) << " seconds."); } -} //end namespace wrapper -} //end namespace otb +} // end namespace wrapper +} // end namespace otb #endif -- GitLab