Skip to content

Commit

Permalink
QgsDataSourceURI used for WMS provider, all params are now encoded in…
Browse files Browse the repository at this point in the history
… URI, removed WMS specific functions from QgsRasterLayer and QgisAppInterface
  • Loading branch information
blazek committed Apr 17, 2012
1 parent d7e8ae1 commit d637c67
Show file tree
Hide file tree
Showing 24 changed files with 267 additions and 435 deletions.
19 changes: 5 additions & 14 deletions python/core/qgsrasterlayer.sip
Expand Up @@ -35,15 +35,10 @@ public:
bool loadDefaultStyleFlag = true );

/** \brief [ data provider interface ] Constructor in provider mode */
QgsRasterLayer( int dummy,
const QString & baseName = QString(),
const QString & path = QString(),
const QString & providerLib = QString(),
const QStringList & layers = QStringList(),
const QStringList & styles = QStringList(),
const QString & format = QString(),
const QString & crs = QString());

QgsRasterLayer( const QString & uri,
const QString & baseName,
const QString & providerKey,
bool loadDefaultStyleFlag = true );

/** \brief The destructor */
~QgsRasterLayer();
Expand Down Expand Up @@ -195,11 +190,7 @@ public:
QString redBandName();

/** [ data provider interface ] Set the data provider */
void setDataProvider( const QString & provider,
const QStringList & layers,
const QStringList & styles,
const QString & format,
const QString & crs );
void setDataProvider( const QString & provider );

/** \brief Mutator for drawing style */
void setDrawingStyle( const DrawingStyle & theDrawingStyle );
Expand Down
2 changes: 1 addition & 1 deletion python/gui/qgisinterface.sip
Expand Up @@ -46,7 +46,7 @@ class QgisInterface : QObject
//! Add a raster layer given a raster layer file name
virtual QgsRasterLayer* addRasterLayer(QString rasterLayerPath, QString baseName = QString())=0;
//! Add a WMS layer
virtual QgsRasterLayer* addRasterLayer(const QString& url, const QString& layerName, const QString& providerKey, const QStringList& layers, const QStringList& styles, const QString& format, const QString& crs) = 0;
virtual QgsRasterLayer* addRasterLayer(const QString& uri, const QString& baseName, const QString& providerKey) = 0;

//! Add a project
virtual bool addProject(QString theProject)=0;
Expand Down
52 changes: 10 additions & 42 deletions src/app/qgisapp.cpp
Expand Up @@ -746,7 +746,7 @@ void QgisApp::dropEvent( QDropEvent *event )
}
else if ( u.layerType == "raster" )
{
addRasterLayer( u.uri, u.name, u.providerKey, QStringList(), QStringList(), QString(), QString() );
addRasterLayer( u.uri, u.name, u.providerKey );
}
}
}
Expand Down Expand Up @@ -2626,10 +2626,8 @@ void QgisApp::addWmsLayer()
QMessageBox::warning( this, tr( "WMS" ), tr( "Cannot get WMS select dialog from provider." ) );
return;
}
connect( wmss , SIGNAL( addRasterLayer( QString const &, QString const &, QString const &, QStringList const &, QStringList const &, QString const &,
QString const & ) ),
this , SLOT( addRasterLayer( QString const &, QString const &, QString const &, QStringList const &, QStringList const &, QString const &,
QString const & ) ) );
connect( wmss , SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ),
this , SLOT( addRasterLayer( QString const &, QString const &, QString const & ) ) );
wmss->exec();
delete wmss;
}
Expand Down Expand Up @@ -6730,22 +6728,17 @@ QgsRasterLayer* QgisApp::addRasterLayer( QString const & rasterFile, QString con

/** Add a raster layer directly without prompting user for location
The caller must provide information compatible with the provider plugin
using the rasterLayerPath and baseName. The provider can use these
using the uri and baseName. The provider can use these
parameters in any way necessary to initialize the layer. The baseName
parameter is used in the Map Legend so it should be formed in a meaningful
way.
\note Copied from the equivalent addVectorLayer function in this file
TODO Make it work for rasters specifically.
*/
QgsRasterLayer* QgisApp::addRasterLayer(
QString const &rasterLayerPath,
QString const &uri,
QString const &baseName,
QString const &providerKey,
QStringList const & layers,
QStringList const & styles,
QString const &format,
QString const &crs )
QString const &providerKey )
{
QgsDebugMsg( "about to get library for " + providerKey );

Expand All @@ -6756,42 +6749,21 @@ QgsRasterLayer* QgisApp::addRasterLayer(

mMapCanvas->freeze();

// Let render() do its own cursor management
// QApplication::setOverrideCursor(Qt::WaitCursor);

// create the layer
QgsRasterLayer *layer;
/* Eliminate the need to instantiate the layer based on provider type.
The caller is responsible for cobbling together the needed information to
open the layer
*/
QgsDebugMsg( "Creating new raster layer using " + rasterLayerPath
+ " with baseName of " + baseName
+ " and layer list of " + layers.join( ", " )
+ " and style list of " + styles.join( ", " )
+ " and format of " + format
+ " and providerKey of " + providerKey
+ " and CRS of " + crs );
QgsDebugMsg( "Creating new raster layer using " + uri
+ " with baseName of " + baseName );

// TODO: Remove the 0 when the raster layer becomes a full provider gateway.
layer = new QgsRasterLayer( 0, rasterLayerPath, baseName, providerKey, layers, styles, format, crs );
layer = new QgsRasterLayer( uri, baseName, providerKey );

QgsDebugMsg( "Constructed new layer." );

if ( layer && shouldAskUserForGDALSublayers( layer ) )
{
askUserForGDALSublayers( 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.
delete layer;
}
else if ( layer && layer->isValid() )
if ( layer && layer->isValid() )
{
addRasterLayer( layer );

statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );

}
else
{
Expand All @@ -6805,10 +6777,6 @@ QgsRasterLayer* QgisApp::addRasterLayer(
mMapCanvas->freeze( false );
mMapCanvas->refresh();
return layer;

// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();

} // QgisApp::addRasterLayer


Expand Down
15 changes: 4 additions & 11 deletions src/app/qgisapp.h
Expand Up @@ -458,16 +458,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
virtual bool event( QEvent * event );

/** Open a raster layer using the Raster Data Provider.
* Note this is included to support WMS layers only at this stage,
* GDAL layer support via a Provider is not yet implemented.
* \note added in 1.9
*/
QgsRasterLayer* addRasterLayer( QString const & rasterLayerPath,
QString const & baseName,
QString const & providerKey,
QStringList const & layers,
QStringList const & styles,
QString const & format,
QString const & crs );
QgsRasterLayer* addRasterLayer( QString const & uri, QString const & baseName, QString const & providerKey );

void addWfsLayer( QString uri, QString typeName );

Expand Down Expand Up @@ -1190,8 +1183,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
QString mOldScale;

#ifdef HAVE_TOUCH
bool gestureEvent(QGestureEvent *event);
void tapAndHoldTriggered(QTapAndHoldGesture *gesture);
bool gestureEvent( QGestureEvent *event );
void tapAndHoldTriggered( QTapAndHoldGesture *gesture );
#endif
};

Expand Down
5 changes: 2 additions & 3 deletions src/app/qgisappinterface.cpp
Expand Up @@ -107,10 +107,9 @@ QgsRasterLayer* QgisAppInterface::addRasterLayer( QString rasterLayerPath, QStri
return qgis->addRasterLayer( rasterLayerPath, baseName );
}

QgsRasterLayer* QgisAppInterface::addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey,
const QStringList& layers, const QStringList& styles, const QString& format, const QString& crs )
QgsRasterLayer* QgisAppInterface::addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey )
{
return qgis->addRasterLayer( url, baseName, providerKey, layers, styles, format, crs );
return qgis->addRasterLayer( url, baseName, providerKey );
}

