From ffe63041afbb127d2ef20330dbae3ee2c0cf9dc1 Mon Sep 17 00:00:00 2001
From: Otmane Lahlou <otmane.lahlou@c-s.fr>
Date: Tue, 7 Sep 2010 11:53:45 +0200
Subject: [PATCH] ENH : enable writing geom files with streamingImageFileWriter

---
 Code/IO/otbStreamingImageFileWriter.h   | 14 ++++++++++++--
 Code/IO/otbStreamingImageFileWriter.txx | 21 ++++++++++++++++++++-
 2 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/Code/IO/otbStreamingImageFileWriter.h b/Code/IO/otbStreamingImageFileWriter.h
index 72e493ee31..88b9d47322 100644
--- a/Code/IO/otbStreamingImageFileWriter.h
+++ b/Code/IO/otbStreamingImageFileWriter.h
@@ -169,6 +169,13 @@ public:
   /** Type use to define number of divisions */
   typedef StreamingMode CalculationDivisionEnumType;
 
+  /**
+   * Enable/disable writing of a .geom file with the ossim keyword list along with the written image
+   */
+  itkSetMacro(WriteGeomFile, bool);
+  itkGetMacro(WriteGeomFile, bool);
+  itkBooleanMacro(WriteGeomFile);
+
 protected:
   StreamingImageFileWriter();
   virtual ~StreamingImageFileWriter();
@@ -209,8 +216,11 @@ private:
   //track whether the region is user specified
   bool m_FactorySpecifiedImageIO; //track whether the factory mechanism set the ImageIO
   bool m_UseCompression;
-  bool m_UseInputMetaDataDictionary; // whether to use the MetaDataDictionary from the input or not.
-
+  bool m_UseInputMetaDataDictionary; // whether to use the
+                                     // MetaDataDictionary from the
+                                     // input or not.
+  
+  bool m_WriteGeomFile;              // Write a geom file to store the kwl
 };
 
 } // end namespace otb
diff --git a/Code/IO/otbStreamingImageFileWriter.txx b/Code/IO/otbStreamingImageFileWriter.txx
index 8c1c6ae5af..3fa74288fc 100644
--- a/Code/IO/otbStreamingImageFileWriter.txx
+++ b/Code/IO/otbStreamingImageFileWriter.txx
@@ -46,7 +46,7 @@ namespace otb
  */
 template <class TInputImage>
 StreamingImageFileWriter<TInputImage>
-::StreamingImageFileWriter()
+::StreamingImageFileWriter():m_WriteGeomFile(false)
 {
   m_BufferMemorySize = 0;
   m_BufferNumberOfLinesDivisions = 0;
@@ -624,6 +624,25 @@ StreamingImageFileWriter<TInputImage>
   const void* dataPtr = (const void*) input->GetBufferPointer();
   m_ImageIO->Write(dataPtr);
 
+  if (m_WriteGeomFile)
+    {
+    // Write the image keyword list if any
+    ossimKeywordlist geom_kwl;
+    ImageKeywordlist otb_kwl;
+
+    itk::MetaDataDictionary dict = input->GetMetaDataDictionary();
+    itk::ExposeMetaData<ImageKeywordlist>(dict, MetaDataKey::OSSIMKeywordlistKey, otb_kwl);
+    otb_kwl.convertToOSSIMKeywordlist(geom_kwl);
+
+    if (geom_kwl.getSize() > 0)
+      {
+      otbMsgDevMacro(<< "Exporting keywordlist ...");
+      ossimFilename geomFileName(this->GetFileName());
+      geomFileName.setExtension(".geom");
+      geom_kwl.write(geomFileName.chars());
+      }
+    }
+  
 }
 
 } // end namespace otb
-- 
GitLab