Skip to content
Permalink
Browse files

Separate GDAL data items from provider code

  • Loading branch information
wonder-sk committed Oct 19, 2011
1 parent b28c910 commit c2190454c3bac33dd8d281e6cee4a9b525a3e501
@@ -1,4 +1,4 @@
SET(GDAL_SRCS qgsgdalprovider.cpp)
SET(GDAL_SRCS qgsgdalprovider.cpp qgsgdaldataitems.cpp)
SET(GDAL_MOC_HDRS qgsgdalprovider.h)

INCLUDE_DIRECTORIES (
@@ -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;
}

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

@@ -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.
*/
@@ -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.
You can’t perform that action at this time.