Commit 8d5b732d authored by Stéphane Albert's avatar Stéphane Albert
Browse files

ENH: Improved otb::GDALOverviewsBuilder interface.

parent 9f56d317
......@@ -21,36 +21,59 @@
// #include "itkLightObject.h"
#include "itkProcessObject.h"
#include "otbConfigure.h"
// class GDALDataset;
// class GDALDriver;
/* GDAL Libraries */
// #include "gdal.h"
// #include "gdaljp2metadata.h"
// #include "gdal_priv.h"
// #include "gdal_alg.h"
#include "otbGDALDatasetWrapper.h"
#include "otbConfigure.h"
namespace otb
{
/**
*/
enum GdalResampling
enum GDALResampling
{
NONE = 0,
NEAREST,
GAUSS,
CUBIC,
AVERAGE,
MODE,
AVERAGE_MAGPHASE,
GDAL_RESAMPLING_NONE = 0,
//
GDAL_RESAMPLING_NEAREST,
GDAL_RESAMPLING_GAUSS,
GDAL_RESAMPLING_CUBIC,
GDAL_RESAMPLING_AVERAGE,
GDAL_RESAMPLING_MODE,
GDAL_RESAMPLING_AVERAGE_MAGPHASE,
//
GDAL_RESAMPLING_COUNT
};
// Compile-time compatibility alias.
typedef GdalResampling GDALResamplingType;
typedef GDALResampling GDALResamplingType;
/**
*/
enum GDALCompression
{
GDAL_COMPRESSION_NONE = 0,
//
GDAL_COMPRESSION_JPEG,
GDAL_COMPRESSION_LZW,
GDAL_COMPRESSION_PACKBITS,
GDAL_COMPRESSION_DEFLATE,
//
GDAL_COMPRESSION_COUNT,
};
/**
*/
enum GDALFormat
{
GDAL_FORMAT_NONE = 0,
//
GDAL_FORMAT_ERDAS,
GDAL_FORMAT_GEOTIFF,
//
GDAL_FORMAT_COUNT,
};
/**
......@@ -66,33 +89,63 @@ public:
/** Method for creation through the object factory. */
itkNewMacro( Self );
void SetResamplingMethod( GDALResamplingType resampMethod );
int GetOverviewsCount() const;
GDALResampling GetResamplingMethod() const;
void SetResamplingMethod( GDALResampling );
GDALCompression GetCompressionMethod() const;
void SetNbOfResolutions( unsigned int nbResol );
void SetCompressionMethod( GDALCompression );
GDALFormat GetFormat() const;
void SetFormat( GDALFormat );
unsigned int GetNbResolutions() const;
void SetNbResolutions( unsigned int nbResol );
unsigned int GetResolutionFactor() const;
void SetResolutionFactor( unsigned int factor );
const std::string & GetInputFileName() const;
void SetInputFileName( const std::string & str );
void Update();
protected:
GDALOverviewsBuilder();
virtual ~GDALOverviewsBuilder() {};
void PrintSelf( std::ostream & os, itk::Indent indent ) const;
private:
private:
GDALOverviewsBuilder( const Self & ); //purposely not implemented
void operator = ( const Self & ); //purposely not implemented
void GetGDALResamplingMethod( std::string & resamplingMethod );
void OpenDataset( const std::string & filename );
GDALDatasetWrapper::Pointer m_GdalDataset;
std::string m_InputFileName;
unsigned int m_NbOfResolutions;
unsigned int m_NbResolutions;
unsigned int m_ResolutionFactor;
GDALResamplingType m_ResamplingMethod;
void GetGDALResamplingMethod( std::string & resamplingMethod );
GDALResampling m_ResamplingMethod;
GDALCompression m_CompressionMethod;
GDALFormat m_Format;
}; // end of GDALOverviewsBuilder
......
......@@ -30,6 +30,19 @@
namespace otb
{
char const * const GDAL_RESAMPLING_NAMES[ GDAL_RESAMPLING_COUNT ] =
{
"NONE",
"NEAREST",
"GAUSS",
"CUBIC",
"AVERAGE",
"MODE",
"AVERAGE_MAGPHASE",
};
// Progress reporting functions compatible with GDAL C API
extern "C"
{
......@@ -44,33 +57,95 @@ extern "C"
}
/***************************************************************************/
GDALOverviewsBuilder
::GDALOverviewsBuilder()
::GDALOverviewsBuilder() :
m_GdalDataset(),
m_InputFileName(),
m_NbResolutions( 1 ),
m_ResolutionFactor( 2 ),
m_ResamplingMethod( GDAL_RESAMPLING_NEAREST ),
m_CompressionMethod( GDAL_COMPRESSION_NONE ),
m_Format( GDAL_FORMAT_GEOTIFF )
{
m_NbOfResolutions = 1;
m_ResolutionFactor = 2;
m_ResamplingMethod = NEAREST;
Superclass::SetNumberOfRequiredInputs(0);
Superclass::SetNumberOfRequiredOutputs(0);
}
/***************************************************************************/
GDALResampling
GDALOverviewsBuilder
::GetResamplingMethod() const
{
return m_ResamplingMethod;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::SetResamplingMethod( GDALResamplingType resampMethod )
::SetResamplingMethod( GDALResamplingType resampling )
{
m_ResamplingMethod = resampMethod;
m_ResamplingMethod = resampling;
};
/***************************************************************************/
GDALCompression
GDALOverviewsBuilder
::GetCompressionMethod() const
{
return m_CompressionMethod;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::SetCompressionMethod( GDALCompression compression )
{
m_CompressionMethod = compression;
}
/***************************************************************************/
GDALFormat
GDALOverviewsBuilder
::GetFormat() const
{
return m_Format;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::SetNbOfResolutions( unsigned int nbResol )
::SetFormat( GDALFormat format )
{
m_NbOfResolutions = nbResol;
m_Format = format;
}
/***************************************************************************/
unsigned int
GDALOverviewsBuilder
::GetNbResolutions() const
{
return m_NbResolutions;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::SetNbResolutions( unsigned int n )
{
m_NbResolutions = n;
};
/***************************************************************************/
unsigned int
GDALOverviewsBuilder
::GetResolutionFactor() const
{
return m_ResolutionFactor;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::SetResolutionFactor( unsigned int factor )
......@@ -78,15 +153,24 @@ GDALOverviewsBuilder
m_ResolutionFactor = factor;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::SetInputFileName( const std::string & str )
::SetInputFileName( const std::string & filename )
{
m_InputFileName = str;
try
{
OpenDataset( filename );
}
catch( ... )
{
throw;
}
m_InputFileName = filename;
};
/***************************************************************************/
void
GDALOverviewsBuilder
::PrintSelf( std::ostream& os, itk::Indent indent ) const
......@@ -94,11 +178,11 @@ GDALOverviewsBuilder
Superclass::PrintSelf(os,indent);
os << indent << "Input Filename: " << m_InputFileName << std::endl;
os << indent << "Number of Resolution requested: " << m_NbOfResolutions << std::endl;
os << indent << "Number of Resolution requested: " << m_NbResolutions << std::endl;
os << indent << "Resampling method: " << m_ResamplingMethod << std::endl;
}
/***************************************************************************/
void
GDALOverviewsBuilder
::GetGDALResamplingMethod( std::string & resamplingMethod )
......@@ -106,25 +190,25 @@ GDALOverviewsBuilder
resamplingMethod.clear();
switch(m_ResamplingMethod)
{
case NONE:
case GDAL_RESAMPLING_NONE:
resamplingMethod = "NONE";
break;
case NEAREST:
case GDAL_RESAMPLING_NEAREST:
resamplingMethod = "NEAREST";
break;
case GAUSS:
case GDAL_RESAMPLING_GAUSS:
resamplingMethod = "GAUSS";
break;
case CUBIC:
case GDAL_RESAMPLING_CUBIC:
resamplingMethod = "CUBIC";
break;
case AVERAGE:
case GDAL_RESAMPLING_AVERAGE:
resamplingMethod = "AVERAGE";
break;
case MODE:
case GDAL_RESAMPLING_MODE:
resamplingMethod = "MODE";
break;
case AVERAGE_MAGPHASE:
case GDAL_RESAMPLING_AVERAGE_MAGPHASE:
resamplingMethod = "AVERAGE_MAGPHASE";
break;
default:
......@@ -133,56 +217,78 @@ GDALOverviewsBuilder
}
}
/***************************************************************************/
void GDALOverviewsBuilder::Update()
{
typedef itk::SmartPointer<GDALDatasetWrapper> GDALDatasetWrapperPointer;
GDALDatasetWrapperPointer wrappedDataset =
GDALDriverManagerWrapper::GetInstance().Open(m_InputFileName);
if (wrappedDataset.IsNull())
{
itkExceptionMacro(<< "Error while opening the file "<< m_InputFileName.c_str() << ".");
}
if( m_NbOfResolutions==0 )
{
itkExceptionMacro(
<< "Wrong number of resolutions: " << m_NbOfResolutions
);
}
// Build the overviews list from nb of resolution desired
std::vector<int> ovwlist;
unsigned int factor = 1;
for (unsigned int i = 1; i < m_NbOfResolutions; i++)
{
factor*=m_ResolutionFactor;
ovwlist.push_back(factor);
}
/*std::cout << "list of overviews level= ";
// typedef itk::SmartPointer<GDALDatasetWrapper> GDALDatasetWrapperPointer;
// GDALDatasetWrapperPointer wrappedDataset =
// GDALDriverManagerWrapper::GetInstance().Open(m_InputFileName);
// if (wrappedDataset.IsNull())
// {
// itkExceptionMacro(<< "Error while opening the file "<< m_InputFileName.c_str() << ".");
// }
assert( !m_GdalDataset.IsNull() );
if( m_NbResolutions==0 )
{
itkExceptionMacro(
<< "Wrong number of resolutions: " << m_NbResolutions
);
}
// Build the overviews list from nb of resolution desired
std::vector<int> ovwlist;
unsigned int factor = 1;
for (unsigned int i = 1; i < m_NbResolutions; i++)
{
factor*=m_ResolutionFactor;
ovwlist.push_back(factor);
}
/*std::cout << "list of overviews level= ";
for (unsigned int i = 0; i < ovwlist.size(); i++)
{
std::cout << ovwlist[i] << ",";
}
{
std::cout << ovwlist[i] << ",";
}
std::cout << std::endl; */
std::string resampMethod;
this->GetGDALResamplingMethod(resampMethod);
CPLErr lCrGdal = wrappedDataset->GetDataSet()->
BuildOverviews( resampMethod.c_str(),
static_cast<int>(m_NbOfResolutions-1),
&ovwlist.front(),
0, // All bands
NULL, // All bands
(GDALProgressFunc)otb_UpdateGDALProgress,
this);
if (lCrGdal == CE_Failure)
{
itkExceptionMacro(<< "Error while building the GDAL overviews from " << m_InputFileName.c_str() << ".");
}
std::string resampMethod;
this->GetGDALResamplingMethod(resampMethod);
CPLErr lCrGdal = m_GdalDataset->GetDataSet()->
BuildOverviews( resampMethod.c_str(),
static_cast<int>(m_NbResolutions-1),
&ovwlist.front(),
0, // All bands
NULL, // All bands
(GDALProgressFunc)otb_UpdateGDALProgress,
this);
if (lCrGdal == CE_Failure)
{
itkExceptionMacro(<< "Error while building the GDAL overviews from " << m_InputFileName.c_str() << ".");
}
}
/***************************************************************************/
void
GDALOverviewsBuilder
::OpenDataset( const std::string & filename )
{
GDALDatasetWrapper::Pointer dataset(
GDALDriverManagerWrapper::GetInstance().Open( filename )
);
if( dataset.IsNull() )
itkExceptionMacro(
<< "Error while opening file '"
<< filename.c_str()
<< "' as GDAL dataset."
);
m_GdalDataset = dataset;
}
} // end namespace otb
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment