Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][vectortiles] Allow users to optionally specify the URL
for a default style to be applied to connections when they are
first loaded into QGIS

When setting up a vector tile source connection, there's a new option
to enter a URL to a MapBox GL JSON style configuration. If one has
been entered, then that style will be applied whenever the layers
from the connection are added to QGIS.

Works also with Arcgis vectortileservice connections -- in this
case the URL overrides the default style configuration specified
in the server configuration.
  • Loading branch information
nyalldawson committed Sep 9, 2020
1 parent 0f044f9 commit dcf8728
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 100 deletions.
7 changes: 7 additions & 0 deletions src/core/vectortile/qgsvectortileconnection.cpp
Expand Up @@ -38,6 +38,8 @@ QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProvider
uri.setPassword( conn.password );
if ( !conn.referer.isEmpty() )
uri.setParam( QStringLiteral( "referer" ), conn.referer );
if ( !conn.styleUrl.isEmpty() )
uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );

switch ( conn.serviceType )
{
Expand Down Expand Up @@ -65,6 +67,7 @@ QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUr
conn.username = dsUri.username();
conn.password = dsUri.password();
conn.referer = dsUri.param( QStringLiteral( "referer" ) );
conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );

if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
{
Expand Down Expand Up @@ -92,6 +95,8 @@ QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTilePro
uri.setPassword( conn.password );
if ( !conn.referer.isEmpty() )
uri.setParam( QStringLiteral( "referer" ), conn.referer );
if ( !conn.styleUrl.isEmpty() )
uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );

