Commit 5640b6a3 authored by Luc Hermitte's avatar Luc Hermitte

ENH: encapsulation of OGR

parent 6f22f577
......@@ -35,6 +35,7 @@ SET(OTB_INCLUDE_DIRS_BUILD_TREE ${OTB_INCLUDE_DIRS_BUILD_TREE}
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/CurlAdapters
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/OssimAdapters
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/TinyXMLAdapters
${OTB_SOURCE_DIR}/Code/UtilitiesAdapters/OGRAdapters
${OTB_SOURCE_DIR}/Code/Visu
${OTB_SOURCE_DIR}/Code/Visualization
${OTB_BINARY_DIR}/Code/Visualization
......
ADD_SUBDIRECTORY(CurlAdapters)
ADD_SUBDIRECTORY(OssimAdapters)
ADD_SUBDIRECTORY(TinyXMLAdapters)
ADD_SUBDIRECTORY(OGRAdapters)
# Sources of non-templated classes.
FILE(GLOB OTBOGRAdapters_SRCS "*.cxx" )
# IF(OTB_USE_EXTERNAL_OSSIM)
# INCLUDE_DIRECTORIES( ${OSSIM_INCLUDE_DIR} )
# ELSE(OTB_USE_EXTERNAL_OSSIM)
# INCLUDE_DIRECTORIES( ${OTB_SOURCE_DIR}/Utilities/otbossim/include
# ${OTB_BINARY_DIR}/Utilities/otbossim/include )
# ENDIF(OTB_USE_EXTERNAL_OSSIM)
ADD_LIBRARY(OTBOGRAdapters ${OTBOGRAdapters_SRCS})
TARGET_LINK_LIBRARIES (OTBOGRAdapters ${OGR_LIBRARY} ITKCommon ${Boost_FILESYSTEM_LIBRARY})
IF(OTB_LIBRARY_PROPERTIES)
SET_TARGET_PROPERTIES(OTBOGRAdapters PROPERTIES ${OTB_LIBRARY_PROPERTIES})
ENDIF(OTB_LIBRARY_PROPERTIES)
IF(NOT OTB_INSTALL_NO_LIBRARIES)
INSTALL(TARGETS OTBOGRAdapters
RUNTIME DESTINATION ${OTB_INSTALL_BIN_DIR_CM24} COMPONENT RuntimeLibraries
LIBRARY DESTINATION ${OTB_INSTALL_LIB_DIR_CM24} COMPONENT RuntimeLibraries
ARCHIVE DESTINATION ${OTB_INSTALL_LIB_DIR_CM24} COMPONENT Development)
ENDIF(NOT OTB_INSTALL_NO_LIBRARIES)
# Note: no txx allowed here
IF(NOT OTB_INSTALL_NO_DEVELOPMENT)
FILE(GLOB __files1 "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
# FILE(GLOB __files2 "${CMAKE_CURRENT_SOURCE_DIR}/*.txx")
# FILE(GLOB __files3 "${CMAKE_CURRENT_BINARY_DIR}/*.h")
# INSTALL(FILES ${__files1} ${__files2} ${__files3}
INSTALL(FILES ${__files1}
DESTINATION ${OTB_INSTALL_INCLUDE_DIR_CM24}/UtilitiesAdapters/OGRAdapters
COMPONENT Development)
ENDIF(NOT OTB_INSTALL_NO_DEVELOPMENT)
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbOGRDataSourceWrapper.h"
// standard includes
#include <cassert>
#include <boost/bind.hpp>
// ITK includes
#include "itkMacro.h" // itkExceptionMacro
#include "itkMetaDataObject.h"
#include "itkExceptionObject.h"
// OTB includes
#include "otbMacro.h"
#include "otbMetaDataKey.h"
#include "otbOGRDriversInit.h"
// OGR includes
#include "ogrsf_frmts.h"
void
otb::ogr::DataSource
::SetProjectionRef(const std::string& projectionRef)
{
itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
itk::EncapsulateMetaData<std::string>(
dict, MetaDataKey::ProjectionRefKey, projectionRef);
this->Modified();
}
std::string
otb::ogr::DataSource
::GetProjectionRef() const
{
const itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
std::string projectionRef;
itk::ExposeMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, projectionRef);
return projectionRef;
}
/*===========================================================================*/
/*=======================[ construction/destruction ]========================*/
/*===========================================================================*/
bool otb::ogr::DataSource::Clear()
{
Reset(0);
return true;
}
void otb::ogr::DataSource::Reset(OGRDataSource * source)
{
if (m_DataSource) {
// OGR makes a pointless check for non-nullity in
// OGRDataSource::DestroyDataSource (pointless because "delete 0" is
// perfectly valid -> it's a no-op)
OGRDataSource::DestroyDataSource(m_DataSource); // void, noexcept
}
m_DataSource = source;
}
otb::ogr::DataSource::DataSource()
: m_DataSource(0)
{
Drivers::Init();
OGRSFDriver * d = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory");
assert(d && "OGR Memory driver not found");
m_DataSource = d->CreateDataSource("in-memory");
if (!m_DataSource) {
itkExceptionMacro(<< "Failed to create OGRMemDataSource");
}
m_DataSource->SetDriver(d);
}
otb::ogr::DataSource::DataSource(OGRDataSource * source)
: m_DataSource(source)
{
}
otb::ogr::DataSource::Pointer
otb::ogr::DataSource::New(std::string const& filename, Modes::type mode)
{
Drivers::Init();
const bool update = mode & Modes::write;
OGRDataSource * source = OGRSFDriverRegistrar::Open(filename.c_str(), update);
if (!source) {
itkGenericExceptionMacro(<< "Failed to open OGRDataSource file " << filename);
}
Pointer res = new DataSource(source);
return res;
}
/*static*/
otb::ogr::DataSource::Pointer
otb::ogr::DataSource::New(OGRDataSource * source)
{
Pointer res = new DataSource(source);
return res;
}
/*virtual*/ otb::ogr::DataSource::~DataSource()
{
Clear();
}
/*===========================================================================*/
/*================================[ layers ]=================================*/
/*===========================================================================*/
OGRLayer& otb::ogr::DataSource::GetLayerChecked(size_t i)
{
const int nb_layers = GetLayersCount();
if (int(i) >= nb_layers)
{
itkExceptionMacro(<< "Cannot fetch " << i << "th layer in the OGRDataSource as it contains only " << nb_layers << "layers.");
}
OGRLayer * layer_ptr = m_DataSource->GetLayer(int(i));
if (!layer_ptr)
{
itkExceptionMacro( << "Unexpected error: cannot fetch " << i << "th layer in the OGRDataSource.");
}
return *layer_ptr;
}
OGRLayer const& otb::ogr::DataSource::GetLayerChecked(size_t i) const
{
return const_cast <DataSource*>(this)->GetLayerChecked(i);
}
OGRLayer* otb::ogr::DataSource::GetLayerUnchecked(size_t i)
{
OGRLayer * layer_ptr = m_DataSource->GetLayer(int(i));
}
int otb::ogr::DataSource::GetLayersCount() const
{
assert(m_DataSource);
return m_DataSource->GetLayerCount();
}
/*===========================================================================*/
/*===============================[ features ]================================*/
/*===========================================================================*/
namespace { // Anonymous namespace
struct AccuLayersSizes
{
AccuLayersSizes(bool doForceComputation)
: m_doForceComputation(doForceComputation) { }
int operator()(OGRLayer& layer, int accumulated) const
{
const int loc_size = layer.GetFeatureCount(m_doForceComputation);
return loc_size < 0 ? loc_size : loc_size+accumulated;
}
private:
bool m_doForceComputation;
};
} // Anonymous namespace
int otb::ogr::DataSource::Size(bool doForceComputation) const
{
return AccumulateOnLayers(AccuLayersSizes(doForceComputation), 0);
}
/*===========================================================================*/
/*=================================[ Misc ]==================================*/
/*===========================================================================*/
/*virtual*/
void otb::ogr::DataSource::PrintSelf(
std::ostream& os, itk::Indent indent) const
{
// ForEachLayer(boost::bind(&OGRLayer::PrintSelf, _1, indent++));
}
/*virtual*/ void otb::ogr::DataSource::Graft(const itk::DataObject * data)
{
assert(! "Disabled to check if it makes sense...");
}
This diff is collapsed.
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbOGRDriversInit.h"
#include "ogr_api.h"
/*static*/
otb::ogr::Drivers & otb::ogr::Drivers::Init()
{
static Drivers theInstance;
return theInstance;
}
otb::ogr::Drivers::Drivers()
{
OGRRegisterAll();
}
otb::ogr::Drivers::~Drivers()
{
OGRCleanupAll();
}
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbOGRDriversInit_h
#define __otbOGRDriversInit_h
#include <boost/noncopyable.hpp>
namespace otb { namespace ogr {
/**\ingroup Geometry
* Singleton-like class to provide lazy-registering of all \c OGRDriver's.
*
* Call
* \code
* otb::ogr::Drivers::Init();
* \endcode
* before using classes wrapping OGR.
*
* <p><b>Properties</b><br>
* - Follows Meyer's Singleton design.
* - Implicitly initialized with a default constructor
* - MT-Safe in C++11, and some compilers like GCC (not on old releases of
* VC++)
* - Non-copyable
*/
class Drivers : private boost::noncopyable
{
/** \name Singleton related functions */
//@{
public:
static Drivers & Init() ;
private:
/** Constructor.
* Calls \c OGRRegisterAll().
*/
Drivers();
/** Destructor.
* Calls \c OGRCleanupAll().
*/
~Drivers();
//@}
};
} } // end namespace otb::ogr
#endif // __otbOGRDriversInit_h
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbOGR_h
#define __otbOGR_h
namespace otb {
/**\defgroup Geometry Geometry
* \ingroup Thematic
* Classes and functions aimed toward the manipulation of set of geometric
* objects.
* This module mainly provides an encapsulation of <a
* href="http://www.gdal.org/ogr/">OGR API</a>.
* @{
*/
namespace ogr { }
//@}
} // namespace otb
#include "otbOGRDriversInit.h"
#include "otbOGRDataSourceWrapper.h"
/**\ingroup Geometry
* \file otbOGR.h
* How to include all include files dedicated to OGR encapsulation in OTB...
*/
#endif // __otbOGR_h
IF( NOT OTB_DISABLE_CXX_TESTING AND BUILD_TESTING )
ADD_SUBDIRECTORY(OGRAdapters)
SET(UtilitiesAdapters_TESTS1 ${CXX_TEST_PATH}/otbUtilitiesAdaptersTests1)
......
IF( NOT OTB_DISABLE_CXX_TESTING AND BUILD_TESTING )
SET(OGR_TESTS ${CXX_TEST_PATH}/otbOGRTests)
SET(OGRCommon_SRC
otbOGRDataSourceWrapperNew.cxx
# otbOGRDataSourceWrapperIO.cxx
)
find_package(Boost COMPONENTS filesystem unit_test_framework)
IF(Boost_FOUND)
MESSAGE(STATUS "Boost_INCLUDE_DIR: ${Boost_INCLUDE_DIR}")
MESSAGE(STATUS "Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
MESSAGE(STATUS "Boost_FILESYSTEM_LIBRARY: ${Boost_FILESYSTEM_LIBRARY}")
MESSAGE(STATUS "Boost_UNIT_TEST_FRAMEWORK_LIBRARY: ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}")
# MESSAGE(STATUS "Boost_DATE_TIME_LIBRARY: ${Boost_DATE_TIME_LIBRARY}")
ELSE(Boost_FOUND)
MESSAGE(FATAL_ERROR
"Cannot build OTB/OGR UT without Boost. Please set Boost_DIR.")
ENDIF(Boost_FOUND)
OTB_ADD_EXECUTABLE(otbOGRTests "${OGRCommon_SRC}" "OTBIO;OTBTesting")
# TARGET_LINK_LIBRARIES(otbOGRTests OTBOGRAdapters OTBTesting itkvcl)
TARGET_LINK_LIBRARIES(otbOGRTests OTBOGRAdapters OTBTesting
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
)
# ------------- otb::OGRDataSourceWrapper ----------------------------
ADD_TEST(coTuOGRDataSourceWrapperNew ${OGR_TESTS}
otbOGRDataSourceWrapperNew
)
# ADD_TEST(coTuOGRDataSourceWrapperIO ${OGR_TESTS}
# otbOGRDataSourceWrapperIO
# )
ENDIF( NOT OTB_DISABLE_CXX_TESTING AND BUILD_TESTING )
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
/*===========================================================================*/
/*===============================[ Includes ]================================*/
/*===========================================================================*/
#include "otbOGRDataSourceWrapper.h"
using namespace otb;
/*===========================================================================*/
/*==============================[ other stuff ]==============================*/
/*===========================================================================*/
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
/*===========================================================================*/
/*===============================[ Includes ]================================*/
/*===========================================================================*/
#include "otbOGRDataSourceWrapper.h"
#include <cstdlib> // EXIT_FAILURE
#include <iostream>
using namespace otb;
/*===========================================================================*/
/*==============================[ other stuff ]==============================*/
/*===========================================================================*/
int otbOGRDataSourceNew (int argc, char **argv)
// int main(int argc, char **argv)
{
if (argc <1)
{
std::cerr << "otbOGRDataSourceNew <shape-file>\n";
return EXIT_FAILURE;
}
ogr::DataSource::Pointer ds = ogr::DataSource::New(argv[1]);
std::cout << "file:\t" << argv[1] << std::endl;
std::cout << "nb layers:\t" << ds->GetLayersCount() << std::endl;
std::cout << "nb features(0):\t" << ds->Size(false) << std::endl;
std::cout << "nb features(1):\t" << ds->Size(true) << std::endl;
return EXIT_SUCCESS;
}
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
/*===========================================================================*/
/*===============================[ Includes ]================================*/
/*===========================================================================*/
#define BOOST_TEST_MODULE "otb::org::DataSource creation unit testing"
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "otbOGRDataSourceWrapper.h"
using namespace otb;
#if 0
#if defined(BOOST_TEST_DYN_LINK)
#error BOOST_TEST_DYN_LINK defined
#else
#error BOOST_TEST_DYN_LINK not defined
#endif
#if defined(BOOST_TEST_MAIN)
#error BOOST_TEST_MAIN defined
#else
#error BOOST_TEST_MAIN not defined
#endif
#if defined(BOOST_TEST_NO_MAIN)
#error BOOST_TEST_NO_MAIN defined
#else
#error BOOST_TEST_NO_MAIN not defined
#endif
#endif
/*===========================================================================*/
/*==============================[ other stuff ]==============================*/
/*===========================================================================*/
BOOST_AUTO_TEST_CASE(OGRDataSource_new)
{
ogr::DataSource::Pointer ds = ogr::DataSource::New();
BOOST_ASSERT(ds);
BOOST_CHECK_EQUAL(ds->GetLayersCount(), 0);
BOOST_CHECK_EQUAL(ds->Size(false), 0);
BOOST_CHECK_EQUAL(ds->Size(true), 0);
BOOST_CHECK_THROW(ds->GetLayerChecked(0), itk::ExceptionObject);
}
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