bool QgisAppInterface::addProject( QString theProjectName )
Expand Down
3 changes: 1 addition & 2 deletions src/app/qgisappinterface.h
Expand Up @@ -64,8 +64,7 @@ class QgisAppInterface : public QgisInterface
//! Add a raster layer given its file name
QgsRasterLayer* addRasterLayer( QString rasterLayerPath, QString baseName );
//! Add a WMS layer
QgsRasterLayer* addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey,
const QStringList& layers, const QStringList& styles, const QString& format, const QString& crs );
QgsRasterLayer* addRasterLayer( const QString& url, const QString& baseName, const QString& providerKey );

//! Add a project
bool addProject( QString theProjectName );
Expand Down
30 changes: 2 additions & 28 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -293,33 +293,7 @@ void QgsBrowserDockWidget::addLayer( QgsLayerItem *layerItem )
}
if ( type == QgsMapLayer::RasterLayer )
{
// This should go to WMS provider
QStringList URIParts = uri.split( "|" );
QString rasterLayerPath = URIParts.at( 0 );
QStringList layers;
QStringList styles;
QString format;
QString crs;
for ( int i = 1 ; i < URIParts.size(); i++ )
{
QString part = URIParts.at( i );
int pos = part.indexOf( "=" );
QString field = part.left( pos );
QString value = part.mid( pos + 1 );

if ( field == "layers" )
layers = value.split( "," );
if ( field == "styles" )
styles = value.split( "," );
if ( field == "format" )
format = value;
if ( field == "crs" )
crs = value;
}
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
QgsDebugMsg( "layers = " + layers.join( " " ) );

QgisApp::instance()->addRasterLayer( rasterLayerPath, layerItem->name(), providerKey, layers, styles, format, crs );
QgisApp::instance()->addRasterLayer( uri, layerItem->name(), providerKey );
}
}

