diff --git a/include/osgTerrain/Layer b/include/osgTerrain/Layer index 7be2b53bf..6f84a4deb 100644 --- a/include/osgTerrain/Layer +++ b/include/osgTerrain/Layer @@ -286,6 +286,25 @@ class OSGTERRAIN_EXPORT CompositeLayer : public Layer Layers _layers; }; +class OSGTERRAIN_EXPORT ProxyLayer : public Layer +{ + public: + + ProxyLayer(); + + /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ + ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + + META_Object(osgTerrain, ProxyLayer); + + + protected: + + virtual ~ProxyLayer() {} + + +}; + } #endif diff --git a/src/osgPlugins/gdal/CMakeLists.txt b/src/osgPlugins/gdal/CMakeLists.txt index e1dbe3e43..e3abf527b 100644 --- a/src/osgPlugins/gdal/CMakeLists.txt +++ b/src/osgPlugins/gdal/CMakeLists.txt @@ -1,6 +1,13 @@ INCLUDE_DIRECTORIES( ${GDAL_INCLUDE_DIR} ) -SET(TARGET_SRC ReaderWriterGDAL.cpp ) +SET(TARGET_SRC + ReaderWriterGDAL.cpp + DataSetLayer.cpp +) + +SET(TARGET_H + DataSetLayer.h +) SET(TARGET_LIBRARIES_VARS GDAL_LIBRARY ) diff --git a/src/osgPlugins/gdal/DataSetLayer.cpp b/src/osgPlugins/gdal/DataSetLayer.cpp new file mode 100644 index 000000000..895d0ec7c --- /dev/null +++ b/src/osgPlugins/gdal/DataSetLayer.cpp @@ -0,0 +1,49 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include "DataSetLayer.h" + +using namespace GDALPlugin; + +DataSetLayer::DataSetLayer() +{ + _dataset = 0; +} + +DataSetLayer::DataSetLayer(const std::string& fileName) +{ + setFileName(fileName); + _dataset = (GDALDataset*)GDALOpen(fileName.c_str(),GA_ReadOnly); +} + +DataSetLayer::DataSetLayer(const DataSetLayer& dataSetLayer,const osg::CopyOp& copyop): + ProxyLayer(dataSetLayer) +{ + _dataset = (GDALDataset*)GDALOpen(getFileName().c_str(),GA_ReadOnly); +} + +DataSetLayer::~DataSetLayer() +{ + if (_dataset) delete _dataset; +} + +unsigned int DataSetLayer::getNumColumns() const +{ + return _dataset!=0 ? _dataset->GetRasterXSize() : 0; +} + +unsigned int DataSetLayer::getNumRows() const +{ + return _dataset!=0 ? _dataset->GetRasterYSize() : 0; +} + diff --git a/src/osgPlugins/gdal/DataSetLayer.h b/src/osgPlugins/gdal/DataSetLayer.h new file mode 100644 index 000000000..bdbdec0c1 --- /dev/null +++ b/src/osgPlugins/gdal/DataSetLayer.h @@ -0,0 +1,51 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef DATASETLAYER +#define DATASETLAYER 1 + +#include + +#include + +namespace GDALPlugin { + +class DataSetLayer : public osgTerrain::ProxyLayer +{ + public: + + DataSetLayer(); + + DataSetLayer(const std::string& fileName); + + /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ + DataSetLayer(const DataSetLayer& dataSetLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + + META_Object(GDALPlugin, DataSetLayer); + + bool valid() const { return _dataset!=0; } + + virtual unsigned int getNumColumns() const; + virtual unsigned int getNumRows() const; + + protected: + + virtual ~DataSetLayer(); + + GDALDataset* _dataset; + +}; + +} + +#endif diff --git a/src/osgPlugins/gdal/ReaderWriterGDAL.cpp b/src/osgPlugins/gdal/ReaderWriterGDAL.cpp index 5c3805523..81fd34e0b 100644 --- a/src/osgPlugins/gdal/ReaderWriterGDAL.cpp +++ b/src/osgPlugins/gdal/ReaderWriterGDAL.cpp @@ -13,6 +13,8 @@ #include +#include "DataSetLayer.h" + #define SERIALIZER() OpenThreads::ScopedLock lock(_serializerMutex) // From easyrgb.com @@ -35,6 +37,25 @@ class ReaderWriterGDAL : public osgDB::ReaderWriter return osgDB::equalCaseInsensitive(extension,"gdal") || osgDB::equalCaseInsensitive(extension,"gdal"); } + virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const + { + OpenThreads::ScopedLock lock(_serializerMutex); + + osg::notify(osg::NOTICE) << "GDALPlugin : " << file << std::endl; + + std::string fileName = osgDB::findDataFile( file, options ); + if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; + + initGDAL(); + + // open a DataSetLayer. + osg::ref_ptr dataset = new GDALPlugin::DataSetLayer; + + if (dataset->valid()) return dataset.release(); + + return ReadResult::FILE_NOT_HANDLED; + } + virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const { OpenThreads::ScopedLock lock(_serializerMutex); @@ -721,7 +742,7 @@ class ReaderWriterGDAL : public osgDB::ReaderWriter } - void initGDAL() + void initGDAL() const { static bool s_initialized = false; if (!s_initialized)