From 87c741e1587baca9311422c5a6d269d4f32f88ec Mon Sep 17 00:00:00 2001
From: Rashad Kanavath <rashad.kanavath.email.com>
Date: Fri, 13 Apr 2018 17:00:59 +0200
Subject: [PATCH] WIP: set no data via extended file name

---
 .../otbExtendedFilenameToWriterOptions.h      | 10 +++++
 .../otbExtendedFilenameToWriterOptions.cxx    | 43 +++++++++++++++++++
 Modules/IO/IOGDAL/include/otbGDALImageIO.h    | 11 +++++
 Modules/IO/IOGDAL/src/otbGDALImageIO.cxx      |  5 +++
 .../IO/ImageIO/include/otbImageFileWriter.txx |  4 +-
 5 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/Modules/IO/ExtendedFilename/include/otbExtendedFilenameToWriterOptions.h b/Modules/IO/ExtendedFilename/include/otbExtendedFilenameToWriterOptions.h
index 81909d1528..fd9959d81c 100644
--- a/Modules/IO/ExtendedFilename/include/otbExtendedFilenameToWriterOptions.h
+++ b/Modules/IO/ExtendedFilename/include/otbExtendedFilenameToWriterOptions.h
@@ -76,12 +76,22 @@ public:
     std::vector<std::string>                     optionList;
   };
 
+  typedef std::pair<int, double> NoDataPairType;
+  typedef std::vector<NoDataPairType> NoDataListType;
+  NoDataListType m_NoDataList;
+  bool has_noDataValue;
+
   /* Set Methods */
   void SetExtendedFileName(const char * extFname) ITK_OVERRIDE;
   /* Get Methods */
   bool SimpleFileNameIsSet () const;
+  bool NoDataValueIsSet () const;
   bool WriteGEOMFileIsSet () const;
   bool WriteRPCTagsIsSet() const;
+  NoDataListType GetNoDataList () const {
+    return m_NoDataList;
+  }
+
   bool GetWriteGEOMFile () const;
   bool GetWriteRPCTags() const;
   bool gdalCreationOptionsIsSet () const;
diff --git a/Modules/IO/ExtendedFilename/src/otbExtendedFilenameToWriterOptions.cxx b/Modules/IO/ExtendedFilename/src/otbExtendedFilenameToWriterOptions.cxx
index b3413fdc53..6d2481a7b8 100644
--- a/Modules/IO/ExtendedFilename/src/otbExtendedFilenameToWriterOptions.cxx
+++ b/Modules/IO/ExtendedFilename/src/otbExtendedFilenameToWriterOptions.cxx
@@ -22,6 +22,7 @@
 #include "otb_boost_string_header.h"
 #include <itksys/RegularExpression.hxx>
 #include "otb_boost_tokenizer_header.h"
+#include "otbStringUtils.h"
 
 namespace otb
 {
@@ -37,6 +38,8 @@ ExtendedFilenameToWriterOptions
 
   m_Options.writeRPCTags.first = false;
   m_Options.writeRPCTags.second = false;
+
+  has_noDataValue = false;
   
   m_Options.gdalCreationOptions.first = false;
   m_Options.streamingType.first       = false;
@@ -51,6 +54,7 @@ ExtendedFilenameToWriterOptions
   m_Options.optionList.push_back("streaming:type");
   m_Options.optionList.push_back("streaming:sizemode");
   m_Options.optionList.push_back("streaming:sizevalue");
+  m_Options.optionList.push_back("nodata");
   m_Options.optionList.push_back("box");
   m_Options.optionList.push_back("bands");
 }
@@ -93,6 +97,38 @@ ExtendedFilenameToWriterOptions
        m_Options.writeGEOMFile.second = false;
        }
      }
+  
+  if (!map["nodata"].empty())
+     {
+       std::string nodata_values = map["nodata"];
+       std::vector<std::string> nodata_list;
+       boost::split(nodata_list, nodata_values, boost::is_any_of(","),
+		    boost::token_compress_on);
+       std::vector<std::string>::const_iterator listIt = nodata_list.begin();
+       for(; listIt != nodata_list.end(); ++listIt) {
+       std::string nodata_pair = (*listIt);
+       std::vector<std::string> per_band_no_data;
+       boost::split(per_band_no_data,
+		    nodata_pair,
+		    boost::is_any_of(":"),
+		    boost::token_compress_on);
+       if (per_band_no_data.size() == 1)
+	 {
+	   double val = Utils::LexicalCast<double>(per_band_no_data[0],
+						   "nodata value");
+	   m_NoDataList.push_back(NoDataPairType(1, val));
+	 }
+       else
+	 {
+	   int band = Utils::LexicalCast<int>(per_band_no_data[0],
+						   "nodata value");
+	   double val = Utils::LexicalCast<double>(per_band_no_data[1],
+						   "nodata value");
+	   m_NoDataList.push_back(NoDataPairType(band, val));
+	 }
+       }
+       has_noDataValue = true;
+     }
 
   if (!map["writerpctags"].empty())
      {
@@ -204,6 +240,13 @@ ExtendedFilenameToWriterOptions
     }
 }
 