Expand Down Expand Up @@ -399,7 +373,7 @@ void QgsBrowserDockWidget::showProperties( )
{
QgsDebugMsg( "creating raster layer" );
// should copy code from addLayer() to split uri ?
QgsRasterLayer* layer = new QgsRasterLayer( 0, layerItem->uri(), layerItem->uri(), layerItem->providerKey() );
QgsRasterLayer* layer = new QgsRasterLayer( layerItem->uri(), layerItem->uri(), layerItem->providerKey() );
if ( layer != NULL )
{
layerCrs = layer->crs();
Expand Down
28 changes: 1 addition & 27 deletions src/browser/qgsbrowser.cpp
Expand Up @@ -207,33 +207,7 @@ bool QgsBrowser::layerClicked( QgsLayerItem *item )
}
if ( type == QgsMapLayer::RasterLayer )
{
// This should go to WMS provider
QStringList URIParts = uri.split( "|" );
QString rasterLayerPath = URIParts.at( 0 );
QStringList layers;
QStringList styles;
QString format;
QString crs;
for ( int i = 1 ; i < URIParts.size(); i++ )
{
QString part = URIParts.at( i );
int pos = part.indexOf( "=" );
QString field = part.left( pos );
QString value = part.mid( pos + 1 );

if ( field == "layers" )
layers = value.split( "," );
if ( field == "styles" )
styles = value.split( "," );
if ( field == "format" )
format = value;
if ( field == "crs" )
crs = value;
}
QgsDebugMsg( "rasterLayerPath = " + rasterLayerPath );
QgsDebugMsg( "layers = " + layers.join( " " ) );

mLayer = new QgsRasterLayer( 0, rasterLayerPath, "", providerKey, layers, styles, format, crs );
mLayer = new QgsRasterLayer( uri, "", providerKey );
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -114,10 +114,7 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
if ( ptr->type() == QgsDataItem::Layer )
{
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
if ( layer->providerKey() != "wms" )
{
flags |= Qt::ItemIsDragEnabled;
}
flags |= Qt::ItemIsDragEnabled;
}
if ( ptr->acceptDrop() )
flags |= Qt::ItemIsDropEnabled;
Expand Down Expand Up @@ -354,7 +351,6 @@ QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
if ( ptr->type() != QgsDataItem::Layer ) continue;
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
if ( layer->providerKey() == "wms" ) continue;
lst.append( QgsMimeDataUtils::Uri( layer ) );
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -165,6 +165,7 @@ bool QgsMapLayer::readXML( const QDomNode& layer_node )
mne = mnl.toElement();
mDataSource = mne.text();

// TODO: this should go to providers
if ( provider == "spatialite" )
{
QgsDataSourceURI uri( mDataSource );
Expand Down Expand Up @@ -192,6 +193,71 @@ bool QgsMapLayer::readXML( const QDomNode& layer_node )
urlDest.setQueryItems( urlSource.queryItems() );
mDataSource = QString::fromAscii( urlDest.toEncoded() );
}
else if ( provider == "wms" )
{
// >>> BACKWARD COMPATIBILITY < 1.9
// For project file backward compatibility we must support old format:
// 1. mode: <url>
// example: http://example.org/wms?
// 2. mode: tiled=<width>;<height>;<resolution>;<resolution>...,ignoreUrl=GetMap;GetFeatureInfo,featureCount=<count>,username=<name>,password=<password>,url=<url>
// example: tiled=256;256;0.703;0.351,url=http://example.org/tilecache?
// example: featureCount=10,http://example.org/wms?
// example: ignoreUrl=GetMap;GetFeatureInfo,username=cimrman,password=jara,url=http://example.org/wms?
// This is modified version of old QgsWmsProvider::parseUri
// The new format has always params crs,format,layers,styles and that params
// should not appear in old format url -> use them to identify version
if ( !mDataSource.contains( "crs=" ) && !mDataSource.contains( "format=" ) )
{
QgsDebugMsg( "Old WMS URI format detected -> converting to new format" );
QgsDataSourceURI uri;
if ( !mDataSource.startsWith( "http:" ) )
{
QStringList parts = mDataSource.split( "," );
QStringListIterator iter( parts );
while ( iter.hasNext() )
{
QString item = iter.next();
if ( item.startsWith( "username=" ) )
{
uri.setParam( "username", item.mid( 9 ) );
}
else if ( item.startsWith( "password=" ) )
{
uri.setParam( "password", item.mid( 9 ) );
}
else if ( item.startsWith( "tiled=" ) )
{
QStringList params = item.mid( 6 ).split( ";" );

uri.setParam( "tileWidth", params.takeFirst() );
uri.setParam( "tileHeight", params.takeFirst() );

uri.setParam( "tileResolutions", params );
}
else if ( item.startsWith( "featureCount=" ) )
{
uri.setParam( "featureCount", item.mid( 13 ) );
}
else if ( item.startsWith( "url=" ) )
{
uri.setParam( "url", item.mid( 4 ) );
}
else if ( item.startsWith( "ignoreUrl=" ) )
{
uri.setParam( "ignoreUrl", item.mid( 10 ).split( ";" ) );
}
}
}
else
{
uri.setParam( "url", mDataSource );
}
mDataSource = uri.encodedUri();
// At this point, the URI is obviously incomplete, we add additional params
// in QgsRasterLayer::readXml
}
// <<< BACKWARD COMPATIBILITY < 1.9
}
else
{
mDataSource = QgsProject::instance()->readPath( mDataSource );
Expand Down Expand Up @@ -318,6 +384,7 @@ bool QgsMapLayer::writeXML( QDomNode & layer_node, QDomDocument & document )
QString src = source();

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
// TODO: what about postgres, mysql and others, they should not go through writePath()
if ( vlayer && vlayer->providerType() == "spatialite" )
{
QgsDataSourceURI uri( src );
Expand Down

0 comments on commit d637c67

Please sign in to comment.