Skip to content

Commit

Permalink
when adding WMS/WFS layers from Browser use project CRS if it is
Browse files Browse the repository at this point in the history
supported by the server (fix #44325)
  • Loading branch information
alexbruy authored and nyalldawson committed Feb 7, 2025
1 parent 446252e commit a37b96b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
23 changes: 21 additions & 2 deletions src/providers/wfs/qgswfsdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "qgswfsdatasourceuri.h"
#include "qgswfsprovider.h"
#include "qgssettings.h"
#include "qgsproject.h"

#ifdef HAVE_GUI
#include "qgswfssourceselect.h"
Expand Down Expand Up @@ -123,13 +124,31 @@ QVector<QgsDataItem *> QgsWfsConnectionItem::createChildren()
QVector<QgsDataItem *> layers;
if ( capabilities.errorCode() == QgsWfsCapabilities::NoError )
{
const QString projectCrs = QgsProject::instance()->crs().authid();
const auto featureTypes = capabilities.capabilities().featureTypes;
for ( const QgsWfsCapabilities::FeatureType &featureType : featureTypes )
{
// if project CRS is supported then use it, otherwise use first available CRS (which is the default CRS)
QString crs;
if ( !featureType.crslist.isEmpty() )
{
for ( const QString &c : std::as_const( featureType.crslist ) )
{
if ( c.compare( projectCrs, Qt::CaseInsensitive ) == 0 )
{
crs = projectCrs;
break;
}
}

if ( crs.isEmpty() )
{
crs = featureType.crslist.first();
}
}
QgsWfsLayerItem *layer = new QgsWfsLayerItem(
this, mName, uri, featureType.name, featureType.title,
!featureType.crslist.isEmpty() ? featureType.crslist.first() : QString(),
QgsWFSProvider::WFS_PROVIDER_KEY
crs, QgsWFSProvider::WFS_PROVIDER_KEY
);
layers.append( layer );
}
Expand Down
29 changes: 18 additions & 11 deletions src/providers/wms/qgswmsdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgswmscapabilities.h"
#include "qgswmsconnection.h"
#include "qgsxyzconnection.h"
#include "qgsproject.h"


// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -455,22 +456,28 @@ QString QgsWMSItemBase::createUri( bool withStyle )
}
mDataSourceUri.setParam( QStringLiteral( "format" ), format );

const QString projectCrs = QgsProject::instance()->crs().authid();
QString crs;
// get first known if possible
QgsCoordinateReferenceSystem testCrs;
for ( const QString &c : std::as_const( mLayerProperty.crs ) )
// if project CRS is supported then use it, otherwise use first available CRS
if ( !mLayerProperty.crs.isEmpty() )
{
testCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( c );
if ( testCrs.isValid() )
QgsCoordinateReferenceSystem testCrs;
for ( const QString &c : std::as_const( mLayerProperty.crs ) )
{
crs = c;
break;
testCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( c );
if ( testCrs.authid().compare( projectCrs, Qt::CaseInsensitive ) == 0 )
{
crs = projectCrs;
break;
}
}

if ( crs.isEmpty() )
{
crs = mLayerProperty.crs[0];
}
}
if ( crs.isEmpty() && !mLayerProperty.crs.isEmpty() )
{
crs = mLayerProperty.crs[0];
}

mDataSourceUri.setParam( QStringLiteral( "crs" ), crs );

// Set default featureCount to 10, old connections might miss this
Expand Down

0 comments on commit a37b96b

Please sign in to comment.