switch ( conn.serviceType )
{
Expand Down Expand Up @@ -131,6 +136,7 @@ QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connectio
conn.username = settings.value( QStringLiteral( "username" ) ).toString();
conn.password = settings.value( QStringLiteral( "password" ) ).toString();
conn.referer = settings.value( QStringLiteral( "referer" ) ).toString();
conn.styleUrl = settings.value( QStringLiteral( "styleUrl" ) ).toString();

if ( settings.contains( QStringLiteral( "serviceType" ) ) )
{
Expand Down Expand Up @@ -159,6 +165,7 @@ void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVec
settings.setValue( QStringLiteral( "username" ), conn.username );
settings.setValue( QStringLiteral( "password" ), conn.password );
settings.setValue( QStringLiteral( "referer" ), conn.referer );
settings.setValue( QStringLiteral( "styleUrl" ), conn.styleUrl );

switch ( conn.serviceType )
{
Expand Down
12 changes: 8 additions & 4 deletions src/core/vectortile/qgsvectortileconnection.h
Expand Up @@ -55,14 +55,18 @@ class CORE_EXPORT QgsVectorTileProviderConnection : public QgsAbstractProviderCo

ServiceType serviceType = Generic;

// Authentication configuration id
//! Authentication configuration id
QString authCfg;
// HTTP Basic username
//! HTTP Basic username
QString username;
// HTTP Basic password
//! HTTP Basic password
QString password;
// Referer
//! Referer
QString referer;

//! Optional style URL (will override any default styles)
QString styleUrl;

};

//! Returns connection data encoded as a string
Expand Down
18 changes: 15 additions & 3 deletions src/core/vectortile/qgsvectortilelayer.cpp
Expand Up @@ -296,10 +296,22 @@ QString QgsVectorTileLayer::loadDefaultStyle( bool &resultFlag )
{
QgsDataSourceUri dsUri;
dsUri.setEncodedUri( mDataSource );
if ( mSourceType == QStringLiteral( "xyz" ) && dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )

QString styleUrl;
if ( !dsUri.param( QStringLiteral( "styleUrl" ) ).isEmpty() )
{
styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
}
else if ( mSourceType == QStringLiteral( "xyz" ) && dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
{
// for ArcMap VectorTileServices we default to the defaultStyles URL from the layer configuration
styleUrl = mArcgisLayerConfiguration.value( QStringLiteral( "serviceUri" ) ).toString()
+ '/' + mArcgisLayerConfiguration.value( QStringLiteral( "defaultStyles" ) ).toString();
}

if ( !styleUrl.isEmpty() )
{
QNetworkRequest request = QNetworkRequest( QUrl( mArcgisLayerConfiguration.value( QStringLiteral( "serviceUri" ) ).toString()
+ '/' + mArcgisLayerConfiguration.value( QStringLiteral( "defaultStyles" ) ).toString() ) );
QNetworkRequest request = QNetworkRequest( QUrl( styleUrl ) );

QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsVectorTileLayer" ) );

Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsmanageconnectionsdialog.cpp
Expand Up @@ -776,6 +776,7 @@ QDomDocument QgsManageConnectionsDialog::saveVectorTileConnections( const QStrin
el.setAttribute( QStringLiteral( "username" ), settings.value( path + "/username" ).toString() );
el.setAttribute( QStringLiteral( "password" ), settings.value( path + "/password" ).toString() );
el.setAttribute( QStringLiteral( "referer" ), settings.value( path + "/referer" ).toString() );
el.setAttribute( QStringLiteral( "styleUrl" ), settings.value( path + "/styleUrl" ).toString() );

root.appendChild( el );
}
Expand Down Expand Up @@ -1626,6 +1627,7 @@ void QgsManageConnectionsDialog::loadVectorTileConnections( const QDomDocument &
settings.setValue( QStringLiteral( "username" ), child.attribute( QStringLiteral( "username" ) ) );
settings.setValue( QStringLiteral( "password" ), child.attribute( QStringLiteral( "password" ) ) );
settings.setValue( QStringLiteral( "referer" ), child.attribute( QStringLiteral( "referer" ) ) );
settings.setValue( QStringLiteral( "styleUrl" ), child.attribute( QStringLiteral( "styleUrl" ) ) );

settings.endGroup();

Expand Down
5 changes: 5 additions & 0 deletions src/gui/vectortile/qgsarcgisvectortileconnectiondialog.cpp
Expand Up @@ -44,6 +44,8 @@ void QgsArcgisVectorTileConnectionDialog::setConnection( const QString &name, co
mAuthSettings->setPassword( conn.password );
mEditReferer->setText( conn.referer );
mAuthSettings->setConfigId( conn.authCfg );

mEditStyleUrl->setText( conn.styleUrl );
}

QString QgsArcgisVectorTileConnectionDialog::connectionUri() const
Expand All @@ -58,6 +60,9 @@ QString QgsArcgisVectorTileConnectionDialog::connectionUri() const
conn.password = mAuthSettings->password();
conn.referer = mEditReferer->text();
conn.authCfg = mAuthSettings->configId( );

conn.styleUrl = mEditStyleUrl->text();

return QgsVectorTileProviderConnection::encodedUri( conn );
}

Expand Down
3 changes: 3 additions & 0 deletions src/gui/vectortile/qgsvectortileconnectiondialog.cpp
Expand Up @@ -52,6 +52,8 @@ void QgsVectorTileConnectionDialog::setConnection( const QString &name, const QS
mAuthSettings->setPassword( conn.password );
mEditReferer->setText( conn.referer );
mAuthSettings->setConfigId( conn.authCfg );

mEditStyleUrl->setText( conn.styleUrl );
}

QString QgsVectorTileConnectionDialog::connectionUri() const
Expand All @@ -66,6 +68,7 @@ QString QgsVectorTileConnectionDialog::connectionUri() const
conn.password = mAuthSettings->password();
conn.referer = mEditReferer->text();
conn.authCfg = mAuthSettings->configId( );
conn.styleUrl = mEditStyleUrl->text();
return QgsVectorTileProviderConnection::encodedUri( conn );
}

Expand Down
100 changes: 60 additions & 40 deletions src/ui/qgsarcgisvectortileconnectiondialog.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>659</width>
<height>336</height>
<width>529</width>
<height>335</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -20,28 +20,42 @@
<string>Connection Details</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="2">
<widget class="QLineEdit" name="mEditReferer">
<property name="toolTip">
<string>Optional custom referer</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="mEditName">
<property name="toolTip">
<string>Name of the new connection</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="lblReferer">
<property name="text">
<string>Referer</string>
</property>
<property name="buddy">
<cstring>mEditReferer</cstring>
<item row="3" column="0" colspan="3">
<widget class="QGroupBox" name="mAuthGroupBox">
<property name="title">
<string>Authentication</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QgsAuthSettingsWidget" name="mAuthSettings" native="true"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
Expand All @@ -61,35 +75,41 @@
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QLineEdit" name="mEditReferer">
<item row="4" column="0">
<widget class="QLabel" name="lblReferer">
<property name="text">
<string>Referer</string>
</property>
<property name="buddy">
<cstring>mEditReferer</cstring>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="mEditName">
<property name="toolTip">
<string>Optional custom referer</string>
<string>Name of the new connection</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="3">
<widget class="QGroupBox" name="mAuthGroupBox">
<property name="title">
<string>Authentication</string>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Style URL</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QLineEdit" name="mEditStyleUrl">
<property name="toolTip">
<string>If specified, will override the default style defined for the layer with the entered URL</string>
</property>
<property name="placeholderText">
<string>Optional</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QgsAuthSettingsWidget" name="mAuthSettings" native="true"/>
</item>
</layout>
</widget>
</item>
</layout>
Expand Down

0 comments on commit dcf8728

Please sign in to comment.