Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate GDAL data items from provider code
- Loading branch information
Showing
5 changed files
with
142 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters