diff --git a/Modules/IO/IOGDAL/include/otbGDALImageIO.h b/Modules/IO/IOGDAL/include/otbGDALImageIO.h index aeb1870facc07e170aa31d6985ea3b498969f5fe..42bedbfab31b33cb1aef486cc525cdabe0a42d39 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 165b4549ecf9aabd60e1e7484d7a43414123a6f8..86e0d2ded19bdb803db52831303df85d4fefa144 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