Skip to content
Permalink
Browse files

Automatically populate appropriate layer metadata when adding OpenStr…

…eetMap tile layers
  • Loading branch information
nyalldawson committed Jan 10, 2021
1 parent 760a436 commit ed4bb5069b1991e296265ac248a985ff242bb8a2
@@ -1432,6 +1432,40 @@ void QgsWmsProvider::setupXyzCapabilities( const QString &uri, const QgsRectangl
bbox.crs = mSettings.mCrsId;
bbox.box = sourceExtent.isNull() ? QgsRectangle( topLeft.x(), bottomRight.y(), bottomRight.x(), topLeft.y() ) : sourceExtent;

// metadata
if ( mSettings.mXyz )
{
if ( parsedUri.param( QStringLiteral( "url" ) ).contains( QLatin1String( "openstreetmap" ), Qt::CaseInsensitive ) )
{
mLayerMetadata.setTitle( tr( "OpenStreetMap tiles" ) );
mLayerMetadata.setIdentifier( tr( "OpenStreetMap tiles" ) );
mLayerMetadata.setAbstract( tr( "OpenStreetMap is built by a community of mappers that contribute and maintain data about roads, trails, cafés, railway stations, and much more, all over the world." ) );

QStringList licenses;
licenses << tr( "Open Data Commons Open Database License (ODbL)" );
if ( parsedUri.param( QStringLiteral( "url" ) ).contains( QLatin1String( "tile.openstreetmap.org" ), Qt::CaseInsensitive ) )
{
// OSM tiles have a different attribution requirement to OpenStreetMap data - see https://www.openstreetmap.org/copyright
mLayerMetadata.setRights( QStringList() << tr( "Base map and data from OpenStreetMap and OpenStreetMap Foundation (CC-BY-SA). © https://www.openstreetmap.org and contributors." ) );
licenses << tr( "Creative Commons Attribution-ShareAlike (CC-BY-SA)" );
}
else
mLayerMetadata.setRights( QStringList() << tr( "© OpenStreetMap and contributors (https://www.openstreetmap.org/copyright)." ) );
mLayerMetadata.setLicenses( licenses );

QgsLayerMetadata::SpatialExtent spatialExtent;
spatialExtent.bounds = QgsBox3d( QgsRectangle( topLeftLonLat.x(), bottomRightLonLat.y(), bottomRightLonLat.x(), topLeftLonLat.y() ) );
spatialExtent.extentCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
QgsLayerMetadata::Extent metadataExtent;
metadataExtent.setSpatialExtents( QList< QgsLayerMetadata::SpatialExtent >() << spatialExtent );
mLayerMetadata.setExtent( metadataExtent );
mLayerMetadata.setCrs( QgsCoordinateReferenceSystem( "EPSG:3857" ) );

mLayerMetadata.addLink( QgsAbstractMetadataBase::Link( tr( "Source" ), QStringLiteral( "WWW:LINK" ), QStringLiteral( "https://www.openstreetmap.org/" ) ) );
}
}
mLayerMetadata.setType( QStringLiteral( "dataset" ) );

QgsWmtsTileLayer tl;
tl.tileMode = XYZ;
tl.identifier = QStringLiteral( "xyz" ); // as set in parseUri
@@ -3545,6 +3579,11 @@ QgsCoordinateReferenceSystem QgsWmsProvider::crs() const
return mCrs;
}

QgsRasterDataProvider::ProviderCapabilities QgsWmsProvider::providerCapabilities() const
{
return ProviderCapability::ReadLayerMetadata;
}

QString QgsWmsProvider::lastErrorTitle()
{
return mErrorCaption;
@@ -3589,6 +3628,11 @@ QList<double> QgsWmsProvider::nativeResolutions() const
return mNativeResolutions;
}

QgsLayerMetadata QgsWmsProvider::layerMetadata() const
{
return mLayerMetadata;
}

QVector<QgsWmsSupportedFormat> QgsWmsProvider::supportedFormats()
{
QVector<QgsWmsSupportedFormat> formats;
@@ -136,6 +136,8 @@ class QgsWmsProvider final: public QgsRasterDataProvider

QgsCoordinateReferenceSystem crs() const override;

QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const override;

/**
* Reorder the list of WMS layer names to be rendered by this server
* (in order from bottom to top)
@@ -215,6 +217,7 @@ class QgsWmsProvider final: public QgsRasterDataProvider
QString description() const override;
bool renderInPreview( const QgsDataProvider::PreviewContext &context ) override;
QList< double > nativeResolutions() const override;
QgsLayerMetadata layerMetadata() const override;

static QVector<QgsWmsSupportedFormat> supportedFormats();

@@ -476,6 +479,7 @@ class QgsWmsProvider final: public QgsRasterDataProvider
QStringList mSupportedGetFeatureFormats;

QgsCoordinateReferenceSystem mCrs;
QgsLayerMetadata mLayerMetadata;

//! Parsed response of server's capabilities - initially (or on error) may be invalid
QgsWmsCapabilities mCaps;
@@ -179,6 +179,20 @@ class TestQgsWmsProvider: public QObject
QCOMPARE( encodedUri, uriString );
}

void testOsmMetadata()
{
// test that we auto-populate openstreetmap tile metadata

// don't actually hit the osm server -- the url below uses "file" instead of "http"!
QgsWmsProvider provider( QStringLiteral( "type=xyz&url=file://tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0" ), QgsDataProvider::ProviderOptions(), mCapabilities );
QCOMPARE( provider.layerMetadata().identifier(), QStringLiteral( "OpenStreetMap tiles" ) );
QCOMPARE( provider.layerMetadata().title(), QStringLiteral( "OpenStreetMap tiles" ) );
QVERIFY( !provider.layerMetadata().abstract().isEmpty() );
QCOMPARE( provider.layerMetadata().licenses().at( 0 ), QStringLiteral( "Open Data Commons Open Database License (ODbL)" ) );
QCOMPARE( provider.layerMetadata().licenses().at( 1 ), QStringLiteral( "Creative Commons Attribution-ShareAlike (CC-BY-SA)" ) );
QVERIFY( provider.layerMetadata().rights().at( 0 ).startsWith( "Base map and data from OpenStreetMap and OpenStreetMap Foundation" ) );
}

bool imageCheck( const QString &testType, QgsMapLayer *layer, const QgsRectangle &extent )
{
//use the QgsRenderChecker test utility class to

0 comments on commit ed4bb50

Please sign in to comment.
You can’t perform that action at this time.