Skip to content

Commit

Permalink
Separate GDAL data items from provider code
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Oct 19, 2011
1 parent b28c910 commit c219045
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 123 deletions.
2 changes: 1 addition & 1 deletion 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 (
Expand Down
116 changes: 116 additions & 0 deletions src/providers/gdal/qgsgdaldataitems.cpp
@@ -0,0 +1,116 @@
#include "qgsgdaldataitems.h"
#include "qgsgdalprovider.h"
#include "qgslogger.h"

#include <QFileInfo>

// 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;
}

18 changes: 18 additions & 0 deletions 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
111 changes: 0 additions & 111 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}

18 changes: 7 additions & 11 deletions src/providers/gdal/qgsgdalprovider.h
Expand Up @@ -39,6 +39,13 @@ class QgsRasterPyramid;
#define CPL_SUPRESS_CPLUSPLUS
#include <gdal.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


/** \ingroup core
* A call back function for showing progress of gdal operations.
*/
Expand Down Expand Up @@ -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

0 comments on commit c219045

Please sign in to comment.