From 4d99cea22a9d8c0e0a543e064169531ba780daa2 Mon Sep 17 00:00:00 2001
From: remi <remi.cresson@irstea.fr>
Date: Tue, 29 Oct 2019 10:42:40 +0100
Subject: [PATCH] ENH: fix bug with nodata

---
 app/otbPatchesExtraction.cxx     | 10 ++++++++--
 include/otbTensorflowSampler.hxx |  6 +++++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/app/otbPatchesExtraction.cxx b/app/otbPatchesExtraction.cxx
index 2f9cee2..64aa24e 100644
--- a/app/otbPatchesExtraction.cxx
+++ b/app/otbPatchesExtraction.cxx
@@ -197,8 +197,14 @@ public:
     SamplerType::Pointer sampler = SamplerType::New();
     sampler->SetInputVectorData(GetParameterVectorData("vec"));
     sampler->SetField(GetParameterAsString("field"));
-    sampler->SetRejectPatchesWithNodata(GetParameterInt("usenodata")==1);
-    sampler->SetNodataValue(GetParameterFloat("nodataval"));
+    if (GetParameterInt("usenodata")==1)
+      {
+      otbAppLogINFO("Rejecting samples that have at least one no-data value");
+      sampler->SetRejectPatchesWithNodata(true);
+      float ndval = GetParameterFloat("nodataval");
+      otbAppLogINFO("No-data value: " << ndval);
+      sampler->SetNodataValue(ndval);
+      }
     for (auto& bundle: m_Bundles)
     {
       sampler->PushBackInputWithPatchSize(bundle.m_ImageSource.Get(), bundle.m_PatchSize);
diff --git a/include/otbTensorflowSampler.hxx b/include/otbTensorflowSampler.hxx
index 8c5d989..4b2d8c7 100644
--- a/include/otbTensorflowSampler.hxx
+++ b/include/otbTensorflowSampler.hxx
@@ -20,6 +20,10 @@ template <class TInputImage, class TVectorData>
 TensorflowSampler<TInputImage, TVectorData>
 ::TensorflowSampler()
  {
+  m_NumberOfAcceptedSamples = 0;
+  m_NumberOfRejectedSamples = 0;
+  m_RejectPatchesWithNodata = false;
+  m_NodataValue = 0;
  }
 
 template <class TInputImage, class TVectorData>
@@ -199,7 +203,7 @@ TensorflowSampler<TInputImage, TVectorData>
                 hasBeenSampled = false;
                 break;
               }
-            if (hasBeenSampled)
+            if (!hasBeenSampled)
               break;
             }
 
-- 
GitLab