From c2190454c3bac33dd8d281e6cee4a9b525a3e501 Mon Sep 17 00:00:00 2001 From: Martin Dobias Date: Tue, 18 Oct 2011 23:01:12 -0300 Subject: [PATCH] Separate GDAL data items from provider code --- src/providers/gdal/CMakeLists.txt | 2 +- src/providers/gdal/qgsgdaldataitems.cpp | 116 ++++++++++++++++++++++++ src/providers/gdal/qgsgdaldataitems.h | 18 ++++ src/providers/gdal/qgsgdalprovider.cpp | 111 ----------------------- src/providers/gdal/qgsgdalprovider.h | 18 ++-- 5 files changed, 142 insertions(+), 123 deletions(-) create mode 100644 src/providers/gdal/qgsgdaldataitems.cpp create mode 100644 src/providers/gdal/qgsgdaldataitems.h diff --git a/src/providers/gdal/CMakeLists.txt b/src/providers/gdal/CMakeLists.txt index 7972bf5eba07..2ef891e2c57e 100644 --- a/src/providers/gdal/CMakeLists.txt +++ b/src/providers/gdal/CMakeLists.txt @@ -1,4 +1,4 @@ -SET(GDAL_SRCS qgsgdalprovider.cpp) +SET(GDAL_SRCS qgsgdalprovider.cpp qgsgdaldataitems.cpp) SET(GDAL_MOC_HDRS qgsgdalprovider.h) INCLUDE_DIRECTORIES ( diff --git a/src/providers/gdal/qgsgdaldataitems.cpp b/src/providers/gdal/qgsgdaldataitems.cpp new file mode 100644 index 000000000000..58604238a09f --- /dev/null +++ b/src/providers/gdal/qgsgdaldataitems.cpp @@ -0,0 +1,116 @@ +#include "qgsgdaldataitems.h" +#include "qgsgdalprovider.h" +#include "qgslogger.h" + +#include + +// defined in qgsgdalprovider.cpp +void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString, QStringList & theExtensions, QStringList & theWildcards ); + + +QgsGdalLayerItem::QgsGdalLayerItem( QgsDataItem* parent, + QString name, QString path, QString uri ) + : QgsLayerItem( parent, name, path, uri, QgsLayerItem::Raster, "gdal" ) +{ + mToolTip = uri; + mPopulated = true; // children are not expected +} + +QgsGdalLayerItem::~QgsGdalLayerItem() +{ +} + +QgsLayerItem::Capability QgsGdalLayerItem::capabilities() +{ + // Check if data sour can be opened for update + QgsDebugMsg( "mPath = " + mPath ); + GDALAllRegister(); + GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); + + if ( !hDS ) + return NoCapabilities; + + return SetCrs; +} + +bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) +{ + QgsDebugMsg( "mPath = " + mPath ); + GDALAllRegister(); + GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); + + if ( !hDS ) + return false; + + QString wkt = crs.toWkt(); + if ( GDALSetProjection( hDS, wkt.toLocal8Bit().data() ) != CE_None ) + { + QgsDebugMsg( "Could not set CRS" ); + return false; + } + GDALClose( hDS ); + return true; +} + + +// --------------------------------------------------------------------------- + +static QStringList extensions = QStringList(); +static QStringList wildcards = QStringList(); + +QGISEXTERN int dataCapabilities() +{ + return QgsDataProvider::File | QgsDataProvider::Dir; +} + +QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem ) +{ + if ( thePath.isEmpty() ) + return 0; + + QFileInfo info( thePath ); + if ( info.isFile() ) + { + // Filter files by extension + if ( extensions.isEmpty() ) + { + QString filterString; + buildSupportedRasterFileFilterAndExtensions( filterString, extensions, wildcards ); + QgsDebugMsg( "extensions: " + extensions.join( " " ) ); + QgsDebugMsg( "wildcards: " + wildcards.join( " " ) ); + } + if ( extensions.indexOf( info.suffix().toLower() ) < 0 ) + { + bool matches = false; + foreach( QString wildcard, wildcards ) + { + QRegExp rx( wildcard, Qt::CaseInsensitive, QRegExp::Wildcard ); + if ( rx.exactMatch( info.fileName() ) ) + { + matches = true; + break; + } + } + if ( !matches ) + return 0; + } + + GDALAllRegister(); + GDALDatasetH hDS = GDALOpen( TO8F( thePath ), GA_ReadOnly ); + + if ( !hDS ) + return 0; + + GDALClose( hDS ); + + QgsDebugMsg( "GdalDataset opened " + thePath ); + + QString name = info.completeBaseName(); + QString uri = thePath; + + QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri ); + return item; + } + return 0; +} + diff --git a/src/providers/gdal/qgsgdaldataitems.h b/src/providers/gdal/qgsgdaldataitems.h new file mode 100644 index 000000000000..fef1a4126ce8 --- /dev/null +++ b/src/providers/gdal/qgsgdaldataitems.h @@ -0,0 +1,18 @@ +#ifndef QGSGDALDATAITEMS_H +#define QGSGDALDATAITEMS_H + +#include "qgsdataitem.h" + +class QgsGdalLayerItem : public QgsLayerItem +{ + public: + QgsGdalLayerItem( QgsDataItem* parent, + QString name, QString path, QString uri ); + ~QgsGdalLayerItem(); + + bool setCrs( QgsCoordinateReferenceSystem crs ); + Capability capabilities(); +}; + + +#endif // QGSGDALDATAITEMS_H diff --git a/src/providers/gdal/qgsgdalprovider.cpp b/src/providers/gdal/qgsgdalprovider.cpp index fd13f545b33e..bbceb6ab31d6 100644 --- a/src/providers/gdal/qgsgdalprovider.cpp +++ b/src/providers/gdal/qgsgdalprovider.cpp @@ -46,12 +46,6 @@ #include "cpl_conv.h" #include "cpl_string.h" -#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800 -#define TO8F(x) (x).toUtf8().constData() -#else -#define TO8F(x) QFile::encodeName( x ).constData() -#endif - static QString PROVIDER_KEY = "gdal"; static QString PROVIDER_DESCRIPTION = "GDAL provider"; @@ -1929,108 +1923,3 @@ QGISEXTERN void buildSupportedRasterFileFilter( QString & theFileFiltersString ) QStringList wildcards; buildSupportedRasterFileFilterAndExtensions( theFileFiltersString, exts, wildcards ); } - -QGISEXTERN int dataCapabilities() -{ - return QgsDataProvider::File | QgsDataProvider::Dir; -} - - -QgsGdalLayerItem::QgsGdalLayerItem( QgsDataItem* parent, - QString name, QString path, QString uri ) - : QgsLayerItem( parent, name, path, uri, QgsLayerItem::Raster, "gdal" ) -{ - mToolTip = uri; - mPopulated = true; // children are not expected -} - -QgsGdalLayerItem::~QgsGdalLayerItem() -{ -} - -QgsLayerItem::Capability QgsGdalLayerItem::capabilities() -{ - // Check if data sour can be opened for update - QgsDebugMsg( "mPath = " + mPath ); - GDALAllRegister(); - GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); - - if ( !hDS ) - return NoCapabilities; - - return SetCrs; -} - -bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) -{ - QgsDebugMsg( "mPath = " + mPath ); - GDALAllRegister(); - GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update ); - - if ( !hDS ) - return false; - - QString wkt = crs.toWkt(); - if ( GDALSetProjection( hDS, wkt.toLocal8Bit().data() ) != CE_None ) - { - QgsDebugMsg( "Could not set CRS" ); - return false; - } - GDALClose( hDS ); - return true; -} - -static QStringList extensions = QStringList(); -static QStringList wildcards = QStringList(); - -QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem ) -{ - if ( thePath.isEmpty() ) - return 0; - - QFileInfo info( thePath ); - if ( info.isFile() ) - { - // Filter files by extension - if ( extensions.isEmpty() ) - { - QString filterString; - buildSupportedRasterFileFilterAndExtensions( filterString, extensions, wildcards ); - QgsDebugMsg( "extensions: " + extensions.join( " " ) ); - QgsDebugMsg( "wildcards: " + wildcards.join( " " ) ); - } - if ( extensions.indexOf( info.suffix().toLower() ) < 0 ) - { - bool matches = false; - foreach( QString wildcard, wildcards ) - { - QRegExp rx( wildcard, Qt::CaseInsensitive, QRegExp::Wildcard ); - if ( rx.exactMatch( info.fileName() ) ) - { - matches = true; - break; - } - } - if ( !matches ) - return 0; - } - - GDALAllRegister(); - GDALDatasetH hDS = GDALOpen( TO8F( thePath ), GA_ReadOnly ); - - if ( !hDS ) - return 0; - - GDALClose( hDS ); - - QgsDebugMsg( "GdalDataset opened " + thePath ); - - QString name = info.completeBaseName(); - QString uri = thePath; - - QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri ); - return item; - } - return 0; -} - diff --git a/src/providers/gdal/qgsgdalprovider.h b/src/providers/gdal/qgsgdalprovider.h index db6ed3d93d79..37fb130d4c37 100644 --- a/src/providers/gdal/qgsgdalprovider.h +++ b/src/providers/gdal/qgsgdalprovider.h @@ -39,6 +39,13 @@ class QgsRasterPyramid; #define CPL_SUPRESS_CPLUSPLUS #include +#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800 +#define TO8F(x) (x).toUtf8().constData() +#else +#define TO8F(x) QFile::encodeName( x ).constData() +#endif + + /** \ingroup core * A call back function for showing progress of gdal operations. */ @@ -296,16 +303,5 @@ class QgsGdalProvider : public QgsRasterDataProvider }; -class QgsGdalLayerItem : public QgsLayerItem -{ - public: - QgsGdalLayerItem( QgsDataItem* parent, - QString name, QString path, QString uri ); - ~QgsGdalLayerItem(); - - bool setCrs( QgsCoordinateReferenceSystem crs ); - Capability capabilities(); -}; - #endif