+bool
+ExtendedFilenameToWriterOptions
+::NoDataValueIsSet () const
+{
+  return has_noDataValue;
+}
+
 bool
 ExtendedFilenameToWriterOptions
 ::SimpleFileNameIsSet () const
diff --git a/Modules/IO/IOGDAL/include/otbGDALImageIO.h b/Modules/IO/IOGDAL/include/otbGDALImageIO.h
index 49b4678ac4..70187dabcb 100644
--- a/Modules/IO/IOGDAL/include/otbGDALImageIO.h
+++ b/Modules/IO/IOGDAL/include/otbGDALImageIO.h
@@ -81,6 +81,10 @@ public:
 
   typedef std::vector<std::string> GDALCreationOptionsType;
 
+  typedef  std::vector<
+    std::pair<int, double>
+    > NoDataListType;
+
   /** Method for creation through the object factory. */
   itkNewMacro(Self);
 
@@ -111,6 +115,8 @@ public:
     m_CreationOptions = opts;
   }
 
+  void SetNoDataList(NoDataListType v) { m_NoDataList = v; }
+
   GDALCreationOptionsType GetOptions(void)
   {
     return m_CreationOptions;
@@ -188,6 +194,8 @@ public:
   /** Returns gdal pixel type as string */
   std::string GetGdalPixelTypeAsString() const;
 
+  int NbBands() { return m_NbBands;}
+
 protected:
   /**
    * Constructor.
@@ -281,6 +289,9 @@ private:
    * True if RPC tags should be exported
    */
   bool m_WriteRPCTags;
+
+
+  NoDataListType m_NoDataList;
   
 };
 
diff --git a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
index 28d6700f52..c026462e57 100644
--- a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
+++ b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
@@ -1847,6 +1847,11 @@ void GDALImageIO::InternalWriteImageInformation(const void* buffer)
         }
       }
     }
+
+  NoDataListType::const_iterator noDataIt = m_NoDataList.begin();
+    for(; noDataIt != m_NoDataList.end(); ++noDataIt)
+    dataset->GetRasterBand(noDataIt->first)->SetNoDataValue(noDataIt->second);
+
 }
 
 std::string GDALImageIO::FilenameToGdalDriverShortName(const std::string& name) const
diff --git a/Modules/IO/ImageIO/include/otbImageFileWriter.txx b/Modules/IO/ImageIO/include/otbImageFileWriter.txx
index b1472291f1..6304fb89e7 100644
--- a/Modules/IO/ImageIO/include/otbImageFileWriter.txx
+++ b/Modules/IO/ImageIO/include/otbImageFileWriter.txx
@@ -456,7 +456,7 @@ ImageFileWriter<TInputImage>
 
   // Manage extended filename
   if ((strcmp(m_ImageIO->GetNameOfClass(), "GDALImageIO") == 0)
-      && (m_FilenameHelper->gdalCreationOptionsIsSet() || m_FilenameHelper->WriteRPCTagsIsSet())  )
+      && (m_FilenameHelper->gdalCreationOptionsIsSet() || m_FilenameHelper->WriteRPCTagsIsSet()  || m_FilenameHelper->NoDataValueIsSet()) )
     {
     typename GDALImageIO::Pointer imageIO = dynamic_cast<GDALImageIO*>(m_ImageIO.GetPointer());
 
@@ -471,6 +471,8 @@ ImageFileWriter<TInputImage>
 
     imageIO->SetOptions(m_FilenameHelper->GetgdalCreationOptions());
     imageIO->SetWriteRPCTags(m_FilenameHelper->GetWriteRPCTags());
+    if (m_FilenameHelper->NoDataValueIsSet() )
+	imageIO->SetNoDataList(m_FilenameHelper->GetNoDataList());
     }
 
 
-- 
GitLab