Skip to content
Permalink
Browse files
apply #1040
git-svn-id: http://svn.osgeo.org/qgis/trunk@14064 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 11, 2010
1 parent 69b047d commit d72cfb4bebec8ba96c59764b51eb0b9f70f9a476
@@ -42,14 +42,14 @@ QStringList QgsOGRSublayersDialog::getSelection()
return list;
}

void QgsOGRSublayersDialog::populateLayerTable( QStringList theList )
void QgsOGRSublayersDialog::populateLayerTable( QStringList theList, QString delim )
{
for ( int i = 0; i < theList.size(); i++ )
{
QString ligne = theList.at( i );
QStringList elements = ligne.split( ":" );
QString line = theList.at( i );
QStringList elements = line.split( delim );
QStringList item = QStringList();
item << elements.at( 0 ) << elements.at( 1 ) << elements.at( 2 ) << elements.at( 3 );
layersTable -> addTopLevelItem( new QTreeWidgetItem( item ) );
layersTable->addTopLevelItem( new QTreeWidgetItem( item ) );
}
}
@@ -19,18 +19,19 @@

#include <QDialog>
#include <ui_qgsogrsublayersdialogbase.h>


#include "qgscontexthelp.h"

class QgsOGRSublayersDialog : public QDialog, private Ui::QgsOGRSublayersDialogBase
{
Q_OBJECT
public:
QgsOGRSublayersDialog( QWidget* parent = 0, Qt::WFlags fl = 0 );
~QgsOGRSublayersDialog();
void populateLayerTable( QStringList theList );
void populateLayerTable( QStringList theList, QString delim = ":" );
QStringList getSelection();

public slots:
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
};

#endif
@@ -2621,7 +2621,7 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
// sublayers selection dialog so the user can select the sublayers to actually load.
if ( sublayers.count() > 1 )
{
askUserForSublayers( layer );
askUserForOGRSublayers( layer );

// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
@@ -2674,17 +2674,48 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
return true;
} // QgisApp::addVectorLayer()

void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
{
if ( !layer )
return;

QStringList sublayers = layer->subLayers();

QgsDebugMsg( "sublayers:\n " + sublayers.join( " \n" ) + "\n" );

// We initialize a selection dialog and display it.
QgsOGRSublayersDialog chooseSublayersDialog( this );
chooseSublayersDialog.setWindowTitle( tr( "Select raster layers to add..." ) );

QStringList layers;
for ( int i = 0; i < sublayers.size(); i++ )
{
layers << QString( "%1|%2|1|%3" ).arg( i ).arg( sublayers[i] ).arg( tr( "Raster" ) );
}

chooseSublayersDialog.populateLayerTable( layers, "|" );

if ( chooseSublayersDialog.exec() )
{
foreach( QString layer, chooseSublayersDialog.getSelection() )
{
QgsRasterLayer *rlayer = new QgsRasterLayer( layer, layer );
if ( rlayer && rlayer->isValid() )
{
addRasterLayer( rlayer );
}
}
}
}

// This method is the method that does the real job. If the layer given in
// parameter is NULL, then the method tries to act on the activeLayer.
void QgisApp::askUserForSublayers( QgsVectorLayer *layer )
void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
{
if ( layer == NULL )
{
if ( activeLayer() == NULL || activeLayer()->type() != QgsMapLayer::VectorLayer )
return;

layer = ( QgsVectorLayer* ) activeLayer();
if ( layer->dataProvider()->name() != "ogr" )
layer = qobject_cast<QgsVectorLayer *>( activeLayer() );
if ( !layer || layer->dataProvider()->name() != "ogr" )
return;
}

@@ -2693,6 +2724,7 @@ void QgisApp::askUserForSublayers( QgsVectorLayer *layer )

// We initialize a selection dialog and display it.
QgsOGRSublayersDialog chooseSublayersDialog( this );
chooseSublayersDialog.setWindowTitle( tr( "Select vector layers to add..." ) );
chooseSublayersDialog.populateLayerTable( sublayers );

if ( chooseSublayersDialog.exec() )
@@ -6350,16 +6382,28 @@ bool QgisApp::addRasterLayers( QStringList const &theFileNameQStringList, bool g

// create the layer
QgsRasterLayer *layer = new QgsRasterLayer( *myIterator, myBaseNameQString );
QStringList sublayers = layer->subLayers();

addRasterLayer( layer );

//only allow one copy of a ai grid file to be loaded at a
//time to prevent the user selecting all adfs in 1 dir which
//actually represent 1 coverate,
if ( sublayers.size() > 0 )
{
askUserForGDALSublayers( layer );

if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
// The first layer loaded is not useful in that case. The user can select it in
// the list if he wants to load it.
delete layer;
}
else
{
break;
addRasterLayer( layer );

//only allow one copy of a ai grid file to be loaded at a
//time to prevent the user selecting all adfs in 1 dir which
//actually represent 1 coverate,

if ( myBaseNameQString.toLower().endsWith( ".adf" ) )
{
break;
}
}
}
else
@@ -765,10 +765,10 @@ class QgisApp : public QMainWindow
void initializationCompleted();

private:
/** This method will open a dialog so the user can select the sublayers
* to load
/** This method will open a dialog so the user can select the sublayers to load
*/
void askUserForSublayers( QgsVectorLayer *layer );
void askUserForOGRSublayers( QgsVectorLayer *layer );
void askUserForGDALSublayers( QgsRasterLayer *layer );
/** Add a raster layer to the map (passed in as a ptr).
* It won't force a refresh.
*/
@@ -444,10 +444,8 @@ void QgsRasterLayer::buildSupportedRasterFileFilter( QString & theFileFiltersStr
/**
* This helper checks to see whether the file name appears to be a valid raster file name
*/
bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
QString & retErrMsg )
bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString, QString & retErrMsg )
{

GDALDatasetH myDataset;
registerGdalDrivers();

@@ -463,10 +461,15 @@ bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
}
else if ( GDALGetRasterCount( myDataset ) == 0 )
{
GDALClose( myDataset );
myDataset = NULL;
retErrMsg = tr( "This raster file has no bands and is invalid as a raster layer." );
return false;
QStringList layers = subLayers( myDataset );
if ( layers.size() == 0 )
{
GDALClose( myDataset );
myDataset = NULL;
retErrMsg = tr( "This raster file has no bands and is invalid as a raster layer." );
return false;
}
return true;
}
else
{
@@ -476,10 +479,8 @@ bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString,
}

