From 47507622ea11bc5fc657bd9c47f2be082481c8c7 Mon Sep 17 00:00:00 2001
From: Julien Osman <julien.osman@c-s.fr>
Date: Tue, 7 Apr 2020 19:13:46 +0200
Subject: [PATCH] ENH: Add possibility to define the output projection with
 EPSG code in otbGDALImageIO

---
 Modules/IO/IOGDAL/include/otbGDALImageIO.h |  7 ++++++-
 Modules/IO/IOGDAL/src/otbGDALImageIO.cxx   | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/Modules/IO/IOGDAL/include/otbGDALImageIO.h b/Modules/IO/IOGDAL/include/otbGDALImageIO.h
index aeb1870fac..42bedbfab3 100644
--- a/Modules/IO/IOGDAL/include/otbGDALImageIO.h
+++ b/Modules/IO/IOGDAL/include/otbGDALImageIO.h
@@ -30,6 +30,7 @@
 #include "otbImageIOBase.h"
 
 #include "OTBIOGDALExport.h"
+#include "otbSpatialReference.h"
 
 namespace otb
 {
@@ -197,6 +198,9 @@ public:
 
   itkGetMacro(NbBands, int);
 
+  /** Set the projection system from EPSG code */
+  void SetEpsgCode(const unsigned int wellKnownCRS);
+
 protected:
   /**
    * Constructor.
@@ -246,9 +250,10 @@ private:
   /** GDAL parameters. */
   typedef itk::SmartPointer<GDALDatasetWrapper> GDALDatasetWrapperPointer;
   GDALDatasetWrapperPointer                     m_Dataset;
+  unsigned int                                  m_epsgCode;
 
   GDALDataTypeWrapper* m_PxType;
-  /** Nombre d'octets par pixel */
+  /** Number of bytes per pixel */
   int m_BytePerPixel;
 
   bool GDALInfoReportCorner(const char* corner_name, double x, double y, double& dfGeoX, double& dfGeoY) const;
diff --git a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
index 165b4549ec..86e0d2ded1 100644
--- a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
+++ b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
@@ -116,6 +116,8 @@ GDALImageIO::GDALImageIO()
   m_ResolutionFactor  = 0;
   m_BytePerPixel      = 0;
   m_WriteRPCTags      = false;
+
+  m_epsgCode          = 0;
 }
 
 GDALImageIO::~GDALImageIO()
@@ -405,6 +407,11 @@ std::vector<std::string> GDALImageIO::GetOverviewsInfo()
   return desc;
 }
 
+void GDALImageIO::SetEpsgCode(const unsigned int epsgCode)
+{
+  m_epsgCode = epsgCode;
+}
+
 void GDALImageIO::InternalReadImageInformation()
 {
   itk::ExposeMetaData<unsigned int>(this->GetMetaDataDictionary(), MetaDataKey::ResolutionFactor, m_ResolutionFactor);
@@ -1165,6 +1172,13 @@ void GDALImageIO::Write(const void* buffer)
     lFirstColumn = 0;
   }
 
+  // If needed, set the coordinate reference
+  if (m_epsgCode != 0)
+  {
+    auto spatialReference = SpatialReference::FromEPSG(m_epsgCode);
+    m_Dataset->GetDataSet()->SetSpatialRef(spatialReference.getOGRSpatialReference());
+  }
+
   // Convert buffer from void * to unsigned char *
   // unsigned char *p = static_cast<unsigned char*>( const_cast<void *>(buffer));
   // printDataBuffer(p,  m_PxType->pixType, m_NbBands, 10*2); // Buffer incorrect
-- 
GitLab