bool QgsRasterLayer::isValidRasterFileName( QString const & theFileNameQString )

{
QString retErrMsg;

return isValidRasterFileName( theFileNameQString, retErrMsg );
}

@@ -2626,7 +2627,7 @@ QString QgsRasterLayer::metadata()
}
else
{
QgsDebugMsg( "band " + QString::number( i ) + "has no metadata" );
QgsDebugMsg( "band " + QString::number( i ) + " has no metadata" );
}

char ** GDALcategories = GDALGetRasterCategoryNames( gdalBand );
@@ -3604,18 +3605,40 @@ void QgsRasterLayer::showStatusMessage( QString const & theMessage )
emit statusChanged( theMessage );
}

QStringList QgsRasterLayer::subLayers() const
QStringList QgsRasterLayer::subLayers( GDALDatasetH dataset )
{
QStringList subLayers;

char **metadata = GDALGetMetadata( dataset, "SUBDATASETS" );
if ( metadata )
{
for ( int i = 0; metadata[i] != NULL; i++ )
{
QString layer = QString::fromUtf8( metadata[i] );

int pos = layer.indexOf( "_NAME=" );
if ( pos >= 0 )
{
subLayers << layer.mid( pos + 6 );
}
}
}

QgsDebugMsg( "sublayers:\n " + subLayers.join( "\n " ) );

return subLayers;
}

QStringList QgsRasterLayer::subLayers() const
{
if ( mDataProvider )
{
return mDataProvider->subLayers();
}
else
{
return QStringList(); // Empty
return subLayers( mGdalDataset );
}

}

void QgsRasterLayer::thumbnailAsPixmap( QPixmap * theQPixmap )
@@ -5258,6 +5281,13 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
GDALReferenceDataset( mGdalDataset );
}

if ( subLayers().size() > 0 )
{
// just to get the sublayers
mValid = false;
return true;
}

//check f this file has pyramids
GDALRasterBandH myGDALBand = GDALGetRasterBand( mGdalDataset, 1 ); //just use the first band
if ( myGDALBand == NULL )
@@ -5270,14 +5300,8 @@ bool QgsRasterLayer::readFile( QString const &theFilename )
mValid = false;
return false;
}
if ( GDALGetOverviewCount( myGDALBand ) > 0 )
{
mHasPyramids = true;
}
else
{
mHasPyramids = false;
}

mHasPyramids = GDALGetOverviewCount( myGDALBand ) > 0;

//populate the list of what pyramids exist
buildPyramidList();
@@ -240,7 +240,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
PalettedMultiBandColor, // currently not supported
MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
//added in 1.6 to fix naming glitch
MultiBandSingleBandGray=MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
MultiBandSingleBandGray = MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
MultiBandSingleBandPseudoColor, //a layer containing 2 or more bands, but a single band drawn using a pseudocolor algorithm
MultiBandColor //a layer containing 2 or more bands, mapped to RGB color space.
//In the case of a multiband with only two bands, one band will be mapped to more than one color.
@@ -286,8 +286,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
* returned in retError.
*/
static bool isValidRasterFileName( const QString & theFileNameQString, QString &retError );

static bool isValidRasterFileName( const QString & theFileNameQString );
static QStringList subLayers( GDALDatasetH dataset );


/** Return time stamp for given file name */
static QDateTime lastModified( const QString & name );
Loading

0 comments on commit d72cfb4

Please sign in to comment.